Class: Imperium::APIObject
- Inherits:
-
Object
- Object
- Imperium::APIObject
- Defined in:
- lib/imperium/api_object.rb
Overview
Base class for handling data coming from the Consul API
Direct Known Subclasses
Class Attribute Summary collapse
-
.attribute_map ⇒ Hash<String => Symbol>
The mapping of attribute names coming from Consul to names that are more Ruby friendly.
-
.ruby_attribute_names ⇒ Array<Symbol>
readonly
The Ruby friendly names from APIObject.attribute_map.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attribute_map ⇒ Object
-
#initialize(attributes = {}) ⇒ APIObject
constructor
Initialize a new object extracting attributes from the supplied hash.
-
#ruby_attribute_names ⇒ Array<Symbol>
Shortcut method to access the class level attribute.
-
#to_h(consul_names_as_keys: true) ⇒ Object
Convert the object and any sub-objects into a hash.
Constructor Details
#initialize(attributes = {}) ⇒ APIObject
Initialize a new object extracting attributes from the supplied hash
25 26 27 28 29 30 |
# File 'lib/imperium/api_object.rb', line 25 def initialize(attributes = {}) self.class.attribute_map.each do |key, attribute_name| value = attributes[attribute_name] || attributes[key] send("#{attribute_name}=", value) if value end end |
Class Attribute Details
.attribute_map ⇒ Hash<String => Symbol>
The mapping of attribute names coming from Consul to names that are more Ruby friendly
11 12 13 |
# File 'lib/imperium/api_object.rb', line 11 def attribute_map @attribute_map end |
.ruby_attribute_names ⇒ Array<Symbol> (readonly)
The Ruby friendly names from attribute_map
15 16 17 |
# File 'lib/imperium/api_object.rb', line 15 def ruby_attribute_names @ruby_attribute_names end |
Instance Method Details
#==(other) ⇒ Object
32 33 34 35 |
# File 'lib/imperium/api_object.rb', line 32 def ==(other) return false unless self.class == other.class ruby_attribute_names.all? { |attr| self.send(attr) == other.send(attr) } end |
#attribute_map ⇒ Object
37 38 39 |
# File 'lib/imperium/api_object.rb', line 37 def attribute_map self.class.attribute_map end |
#ruby_attribute_names ⇒ Array<Symbol>
Shortcut method to access the class level attribute
43 44 45 |
# File 'lib/imperium/api_object.rb', line 43 def ruby_attribute_names self.class.ruby_attribute_names end |
#to_h(consul_names_as_keys: true) ⇒ Object
Convert the object and any sub-objects into a hash
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/imperium/api_object.rb', line 51 def to_h(consul_names_as_keys: true) if consul_names_as_keys attribute_map.each_with_object({}) do |(consul, ruby), h| h[consul] = maybe_hashified_attribute(ruby, true) end.compact else ruby_attribute_names.each_with_object({}) do |attr, h| h[attr] = maybe_hashified_attribute(attr, false) end.compact end end |