Class: ESM::Arma::HashMap

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/esm/model/arma/hash_map.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ HashMap

Returns a new instance of HashMap.



16
17
18
19
20
21
# File 'lib/esm/model/arma/hash_map.rb', line 16

def initialize(data = {})
  super
  return if data.blank?

  from(data)
end

Class Method Details

.from(input) ⇒ Object

Parameters:

  • input (String, Array, Hash, OpenStruct)

    The data to be converted. If a String, data must be array pairs



7
8
9
10
11
12
13
14
# File 'lib/esm/model/arma/hash_map.rb', line 7

def self.from(input)
  hash_map = new
  return hash_map if input.blank?

  hash_map.from(input)
rescue
  nil
end

Instance Method Details

#from(input) ⇒ Object



23
24
25
26
27
28
# File 'lib/esm/model/arma/hash_map.rb', line 23

def from(input)
  hash = normalize(input)
  merge!(hash)

  self
end

#to_aObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/esm/model/arma/hash_map.rb', line 30

def to_a
  convert_value =
    lambda do |value|
      case value
      when Array
        value.map { |v| convert_value.call(v) }
      when Hash, ActiveSupport::HashWithIndifferentAccess
        value.map do |key, value|
          [convert_value.call(key), convert_value.call(value)]
        end
      when Symbol
        value.to_s
      else
        value
      end
    end

  map { |key, value| [key, convert_value.call(value)] }
end

#to_jsonObject Also known as: to_s



50
51
52
# File 'lib/esm/model/arma/hash_map.rb', line 50

def to_json(*)
  ::JSON.generate(to_a, *)
end