Class: Darthjee::CoreExt::Hash::DeepHashConstructor Private

Inherits:
Object
  • Object
show all
Defined in:
lib/darthjee/core_ext/hash/deep_hash_constructor.rb,
lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class responsible for creating a Hash deep hash

Deep hash construction happens when a hash of one layer (no sub hashes) has keys that, once explitted, can be assembled in a hash with many layers

Examples:

General Usage

hash = {
  'account.person.name[0]' => 'John',
  'account.person.name[1]' => 'Wick',
  'account.person.age'     =>  20,
  'account.number'         => '102030',
  :'house.number'          => 67,
  :'house.zip'             => 12_345
}

constructor = Darthjee::CoreExt::Hash::DeepHashConstructor.new('.')

constructor.deep_hash(hash)  # returns {
                             #   'account' => {
                             #     'person' => {
                             #       'name'   => ['John', 'Wick'],
                             #       'age'    =>  20
                             #     },
                             #     'number' => '102030',
                             #   },
                             #   'house' => {
                             #     'number' => 67,
                             #     'zip'    => 12345
                             #   }
                             # }

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

See Also:

Author:

  • Darthjee

Defined Under Namespace

Classes: Setter

Instance Method Summary collapse

Constructor Details

#initialize(separator = '.') ⇒ DeepHashConstructor

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DeepHashConstructor.

Parameters:

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

    keys splitter



49
50
51
# File 'lib/darthjee/core_ext/hash/deep_hash_constructor.rb', line 49

def initialize(separator = '.')
  @separator = separator
end

Instance Method Details

#deep_hash(hash) ⇒ ::Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs deep hash transformation

Examples:

General Usage

hash = {
  'account.person.name[0]' => 'John',
  'account.person.name[1]' => 'Wick',
  'account.person.age'     =>  20,
  'account.number'         => '102030',
  :'house.number'          => 67,
  :'house.zip'             => 12_345
}

constructor = Darthjee::CoreExt::Hash::DeepHashConstructor.new('.')

constructor.deep_hash(hash)  # returns {
                             #   'account' => {
                             #     'person' => {
                             #       'name'   => ['John', 'Wick'],
                             #       'age'    =>  20
                             #     },
                             #     'number' => '102030',
                             #   },
                             #   'house' => {
                             #     'number' => 67,
                             #     'zip'    => 12345
                             #   }
                             # }

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

Parameters:

  • hash (::Hash)

    On layered hash

Returns:

  • (::Hash)

    Many layered hash



60
61
62
63
64
65
66
# File 'lib/darthjee/core_ext/hash/deep_hash_constructor.rb', line 60

def deep_hash(hash)
  break_keys(hash).tap do
    hash.each do |key, value|
      hash[key] = deep_hash_value(value)
    end
  end
end