Class: Warframe::Models::ObjectifiedHash

Inherits:
Object
  • Object
show all
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

Base

Class Method Summary collapse

Instance Method Summary collapse

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

#inspectString

Returns Formatted string with the class name, object id and original hash.

Returns:

  • (String)

    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

#sizeObject



42
43
44
# File 'lib/warframe/models/objectified_hash.rb', line 42

def size
  @data.size
end

#to_hashObject 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_sObject



29
30
31
# File 'lib/warframe/models/objectified_hash.rb', line 29

def to_s
  @to_s ||= hash.to_s
end