Class: Bytemapper::Chunk
- Inherits:
-
Hash
- Object
- Hash
- Bytemapper::Chunk
- Includes:
- Flattenable, Nameable
- Defined in:
- lib/bytemapper/chunk.rb
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
Attributes included from Nameable
Instance Method Summary collapse
- #capacity ⇒ Object
- #chr ⇒ Object
- #consumed ⇒ Object
-
#initialize(bytes, shape, name) ⇒ Chunk
constructor
A new instance of Chunk.
- #ord ⇒ Object
- #remaining ⇒ Object
- #size ⇒ Object
- #string ⇒ Object
- #unpack(value, endian = nil) ⇒ Object
Methods included from Flattenable
Constructor Details
#initialize(bytes, shape, name) ⇒ Chunk
Returns a new instance of Chunk.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bytemapper/chunk.rb', line 24 def initialize(bytes, shape, name) @name = name @shape = shape @bytes = bytes.is_a?(StringIO) ? bytes : StringIO.new(bytes) replace(shape) each_pair do |k,v| self[k] = if v.is_a?(Hash) Chunk.new(@bytes.read(v.size), v, k) elsif v.is_a?(Bytemapper::Table) if v.unbounded? v.populate(@bytes.read(@bytes.size-@bytes.pos)) else v.populate(@bytes.read(v.capacity)) end else unpack(v) end singleton_class.instance_eval { attr_reader k } instance_variable_set("@#{k.to_s}", self[k]) end shape.hooks.each {|h| h.call(self) } end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes.
22 23 24 |
# File 'lib/bytemapper/chunk.rb', line 22 def bytes @bytes end |
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
22 23 24 |
# File 'lib/bytemapper/chunk.rb', line 22 def shape @shape end |
Instance Method Details
#capacity ⇒ Object
60 61 62 |
# File 'lib/bytemapper/chunk.rb', line 60 def capacity shape.size end |
#chr ⇒ Object
56 57 58 |
# File 'lib/bytemapper/chunk.rb', line 56 def chr bytes.string.split(//).map(&:chr) end |
#consumed ⇒ Object
68 69 70 |
# File 'lib/bytemapper/chunk.rb', line 68 def consumed size end |
#ord ⇒ Object
52 53 54 |
# File 'lib/bytemapper/chunk.rb', line 52 def ord bytes.string.split(//).map(&:ord) end |
#remaining ⇒ Object
72 73 74 |
# File 'lib/bytemapper/chunk.rb', line 72 def remaining capacity - consumed end |
#size ⇒ Object
64 65 66 |
# File 'lib/bytemapper/chunk.rb', line 64 def size bytes.size end |
#string ⇒ Object
48 49 50 |
# File 'lib/bytemapper/chunk.rb', line 48 def string bytes.string end |
#unpack(value, endian = nil) ⇒ Object
76 77 78 79 80 |
# File 'lib/bytemapper/chunk.rb', line 76 def unpack(value, endian = nil) num_bytes, flag = value _bytes = bytes.read(num_bytes >> 3) _bytes.unpack("#{flag}#{endian}")[0] unless _bytes.nil? end |