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



118
119
120
121
# File 'lib/blocker.rb', line 118

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

Instance Method Details

#block(io = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/blocker.rb', line 124

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/blocker.rb', line 141

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



156
157
158
# File 'lib/blocker.rb', line 156

def key_value?
  @opts[:key]
end

#meObject



101
102
103
# File 'lib/blocker.rb', line 101

def me
  self.class.superclass.to_s
end