Class: Darthjee::CoreExt::Hash::Squasher Private

Inherits:
Object
  • Object
show all
Defined in:
lib/darthjee/core_ext/hash/squasher.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 squashing a 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'
                   # }

Simple usage

hash = {
  person: [{
    name: %w[John Wick],
    age: 22
  }, {
    name: %w[John Constantine],
    age: 25
  }]
}

squasher = Darthjee::CoreExt::Hash::Squasher.new

squasher.squash(hash) # changes hash to {
                      #   'person[0].name[0]' => 'John',
                      #   'person[0].name[1]' => 'Wick',
                      #   'person[0].age'  => 22,
                      #   'person[1].name[0]' => 'John',
                      #   'person[1].name[1]' => 'Constantine',
                      #   'person[1].age'  => 25
                      # }

Custom joiner

hash = {
  person: {
    name: 'John',
    age: 22
  }
}

squasher = Darthjee::CoreExt::Hash::Squasher.new('> ')

squasher.squash(hash) # changes hash to {
                      #   'person> name' => 'John',
                      #   'person> age'  => 22
                      # }

See Also:

Author:

  • Darthjee

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(joiner = '.') ⇒ Squasher

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

Parameters:

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

    string used to join keys



21
22
23
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 21

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

Instance Attribute Details

#joinerObject (readonly)

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.



18
19
20
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 18

def joiner
  @joiner
end

Instance Method Details

#squash(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.

Squash a hash creating a new hash

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

Examples:

Simple usage

hash = {
  person: [{
    name: %w[John Wick],
    age: 22
  }, {
    name: %w[John Constantine],
    age: 25
  }]
}

squasher = Darthjee::CoreExt::Hash::Squasher.new

squasher.squash(hash) # changes hash to {
                      #   'person[0].name[0]' => 'John',
                      #   'person[0].name[1]' => 'Wick',
                      #   'person[0].age'  => 22,
                      #   'person[1].name[0]' => 'John',
                      #   'person[1].name[1]' => 'Constantine',
                      #   'person[1].age'  => 25
                      # }

Custom joiner

hash = {
  person: {
    name: 'John',
    age: 22
  }
}

squasher = Darthjee::CoreExt::Hash::Squasher.new('> ')

squasher.squash(hash) # changes hash to {
                      #   'person> name' => 'John',
                      #   'person> age'  => 22
                      # }

Parameters:

  • hash (::Hash)

    hash to be squashed

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/darthjee/core_ext/hash/squasher.rb', line 70

def squash(hash)
  hash.keys.each do |key|
    next unless hash[key].is_any?(Hash, Array)

    value = hash.delete(key)
    add_value_to_hash(hash, key, value)
  end
  hash
end