Class | BinaryBlocker::GroupEncoder |
In: |
lib/blocker.rb
|
Parent: | Encoder |
All Encoders that store multiple items subclass from here.
# File lib/blocker.rb, line 259 259: def has_bit_field(sym, type, bit_info, *opts) 260: @lookup[sym] = @attributes.size 261: @attributes << lambda { BitFieldEncoder.new(type, bit_info, *opts) } 262: end
# File lib/blocker.rb, line 247 247: def has_counted_array(sym, count_type, klasses, *opts) 248: klasses = include_klasses(klasses, *opts) 249: @lookup[sym] = @attributes.size 250: @attributes << lambda { CountedArrayEncoder.new(count_type, klasses, *opts) } 251: end
# File lib/blocker.rb, line 253 253: def has_fixed_array(sym, count, klasses, *opts) 254: klasses = include_klasses(klasses, *opts) 255: @lookup[sym] = @attributes.size 256: @attributes << lambda { FixedArrayEncoder.new(count, klasses, *opts) } 257: end
# File lib/blocker.rb, line 264 264: def has_list_of(sym, klasses, *opts) 265: klasses = include_klasses(klasses, *opts) 266: @lookup[sym] = @attributes.size 267: @attributes << lambda { ListOfEncoder.new(klasses, *opts) } 268: end
One and only one (this is the easiest :-)
# File lib/blocker.rb, line 224 224: def has_one(sym, klass, *opts) 225: klass = @klasses[klass] if @klasses[klass] 226: @lookup[sym] = @attributes.size 227: @attributes << lambda { klass.new(*opts) } 228: end
# File lib/blocker.rb, line 241 241: def has_one_of(sym, klasses, *opts) 242: klasses = include_klasses(klasses, *opts) 243: @lookup[sym] = @attributes.size 244: @attributes << lambda { OneOfEncoder.new(klasses, *opts) } 245: end
# File lib/blocker.rb, line 230 230: def include_klasses(klasses, *opts) 231: klasses = klasses.map do |k| 232: case 233: when @klasses[k] : lambda { @klasses[k].new(*opts) } 234: when k.respond_to?(:call) : k 235: when k.respond_to?(:new) : lambda { k.new(*opts) } 236: else raise "Unable to process class: #{k}" 237: end 238: end 239: end
# File lib/blocker.rb, line 214 214: def inherited(obj) 215: obj.instance_eval do 216: @klasses = BinaryBlocker.klasses.clone 217: @attributes = [] 218: @lookup = {} 219: end 220: super 221: end
# File lib/blocker.rb, line 288 288: def initialize(*opts) 289: @lookup = self.class.lookup.clone 290: @value = self.class.attributes.map { |a| a.call } 291: super 292: end
# File lib/blocker.rb, line 270 270: def register_klass(sym, klass) 271: @klasses ||= {} 272: @klasses[sym] = klass 273: end
# File lib/blocker.rb, line 294 294: def block 295: @value.inject("") { |a,b| a + b.block } 296: end
# File lib/blocker.rb, line 298 298: def deblock(io) 299: BinaryBlocker.with_guarded_io_pos(io) do 300: @value.all? { |o| o.deblock(io) } 301: end 302: end
# File lib/blocker.rb, line 304 304: def method_missing(sym, *args) 305: super unless @lookup 306: if pos = @lookup[sym] 307: return @value[pos].value 308: else 309: sym = sym.to_s 310: if sym[-1] == ?= 311: if pos = @lookup[sym[0..-2].to_sym] 312: return @value[pos].value = args.first 313: end 314: end 315: end 316: super 317: end