Class: Veriform::Decoder
- Inherits:
-
Object
- Object
- Veriform::Decoder
- Defined in:
- lib/veriform/decoder.rb
Overview
Build Veriform::Objects from Veriform’s self-describing messages
Instance Method Summary collapse
-
#begin_nested ⇒ Object
Push down the internal stack, constructing a new Veriform::Object.
-
#binary(id, value) ⇒ Object
Add binary data to the current object.
-
#end_nested(id) ⇒ Object
Complete the pushdown, adding the newly constructed object to the next one in the stack.
-
#finish ⇒ Object
Finish decoding, returning the parent Veriform::Object.
-
#initialize ⇒ Decoder
constructor
Create a new decoder object which will construct a Veriform::Object tree.
-
#uint64(id, value) ⇒ Object
Add a uint64 to the current object.
Constructor Details
Instance Method Details
#begin_nested ⇒ Object
Push down the internal stack, constructing a new Veriform::Object
25 26 27 |
# File 'lib/veriform/decoder.rb', line 25 def begin_nested @stack << Veriform::Object.new end |
#binary(id, value) ⇒ Object
Add binary data to the current object
18 19 20 21 22 |
# File 'lib/veriform/decoder.rb', line 18 def binary(id, value) raise TypeError, "expected String, got #{value.class}" unless value.is_a?(String) raise EncodingError, "expected BINARY encoding, got #{value.encoding}" unless value.encoding == Encoding::BINARY @stack.last[id] = value end |
#end_nested(id) ⇒ Object
Complete the pushdown, adding the newly constructed object to the next one in the stack
30 31 32 33 34 |
# File 'lib/veriform/decoder.rb', line 30 def end_nested(id) value = @stack.pop raise StateError, "not inside a nested message" if @stack.empty? @stack.last[id] = value end |
#finish ⇒ Object
Finish decoding, returning the parent Veriform::Object
37 38 39 40 41 |
# File 'lib/veriform/decoder.rb', line 37 def finish result = @stack.pop raise StateError, "objects remaining in stack" unless @stack.empty? result end |
#uint64(id, value) ⇒ Object
Add a uint64 to the current object
12 13 14 15 |
# File 'lib/veriform/decoder.rb', line 12 def uint64(id, value) raise TypeError, "expected Integer, got #{value.class}" unless value.is_a?(Integer) @stack.last[id] = value end |