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

Inherits:
Object
  • Object
show all
Defined in:
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 setting value localized inside hash

Examples:

Simple usage

hash   = {}
base   = 'person'
setter = Darthjee::CoreExt::Hash::DeepHashConstructor::Setter.new(
  hash, base
)

setter.set('age', 21)

hash # changed to {
     #   'person' => {
     #     'age' => 21,
     #   }
     # }

Instance Method Summary collapse

Constructor Details

#initialize(hash, base_key) ⇒ Setter

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 Setter.

Parameters:

  • hash (Hash)

    hash to be changed

  • base_key (::String)

    base key of hash where subhash will be created



29
30
31
32
# File 'lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb', line 29

def initialize(hash, base_key)
  @hash     = hash
  @base_key = base_key
end

Instance Method Details

#set(key, value) ⇒ ::Object

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.

Sets a value in the correct key inside the hash

Examples:

With Array index

hash   = {}
base   = 'person[0]'
setter = Darthjee::CoreExt::Hash::DeepHashConstructor::Setter.new(
  hash, base
)

setter.set('age', 21)

hash # changed to {
     #   'person' => [{
     #     'age' => 21,
     #   }]
     # }

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:

  • key (::String, ::NilClass)

    key where value will live

  • value (::Object)

    value to be set

Returns:



57
58
59
60
61
62
# File 'lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb', line 57

def set(key, value)
  return hash[base_key] = value unless key || index
  return array[index]   = value unless key

  sub_hash[key] = value
end