Class: BinaryBlocker::SimpleEncoder

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Encoder

#block, #deblock, #initialize, #key_value?, #me

Constructor Details

This class inherits a constructor from BinaryBlocker::Encoder

Class Method Details

.register(sym, fmt, *opts) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/blocker.rb', line 415

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

#inspectObject



453
454
455
# File 'lib/blocker.rb', line 453

def inspect
  "#{@format} - #{@length} - #{self.value.inspect}"
end

#internal_block(val) ⇒ Object



431
432
433
434
435
436
437
# File 'lib/blocker.rb', line 431

def internal_block(val)
  if val.nil?
    [0].pack(@format)
  else
    [val].pack(@format)
  end
end

#internal_deblock(io) ⇒ Object



439
440
441
442
443
# File 'lib/blocker.rb', line 439

def internal_deblock(io)
  buffer = io.read(@length)
  result = buffer.unpack(@format)
  result.first
end

#valid?Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
# File 'lib/blocker.rb', line 445

def valid?
  if @valid
    @valid.call(self.value)
  else
    self.value != nil && (@key == nil || @key === self.value)
  end
end