Class: Darthjee::CoreExt::Hash::Squasher::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/darthjee/core_ext/hash/squasher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ Builder

Returns a new instance of Builder.



10
11
12
13
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 10

def initialize(key, value)
  @value = value
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



8
9
10
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 8

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 8

def value
  @value
end

Instance Method Details

#to_hObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 15

def to_h
  if value.is_a? Hash
    value.squash.inject({}) do |hash, (k, v)|
      new_key = [key, k].join('.')
      hash.merge!(new_key => v)
    end
  else
    { key => value }
  end
end