Class: BinaryBlocker::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/blocker.rb

Overview

This is the base class for all the various encoders. It supports a variety of options (as Symbols in a Hash):

default

used as the default value for the element

pre_block

passed the value before being blocked

post_block

passed the blocked value before returned

pre_deblock

passed the io before attempting to deblock it (hard to imagine why you would need this but I was compelled by orthaganality)

post_deblock

passed the deblocked value before being stored internally

get_filter

more info

set_filter

all done

It also supports either a string or io parameter which will be used to initialize the class

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Encoder

Parameters: (io | buf, options_hash)

Options (lambda):

default

used as the default value for the element

pre_block

passed the value before being blocked

post_block

passed the blocked value before returned

pre_deblock

passed the io before attempting to deblock it (hard to imagine why you would need this but I was compelled by orthaganality)

post_deblock

passed the deblocked value before being stored internally

get_filter

more info

set_filter

all done



123
124
125
126
# File 'lib/blocker.rb', line 123

def initialize(*opts)
  initialize_options(*opts)
  initialize_data(*opts)
end

Instance Method Details

#block(io = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/blocker.rb', line 129

def block(io=nil)
  val = if @pre_block
    @pre_block.call(self.value)
  else
    self.value
  end
  result = internal_block(val)
  if @post_block
    result = @post_block.call(result)
  end
  io.write(result) if io
  result
end

#deblock(io) ⇒ Object

This routine takes an io and will parse the stream on success it returns the object, on failure it returns a nil



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/blocker.rb', line 146

def deblock(io)
  with_guarded_io_pos(io) do
    if @pre_deblock
      # does this serve any real purpose? other
      # than making me feel good and orthoginal
      io = @pre_deblock.call(io)
    end
    self.value = internal_deblock(io)
    if @post_deblock
      self.value = @post_deblock.call(self.value)
    end
    self.value || self.valid?
  end
end

#key_value?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/blocker.rb', line 161

def key_value?
  @opts[:key]
end

#meObject



106
107
108
# File 'lib/blocker.rb', line 106

def me
  self.class.superclass.to_s
end