Class: Exonum::StructT
- Inherits:
-
Object
- Object
- Exonum::StructT
- Defined in:
- lib/exonum/types/struct.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
Instance Method Summary collapse
- #fixed? ⇒ Boolean
-
#initialize(fields) ⇒ StructT
constructor
A new instance of StructT.
- #serialize(data, buffer, from = 0, shift = 0, isTransactionBody = false) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(fields) ⇒ StructT
Returns a new instance of StructT.
5 6 7 8 9 10 11 12 |
# File 'lib/exonum/types/struct.rb', line 5 def initialize fields raise 'Expecting array parameter' unless fields.is_a?(Array) fields.each do |field| raise 'Name property is missing' unless field[:name].present? raise 'Type property is missing' unless field[:type].present? end self.fields = fields end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
3 4 5 |
# File 'lib/exonum/types/struct.rb', line 3 def fields @fields end |
Instance Method Details
#fixed? ⇒ Boolean
24 25 26 |
# File 'lib/exonum/types/struct.rb', line 24 def fixed? fields.map {|f| f[:type].fixed? }.reduce(&:&) end |
#serialize(data, buffer, from = 0, shift = 0, isTransactionBody = false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/exonum/types/struct.rb', line 28 def serialize data, buffer, from=0, shift=0, isTransactionBody=false (0..(size-1)).each do |i| buffer[from+i] = 0 end localShift = 0 fields.each do |field| value = data.with_indifferent_access[field[:name]] raise "Field #{field[:name]} is not defined" if value.nil? localFrom = from + localShift nestedShift = isTransactionBody ? 0 : shift if field[:type].is_a?(StructT) if field[:type].fixed? field[:type].serialize value, buffer, localFrom, nestedShift localShift += field[:type].size else bufferLengthOld = buffer.length UInt32T.serialize(bufferLengthOld - nestedShift, buffer, localFrom) # index where string content starts in buffer field[:type].serialize value, buffer, bufferLengthOld, nestedShift bufferLengthNew = buffer.length UInt32T.serialize(bufferLengthNew - bufferLengthOld, buffer, localFrom + 4) # string length localShift += 8 end else field[:type].serialize value, buffer, localFrom, nestedShift localShift += field[:type].size #puts "\n\n#{field[:type].class.name}, #{value}, #{localFrom}, #{nestedShift}" #puts buffer.serialize.inspect end end end |
#size ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/exonum/types/struct.rb', line 14 def size fields.map do |field| if field[:type].fixed? field[:type].size else 8 end end.reduce(&:+) end |