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

Merge only common keys

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
                            # }

Parameters:

  • other (::Hash)

    other hash to be merged

Returns:

  • (::Hash)

    the merged hash



22
23
24
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 22

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

#exclusive_merge!(other) ⇒ ::Hash

Merge only common keys

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) # changes hash to {
                            #   a: 1,
                            #   b: 4,
                            #   c: 3
                            # }

Parameters:

  • other (::Hash)

    other hash to be merged

Returns:

  • (::Hash)

    the merged hash



41
42
43
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 41

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

#map_to_hash {|key, value| ... } ⇒ ::Hash

Map returning a hash keeping the original keys

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:

  • (::Hash)

    new Hash made with the pairs key => mapped_value

See Also:

  • ToHashMapper


70
71
72
73
74
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 70

def map_to_hash
  map do |key, value|
    [key, yield(key, value)]
  end.to_h
end

#squash(joiner = '.') ⇒ ::Hash

Squash the hash returning a single level hash

The squashing happens by merging the keys of outter and inner hashes

This operation is the oposite of #to_deep_hash

Examples:

Simple Usage

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: %w[John Wick],
    age: 22
  }, {
    name: %w[John Constantine],
    age: 25
  }]
}
person = person_data.to_deep_hash

person.squash # returns {
              #   'person' => [{
              #     'name' => %w[John Wick],
              #     'age' => 22
              #   }, {
              #     'name' => %w[John Constantine],
              #     'age' => 25
              #   }]
              # }

Giving a custom joiner

hash = {
  links: {
    home: '/',
    products: '/products'
  }
}

hash.squash('> ')  # returns {
                   #   'links> home' => '/',
                   #   'links> products' => '/products'
                   # }

Parameters:

  • joiner (::String) (defaults to: '.')

    String to be used when joining keys

Returns:

See Also:



133
134
135
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 133

def squash(joiner = '.')
  Hash::Squasher.new(joiner).squash(deep_dup)
end

#squash!(joiner = '.') ⇒ ::Hash

Squash the hash so that it becomes a single level hash

The squashing happens by merging the keys of outter and inner hashes

This operation is the oposite of #to_deep_hash!

Examples:

Simple Usage

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: %w[John Wick],
    age: 22
  }, {
    name: %w[John Constantine],
    age: 25
  }]
}
person = person_data.to_deep_hash

person.squash # returns {
              #   'person' => [{
              #     'name' => %w[John Wick],
              #     'age' => 22
              #   }, {
              #     'name' => %w[John Constantine],
              #     'age' => 25
              #   }]
              # }

Giving a custom joiner

hash = {
  links: {
    home: '/',
    products: '/products'
  }
}

hash.squash('> ')  # returns {
                   #   'links> home' => '/',
                   #   'links> products' => '/products'
                   # }

Parameters:

  • joiner (::String) (defaults to: '.')

    String to be used when joining keys

Returns:

See Also:



153
154
155
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 153

def squash!(joiner = '.')
  Hash::Squasher.new(joiner).squash(self)
end

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

Creates a new hash of multiple levels

this operation is the oposite from #squash

Examples:

With custom separator

hash = {
  'person[0]_name_first' => 'John',
  'person[0]_name_last'  => 'Doe',
  'person[1]_name_first' => 'John',
  'person[1]_name_last'  => 'Wick'
}

hash.to_deep_hash('_') # return {
                       #   'person' => [{
                       #     'name' => {
                       #       'first' => 'John',
                       #       'last'  => 'Doe'
                       #   }, {
                       #     'name' => {
                       #       'first' => 'John',
                       #       'last'  => 'Wick'
                       #     }
                       #   }]
                       # }

Reverting the result of a squash

person = {
  person: [{
    name: ['John', 'Wick'],
    age:  23
  }, {
    name: %w[John Constantine],
    age:  25
  }]
}
person_data = person.squash

person_data.to_deep_hash
# returns {
#   'person' => [{
#     'name' => ['John', 'Wick'],
#     'age'  => 23
#   }, {
#     'name' => %w[John Constantine],
#     'age'  => 25
#   }]
# }

Returns:

  • (::Hash)

    A multi-level hash

See Also:



209
210
211
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 209

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

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

Changes hash to be a multiple level hash

this operation is the oposite from #squash!

Examples:

With custom separator

hash = {
  'person[0]_name_first' => 'John',
  'person[0]_name_last'  => 'Doe',
  'person[1]_name_first' => 'John',
  'person[1]_name_last'  => 'Wick'
}

hash.to_deep_hash('_') # return {
                       #   'person' => [{
                       #     'name' => {
                       #       'first' => 'John',
                       #       'last'  => 'Doe'
                       #   }, {
                       #     'name' => {
                       #       'first' => 'John',
                       #       'last'  => 'Wick'
                       #     }
                       #   }]
                       # }

Reverting the result of a squash

person = {
  person: [{
    name: ['John', 'Wick'],
    age:  23
  }, {
    name: %w[John Constantine],
    age:  25
  }]
}
person_data = person.squash

person_data.to_deep_hash
# returns {
#   'person' => [{
#     'name' => ['John', 'Wick'],
#     'age'  => 23
#   }, {
#     'name' => %w[John Constantine],
#     'age'  => 25
#   }]
# }

Returns:

  • (::Hash)

    Self changed to be a multi-level hash

See Also:



223
224
225
# File 'lib/darthjee/core_ext/hash/transformable.rb', line 223

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