Class: SleepingKingStudios::Tools::HashTools

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/hash_tools.rb

Overview

Tools for working with hash-like enumerable objects.

Constant Summary collapse

HASH_METHODS =

Expected methods that a Hash-like object should implement.

i[[] count each each_key each_pair].freeze

Instance Method Summary collapse

Methods inherited from Base

instance

Instance Method Details

#convert_keys_to_strings(hsh) ⇒ Hash Also known as: stringify_keys

Returns a deep copy of the hash with the keys converted to strings.

Examples:

hsh = { :one => 1, :two => 2, :three => 3 }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { 'one' => 1, 'two' => 2, 'three' => 3 }
hsh
#=> { :one => 1, :two => 2, :three => 3 }

hsh = { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }
hsh
#=> { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }

Parameters:

  • hsh (Hash)

    the hash to convert.

Returns:

  • (Hash)

    the converted copy of the hash.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.



45
46
47
48
49
50
51
52
53
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 45

def convert_keys_to_strings(hsh)
  require_hash!(hsh)

  hsh.each.with_object({}) do |(key, value), cpy|
    sym = key.to_s

    cpy[sym] = convert_value_to_stringified_hash(value)
  end
end

#convert_keys_to_symbols(hsh) ⇒ Hash Also known as: symbolize_keys

Returns a deep copy of the hash with the keys converted to symbols.

Examples:

hsh = { 'one' => 1, 'two' => 2, 'three' => 3 }
cpy = HashTools.convert_keys_to_symbols(hsh)
#=> { :one => 1, :two => 2, :three => 3 }
hsh
#=> { 'one' => 1, 'two' => 2, 'three' => 3 }

hsh = { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }
cpy = HashTools.convert_keys_to_strings(hsh)
#=> { :odd => { :one => 1, :three => 3 }, :even => { :two => 2, :four => 4 } }
hsh
#=> { 'odd' => { 'one' => 1, 'three' => 3 }, 'even' => { 'two' => 2, 'four' => 4 } }

Parameters:

  • hsh (Hash)

    the hash to convert.

Returns:

  • (Hash)

    the converted copy of the hash.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.



76
77
78
79
80
81
82
83
84
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 76

def convert_keys_to_symbols(hsh)
  require_hash!(hsh)

  hsh.each.with_object({}) do |(key, value), cpy|
    sym = key.to_s.intern

    cpy[sym] = convert_value_to_symbolic_hash(value)
  end
end

#deep_dup(hsh) ⇒ Hash

Creates a deep copy of the Hash.

Examples:

hsh = { :one => 'one', :two => 'two', :three => 'three' }
cpy = HashTools.deep_dup hsh

cpy.update :four => 'four'
#=> { :one => 'one', :two => 'two', :three => 'three', :four => 'four' }
hsh
#=> { :one => 'one', :two => 'two', :three => 'three' }

cpy[:one].sub!(/on/, 'vu'); cpy
#=> { :one => 'vun', :two => 'two', :three => 'three', :four => 'four' }
hsh
#=> { :one => 'one', :two => 'two', :three => 'three' }

Parameters:

  • hsh (Hash<Object>)

    the hash to copy.

Returns:

  • (Hash)

    the copy of the hash.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.



108
109
110
111
112
113
114
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 108

def deep_dup(hsh)
  require_hash!(hsh)

  hsh.each.with_object({}) do |(key, value), copy|
    copy[ObjectTools.deep_dup key] = ObjectTools.deep_dup(value)
  end
end

#deep_freeze(hsh) ⇒ Hash

Freezes the hash and performs a deep freeze on each hash key and value.

Examples:

hsh = { :one => 'one', :two => 'two', :three => 'three' }
HashTools.deep_freeze hsh

hsh.frozen?
#=> true
hsh[:one].frozen?
#=> true

Parameters:

  • hsh (Hash)

    the hash to freeze.

Returns:

  • (Hash)

    the frozen hash.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.



132
133
134
135
136
137
138
139
140
141
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 132

def deep_freeze(hsh)
  require_hash!(hsh)

  hsh.freeze

  hsh.each do |key, value|
    ObjectTools.deep_freeze key
    ObjectTools.deep_freeze value
  end
end

#generate_binding(hsh) ⇒ Binding

Generates a Binding with the hash values as local variables.

Examples:

hsh     = { :one => 'one', :two => 'two', :three => 'three' }
binding = HashTools.generate_binding(hsh)
#=> Binding

binding.local_variable_defined?(:one)
#=> true
binding.local_variable_get(:one)
#=> 'one'
binding.eval('one')
#=> 'one'

Returns:

  • (Binding)

    the binding object.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.



160
161
162
163
164
165
166
167
168
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 160

def generate_binding(hsh)
  require_hash!(hsh)

  CoreTools.empty_binding.tap do |binding|
    hsh.each do |key, value|
      binding.local_variable_set key, value
    end
  end
end

#hash?(obj) ⇒ Boolean

Returns true if the object is or appears to be a Hash.

This method checks for the method signatures of the object. A Hash-like method will define all of the the #[], #count, #each, #each_key, and #each_value methods.

Examples:

HashTools.hash?(nil)
#=> false
HashTools.hash?([])
#=> false
HashTools.hash?({})
#=> true

Parameters:

  • obj (Object)

    the object to test.

Returns:

  • (Boolean)

    true if the object is a Hash, otherwise false.



187
188
189
190
191
192
193
194
195
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 187

def hash?(obj)
  return true if obj.is_a?(Hash)

  HASH_METHODS.each do |method_name|
    return false unless obj.respond_to?(method_name)
  end

  true
end

#immutable?(hsh) ⇒ Boolean

Returns true if the hash is immutable.

A hash is considered immutable if the hash itself is frozen and each key and value in the hash is immutable.

Examples:

HashTools.immutable?({ :id => 0, :title => 'The Ramayana' })
#=> false

HashTools.immutable?({ :id => 0, :title => +'The Ramayana' }.freeze)
#=> false

HashTools.immutable?({ :id => 0, :title => 'The Ramayana' }.freeze)
#=> true

Parameters:

  • hsh (Hash)

    the hash to test.

Returns:

  • (Boolean)

    true if the hash is immutable, otherwise false.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.

See Also:



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 221

def immutable?(hsh)
  require_hash!(hsh)

  return false unless hsh.frozen?

  hsh.each do |key, value|
    unless ObjectTools.immutable?(key) && ObjectTools.immutable?(value)
      return false
    end
  end

  true
end

#mutable?(hsh) ⇒ Boolean

Returns true if the hash or any of its contents are mutable.

Parameters:

  • hsh (Hash)

    the hash to test.

Returns:

  • (Boolean)

    true if the hash is mutable, otherwise false.

Raises:

  • (ArgumentError)

    if the first argument is not an Hash-like object.

See Also:



244
245
246
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 244

def mutable?(hsh)
  !immutable?(hsh)
end