Class: Warframe::Models::ObjectifiedHash
- Inherits:
-
Object
- Object
- Warframe::Models::ObjectifiedHash
- Defined in:
- lib/warframe/models/objectified_hash.rb
Overview
An objectified hash response. Used for turning raw JSON objects into a ruby friendly object. This can be accessed in multiple formats, ie: JSON ‘obj` access, Ruby hash `obj`, Ruby object `obj.method`. This also handles serialization of nested hashes or arrays of hashes, which in turn will become a Waframe::Models::ObjectifiedHash.
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
-
#initialize(data) ⇒ ObjectifiedHash
constructor
A new instance of ObjectifiedHash.
-
#inspect ⇒ String
Formatted string with the class name, object id and original hash.
- #size ⇒ Object
-
#to_hash ⇒ Object
(also: #to_h, #hash)
rubocop:todo Metrics/CyclomaticComplexity.
- #to_s ⇒ Object
Constructor Details
#initialize(data) ⇒ ObjectifiedHash
Returns a new instance of ObjectifiedHash.
10 11 12 13 |
# File 'lib/warframe/models/objectified_hash.rb', line 10 def initialize(data) @data = transform_to_hash(data) populate_object end |
Class Method Details
.from_array(arr) ⇒ Object
33 34 35 |
# File 'lib/warframe/models/objectified_hash.rb', line 33 def self.from_array(arr) arr.map { |data| new(data) if data.is_a?(Hash) }.compact end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 |
# File 'lib/warframe/models/objectified_hash.rb', line 15 def [](key) @data[key] || @data[string_to_symbol(key)] end |
#inspect ⇒ String
Returns Formatted string with the class name, object id and original hash.
38 39 40 |
# File 'lib/warframe/models/objectified_hash.rb', line 38 def inspect "#<#{self.class}:#{object_id} { data: #{data.inspect} }".gsub('\"', '') end |
#size ⇒ Object
42 43 44 |
# File 'lib/warframe/models/objectified_hash.rb', line 42 def size @data.size end |
#to_hash ⇒ Object Also known as: to_h, hash
rubocop:todo Metrics/CyclomaticComplexity
19 20 21 22 23 24 25 |
# File 'lib/warframe/models/objectified_hash.rb', line 19 def to_hash # rubocop:todo Metrics/CyclomaticComplexity @to_hash ||= @data.each_with_object({}) do |(k, v), hash| v = v.to_h if v.is_a?(Warframe::Models::ObjectifiedHash) && !v.empty? v = v.map(&:to_h) if v.is_a?(Array) && v.first.is_a?(Warframe::Models::ObjectifiedHash) hash[k] = v end end |
#to_s ⇒ Object
29 30 31 |
# File 'lib/warframe/models/objectified_hash.rb', line 29 def to_s @to_s ||= hash.to_s end |