Class: BinaryBlocker::PackedNumberEncoder

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) ⇒ PackedNumberEncoder

Returns a new instance of PackedNumberEncoder.

Raises:

  • (ArgumentError)


538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/blocker.rb', line 538

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



553
554
555
# File 'lib/blocker.rb', line 553

def internal_block(val)
  ["%0#{@length}d" % val.to_i].pack(@format)
end

#internal_deblock(io) ⇒ Object



557
558
559
560
561
# File 'lib/blocker.rb', line 557

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