Class: BinaryBlocker::CountedArrayEncoder

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

Instance Method Summary collapse

Methods inherited from GroupEncoder

attributes, attributes=, #block, clear_registered_klasses, #clone, has_bit_field, has_counted_array, has_fixed_array, has_list_of, has_one, has_one_of, include_klasses, inherited, keys, klasses, klasses=, lookup, lookup=, #method_missing, #orig_clone, register_klass, #to_h, #valid?, #value, #value=

Methods inherited from Encoder

#block, #key_value?, #me

Constructor Details

#initialize(count_type, classes, *opts) ⇒ CountedArrayEncoder

Returns a new instance of CountedArrayEncoder.



953
954
955
956
957
958
959
960
961
# File 'lib/blocker.rb', line 953

def initialize(count_type, classes, *opts)
  # this is dynamic now, but we init to zero for the range checking
  @count_enc = BinaryBlocker.pack_symbols[count_type] || count_type
  @count = 0 
  initialize_options(*opts)
  @classes = classes
  @value = []
  initialize_data(*opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BinaryBlocker::GroupEncoder

Instance Method Details

#<<(val) ⇒ Object



990
991
992
993
# File 'lib/blocker.rb', line 990

def <<(val)
  @count += 1
  @value << val
end

#[](offset) ⇒ Object

Raises:

  • (RangeError)


980
981
982
983
# File 'lib/blocker.rb', line 980

def [](offset)
  raise RangeError.new("Access (#{offset}) out of range (#{@count})") unless (0...@count) === offset
  @value[offset]
end

#[]=(offset, val) ⇒ Object

Raises:

  • (RangeError)


985
986
987
988
# File 'lib/blocker.rb', line 985

def []=(offset,val)
  raise RangeError.new("Access (#{offset}) out of range (#{@count})") unless (0...@count) === offset
  @value[offset] = val
end

#deblock(io) ⇒ Object



968
969
970
971
972
973
974
975
976
977
978
# File 'lib/blocker.rb', line 968

def deblock(io)
  length = BinaryBlocker.sizeof_format(@count_enc)
  result = []
  with_guarded_io_pos(io) do
    @count = io.read(length).unpack(@count_enc)
    @count.times do
      result << OneOfEncoder.new(@classes, io, @opts)
    end
  end 
  @value = result
end

#internal_block(val) ⇒ Object



963
964
965
966
# File 'lib/blocker.rb', line 963

def internal_block(val)
  buf = [val.size].pack(@count_enc)
  val.inject(buf) { |r, o| r + o.block }
end

#sizeObject Also known as: length



995
996
997
# File 'lib/blocker.rb', line 995

def size
  @count
end