Class | BinaryBlocker::Encoder |
In: |
lib/blocker.rb
|
Parent: | Object |
This is the base class for all the various encoders. It supports a variety of options (as Symbols in a Hash):
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
Parameters: (io | buf, options_hash)
Options (lambda):
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 |
# File lib/blocker.rb, line 108 108: def initialize(*opts) 109: initialize_options(*opts) 110: initialize_data(*opts) 111: end
# File lib/blocker.rb, line 114 114: def block(io=nil) 115: val = if @pre_block 116: @pre_block.call(self.value) 117: else 118: self.value 119: end 120: result = internal_block(val) 121: if @post_block 122: result = @post_block.call(result) 123: end 124: io.write(result) if io 125: result 126: end
This routine takes an io and will parse the stream on success it returns the object, on failure it returns a nil
# File lib/blocker.rb, line 131 131: def deblock(io) 132: with_guarded_io_pos(io) do 133: if @pre_deblock 134: # does this serve any real purpose? other 135: # than making me feel good and orthoginal 136: io = @pre_deblock.call(io) 137: end 138: self.value = internal_deblock(io) 139: if @post_deblock 140: self.value = @post_deblock.call(self.value) 141: end 142: self.value 143: end 144: end
# File lib/blocker.rb, line 184 184: def initialize_data(*opts) 185: data = opts.find { |o| !o.respond_to? :to_hash } 186: if data.respond_to? :to_str 187: raise "Unable to parse block" unless deblock(StringIO.new(data)) 188: elsif data.respond_to? :read 189: raise "Unable to parse block" unless deblock(data) 190: end 191: end
# File lib/blocker.rb, line 168 168: def initialize_options(*opts) 169: @opts ||= {} 170: opts = opts.find { |o| o.respond_to? :to_hash } 171: if opts 172: @opts = @opts.merge(opts) 173: @pre_block = @opts[:pre_block] 174: @pre_deblock = @opts[:pre_deblock] 175: 176: @get_filter = @opts[:get_filter] 177: @set_filter = @opts[:set_filter] 178: 179: @post_block = @opts[:post_block] 180: @post_deblock = @opts[:post_deblock] 181: end 182: end
Override valid? to allow check constraints on a particular record type, on failure
# File lib/blocker.rb, line 150 150: def valid? 151: true 152: end
# File lib/blocker.rb, line 154 154: def value 155: v = @value 156: if @get_filter 157: @get_filter.call(v) 158: else 159: v 160: end 161: end
# File lib/blocker.rb, line 163 163: def value=(val) 164: val = @set_filter.call(val) if @set_filter 165: @value = val 166: end