Class: AnsibleTowerClient::HashModel

Inherits:
Object
  • Object
show all
Defined in:
lib/ansible_tower_client/hash_model.rb

Overview

A core base class for JSON wrapper classes. The original data can be a JSON string or a hash parsed from JSON string. Similar to OpenStruct, it auto generates access methods base on key names. If the value is also a hash, it too gets converted to an instance of model class.

This is an abstract class.

Direct Known Subclasses

BaseModel

Instance Method Summary collapse

Constructor Details

#initialize(json_or_hash) ⇒ HashModel

Returns a new instance of HashModel.



84
85
86
87
# File 'lib/ansible_tower_client/hash_model.rb', line 84

def initialize(json_or_hash)
  raw_hash = json_or_hash.kind_of?(Hash) ? json_or_hash : JSON.parse(json_or_hash)
  @data = Hash[raw_hash.collect { |key, value| [key, self.class.send(:convert_value, key, value, self)] }]
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



111
112
113
114
# File 'lib/ansible_tower_client/hash_model.rb', line 111

def ==(other)
  return false unless other.kind_of?(HashModel)
  to_h == other.to_h
end

#[](key) ⇒ Object



89
90
91
# File 'lib/ansible_tower_client/hash_model.rb', line 89

def [](key)
  @data[key]
end

#inspectObject



105
106
107
108
109
# File 'lib/ansible_tower_client/hash_model.rb', line 105

def inspect
  string = "<#{self.class} "
  string << @data.collect { |key, value| "#{self.class.send(:key_to_attribute, key)}=#{value.inspect}" }.join(", ")
  string << ">"
end

#to_hObject Also known as: to_hash



93
94
95
# File 'lib/ansible_tower_client/hash_model.rb', line 93

def to_h
  Hash[@data.collect { |key, value| [key, value_to_hash(value)] }]
end

#to_jsonObject Also known as: to_s



99
100
101
# File 'lib/ansible_tower_client/hash_model.rb', line 99

def to_json
  to_h.to_json
end