Class: SolanaRuby::DataTypes::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/solana_ruby/data_types/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ Layout

Returns a new instance of Layout.



6
7
8
# File 'lib/solana_ruby/data_types/layout.rb', line 6

def initialize(fields)
  @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/solana_ruby/data_types/layout.rb', line 4

def fields
  @fields
end

Instance Method Details

#deserialize(bytes) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/solana_ruby/data_types/layout.rb', line 17

def deserialize(bytes)
  result = {}
  fields.map do |field, type|
    data_type = type.is_a?(Symbol) ? SolanaRuby::DataTypes.send(type) : type
    result[field] = data_type.deserialize(bytes.shift(data_type.size))
  end
  result
end

#serialize(params) ⇒ Object



10
11
12
13
14
15
# File 'lib/solana_ruby/data_types/layout.rb', line 10

def serialize(params)
  fields.flat_map do |field, type|
    data_type = type.is_a?(Symbol) ? SolanaRuby::DataTypes.send(type) : type
    data_type.serialize(params[field])
  end
end