Class: AnsibleTowerClient::HashModel
- Inherits:
-
Object
- Object
- AnsibleTowerClient::HashModel
- 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
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
-
#initialize(json_or_hash) ⇒ HashModel
constructor
A new instance of HashModel.
- #inspect ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #to_json ⇒ Object (also: #to_s)
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 |
#inspect ⇒ Object
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_h ⇒ Object 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_json ⇒ Object 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 |