Class: BinaryBlocker::SimpleEncoder
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Encoder
#block, #deblock, #initialize, #key_value?, #me
Class Method Details
.register(sym, fmt, *opts) ⇒ Object
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/blocker.rb', line 410
def self.register(sym, fmt, *opts)
klass = Class.new(SimpleEncoder)
klass.send(:define_method,:initialize) do |*opts|
initialize_options(*opts)
@format = fmt
@length = BinaryBlocker.sizeof_format(@format)
@key = @opts[:key]
@valid = @opts[:valid]
initialize_data(*opts)
end
BinaryBlocker.register_klass(sym, klass)
end
|
Instance Method Details
#inspect ⇒ Object
448
449
450
|
# File 'lib/blocker.rb', line 448
def inspect
"#{@format} - #{@length} - #{self.value.inspect}"
end
|
#internal_block(val) ⇒ Object
426
427
428
429
430
431
432
|
# File 'lib/blocker.rb', line 426
def internal_block(val)
if val.nil?
[0].pack(@format)
else
[val].pack(@format)
end
end
|
#internal_deblock(io) ⇒ Object
434
435
436
437
438
|
# File 'lib/blocker.rb', line 434
def internal_deblock(io)
buffer = io.read(@length)
result = buffer.unpack(@format)
result.first
end
|
#valid? ⇒ Boolean
440
441
442
443
444
445
446
|
# File 'lib/blocker.rb', line 440
def valid?
if @valid
@valid.call(self.value)
else
self.value != nil && (@key == nil || @key === self.value)
end
end
|