Class: Sumac::Message::Object::HashTable

Inherits:
Base show all
Defined in:
lib/sumac/message/object/hash_table.rb

Instance Method Summary collapse

Methods inherited from Base

from_json_structure, from_native_object

Methods inherited from Sumac::Message::Object

from_json_structure, from_native_object

Methods inherited from Sumac::Message

from_json, #to_json

Constructor Details

#initialize(connection) ⇒ HashTable

Returns a new instance of HashTable.



6
7
8
9
# File 'lib/sumac/message/object/hash_table.rb', line 6

def initialize(connection)
  super
  @entries = nil
end

Instance Method Details

#invert_orginObject

Raises:



57
58
59
60
61
62
63
64
# File 'lib/sumac/message/object/hash_table.rb', line 57

def invert_orgin
  raise MessageError unless setup?
  @entries.each do |entry|
    entry['key'].invert_orgin if entry['key'].respond_to?(:invert_orgin)
    entry['value'].invert_orgin if entry['value'].respond_to?(:invert_orgin)
  end
  nil
end

#parse_json_structure(json_structure) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sumac/message/object/hash_table.rb', line 11

def parse_json_structure(json_structure)
  raise MessageError unless json_structure.is_a?(::Hash) &&
    json_structure['message_type'] == 'object' &&
    json_structure['object_type'] == 'hash_table'
  raise MessageError unless json_structure['entries'].is_a?(::Array)
  @entries = json_structure['entries'].map do |entry|
    raise MessageError unless entry.is_a?(::Hash) && entry.include?('key') && entry.include?('value')
    {
      'key' => Object.from_json_structure(@connection, entry['key']),
      'value' => Object.from_json_structure(@connection, entry['value'])
    }
  end
  nil
end

#parse_native_object(native_object) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
# File 'lib/sumac/message/object/hash_table.rb', line 26

def parse_native_object(native_object)
  raise MessageError unless native_object.is_a?(::Hash)
  @entries = native_object.map do |key, value|
    {
      'key' => Object.from_native_object(@connection, key),
      'value' => Object.from_native_object(@connection, value)
    }
  end
  nil
end

#to_json_structureObject

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sumac/message/object/hash_table.rb', line 37

def to_json_structure
  raise MessageError unless setup?
  json_entries = @entries.map do |entry|
    {
      'key' => entry['key'].to_json_structure,
      'value' => entry['value'].to_json_structure
    }
  end
  {
    'message_type' => 'object',
    'object_type' => 'hash_table',
    'entries' => json_entries
  }
end

#to_native_objectObject

Raises:



52
53
54
55
# File 'lib/sumac/message/object/hash_table.rb', line 52

def to_native_object
  raise MessageError unless setup?
  @entries.map { |entry| [entry['key'].to_native_object, entry['value'].to_native_object] }.to_h
end