Class: BinaryBlocker::NewPackedNumberEncoder

Inherits:
SimpleEncoder show all
Defined in:
lib/blocker.rb

Instance Method Summary collapse

Methods inherited from SimpleEncoder

#inspect, register, #valid?

Methods inherited from Encoder

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

Constructor Details

#initialize(*opts) ⇒ NewPackedNumberEncoder

Returns a new instance of NewPackedNumberEncoder.

Raises:

  • (ArgumentError)


566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/blocker.rb', line 566

def initialize(*opts)
  initialize_options(*opts)
  
  @length = @opts[:length].to_i
  raise ArgumentError.new("Missing or invalid string length") unless @length > 0
  @length += 1 if @length[0] == 1
  @bytes = @length / 2
  @format = "H#{@length}"
  
  @key = @opts[:key]
  @valid = @opts[:valid]
  
  initialize_data(*opts)
end

Instance Method Details

#internal_block(val) ⇒ Object



581
582
583
# File 'lib/blocker.rb', line 581

def internal_block(val)
  [val.to_s.rjust(@length,"\xff")].pack(@format)
end

#internal_deblock(io) ⇒ Object



585
586
587
588
589
# File 'lib/blocker.rb', line 585

def internal_deblock(io)
  buffer = io.read(@bytes)
  result = buffer.unpack(@format)
  result.first.match(/(\d+)/)[0].to_i
end