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 copy of the hash with the keys converted to strings.

Parameters:

  • hsh (Hash)

    The hash to convert.

Returns:

  • (Hash)

    The converted copy of the hash.



30
31
32
33
34
35
36
37
38
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 30

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 copy of the hash with the keys converted to symbols.

Parameters:

  • hsh (Hash)

    The hash to convert.

Returns:

  • (Hash)

    The converted copy of the hash.



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

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 object by returning a new Hash with deep copies of each key and value.

Parameters:

  • hsh (Hash<Object>)

    The hash to copy.

Returns:

  • (Hash)

    The copy of the hash.



63
64
65
66
67
68
69
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 63

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) ⇒ Object

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

Parameters:

  • hsh (Hash)

    The object to freeze.



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

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 object with an Object as the receiver and the hash key-value pairs set as local variables.

Returns:

  • (Binding)

    The binding object.



90
91
92
93
94
95
96
97
98
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 90

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?(hsh) ⇒ Boolean

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

Parameters:

  • hsh (Object)

    The object to test.

Returns:

  • (Boolean)

    True if the object is a Hash, otherwise false.



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

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

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

  true
end

#immutable?(hsh) ⇒ Boolean

Returns true if the hash is immutable, i.e. if the hash is frozen and each hash key and hash value are immutable.

Parameters:

  • hsh (Hash)

    The hash to test.

Returns:

  • (Boolean)

    True if the hash is immutable, otherwise false.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 121

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 is mutable.

Parameters:

  • hsh (Array)

    The hash to test.

Returns:

  • (Boolean)

    True if the hash is mutable, otherwise false.

See Also:



142
143
144
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 142

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