Module: Darthjee::CoreExt::Hash::Transformable

Included in:
Darthjee::CoreExt::Hash
Defined in:
lib/darthjee/core_ext/hash/transformable.rb

Instance Method Summary collapse

Instance Method Details

#exclusive_merge(other) ⇒ ::Hash

Merges both hashes not adding keys that don’t exist in the original hash

Examples:

merging of hashes with some clashing keys

hash = { a: 1, b: 2, c: 3 }
other = { b: 4, 'c' => 5, e: 6 }

hash.exclusive_merge(other) # returns { a: 1, b: 4, c: 3 }
hash                        # returns { a: 1, b: 2, c: 3 }

Parameters:

  • other (::Hash)

    other hash to be merged

Returns:

  • (::Hash)

    the merged hash



20
21
22
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 20

def exclusive_merge(other)
  dup.exclusive_merge!(other)
end

#exclusive_merge!(other) ⇒ ::Hash

Merges both hashes not adding keys that don’t exist in the original hash

Examples:

merging of hashes with some clashing keys

hash = { a: 1, b: 2, c: 3 }
other = { b: 4, 'c' => 5, e: 6 }

hash.exclusive_merge!(other) # returns { a: 1, b: 4, c: 3 }
hash                         # returns { a: 1, b: 4, c: 3 }

Parameters:

  • other (::Hash)

    other hash to be merged

Returns:

  • (::Hash)

    the merged hash



37
38
39
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 37

def exclusive_merge!(other)
  merge!(other.slice(*keys))
end

#map_to_hash {|key, value| ... } ⇒ Object

Run map block where each pair key, value is mapped to a new value to be assigned in the same key on the returned hash

Examples:

mapping to size of the original words

hash = { a: 'word', b: 'bigword', c: 'c' }

new_hash = hash.map_to_hash do |key, value|
  "#{key}->#{value.size}"
end

new_hash # returns { a: 'a->4', b: 'b->7', c: 'c->1' }

Yields:

  • (key, value)

    block returning the new value

Returns:

  • new Hash made with the pairs key => mapped_value

See Also:



59
60
61
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 59

def map_to_hash(&block)
  Hash::ToHashMapper.new(self).map(&block)
end

#squash::Hash

Squash the hash so that it becomes a single level hash merging the keys of outter and inner hashes

This operation is the oposite of #to_deep_hash

Examples:

a name hash

hash = { name: { first: 'John', last: 'Doe' } }

hash.squash # returns {
            #   'name.first' => 'John',
            #   'name.last'  => 'Doe'
            # }

Reverting a #to_deep_hash call

person_data = {
  'person.name' => 'John',
  'person.age' => 23
}
person = person_data.to_deep_hash

person.squash # returns {
              #   'person.name' => 'John',
              #   'person.age' => 23
              # }

Returns:

  • (::Hash)

    A one level hash

See Also:



92
93
94
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 92

def squash
  Hash::Squasher.squash(self)
end

#to_deep_hash(separator = '.') ⇒ ::Hash

Creates a new hash of multiple levels from a one level hash

this operation is the oposite from #squash

Examples:

construction of name hash

{ 'name_first' => 'John', 'name_last' => 'Doe' }

hash.to_deep_hash # return {
                  #   'name' => {
                  #     'first' => 'John',
                  #     'last' => 'Doe'
                  #   }
                  # }

Reverting squash

person = {
  'person' => {
    'name' => 'John',
    'age' => 23
  }
}
person_data = person.squash

person_data.to_deep_hash
# returns {
#   'person' => {
#     'name' => 'John',
#     'age' => 23
#   }
# }

Returns:

  • (::Hash)

    A multi-level hash

See Also:



131
132
133
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 131

def to_deep_hash(separator = '.')
  Hash::DeepHashConstructor.new(separator).deep_hash(self)
end