Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/cliutils/ext/hash_extensions.rb

Overview

Hash Class Contains many convenient methods borrowed from Rails api.rubyonrails.org/classes/Hash.html

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) { ... } ⇒ Object

Allows for dot-notation getting/setting within a Hash. ceronio.net/2012/01/javascript-style-object-value-assignment-in-ruby/

Parameters:

  • meth (<String, Symbol>)

    The method name

  • args (Array)

    Any passed arguments

Yields:

  • if a block is passed



76
77
78
79
80
81
82
83
# File 'lib/cliutils/ext/hash_extensions.rb', line 76

def method_missing(meth, *args, &block)
  meth_name = meth.to_s
  if meth_name[-1,1] == '='
    self[meth_name[0..-2].to_sym] = args[0]
  else
    self[meth] || self[meth_name]
  end
end

Instance Method Details

#deep_merge(other_hash) ⇒ Hash

Deep merges a hash into the current one. Returns a new copy of the hash.

Parameters:

  • other_hash (Hash)

    The hash to merge in

Returns:

  • (Hash)

    The original Hash



9
10
11
12
13
14
15
16
# File 'lib/cliutils/ext/hash_extensions.rb', line 9

def deep_merge(other_hash)
  self.merge(other_hash) do |key, oldval, newval|
    oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
    newval = newval.to_hash if newval.respond_to?(:to_hash)
    oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ?
      oldval.deep_merge(newval) : newval
  end
end

#deep_merge!(other_hash) ⇒ Hash

Deep merges a hash into the current one. Does the replacement inline.

Parameters:

  • other_hash (Hash)

    The hash to merge in

Returns:

  • (Hash)

    The original Hash



22
23
24
# File 'lib/cliutils/ext/hash_extensions.rb', line 22

def deep_merge!(other_hash)
  replace(deep_merge(other_hash))
end

#deep_stringify_keysHash

Recursively turns all Hash keys into strings and returns the new Hash.

Returns:

  • (Hash)

    A new copy of the original Hash



29
30
31
# File 'lib/cliutils/ext/hash_extensions.rb', line 29

def deep_stringify_keys
  deep_transform_keys { |key| key.to_s }
end

#deep_stringify_keys!Hash

Same as deep_stringify_keys, but destructively alters the original Hash.

Returns:

  • (Hash)

    The original Hash



36
37
38
# File 'lib/cliutils/ext/hash_extensions.rb', line 36

def deep_stringify_keys!
  deep_transform_keys! { |key| key.to_s }
end

#deep_symbolize_keysHash

Recursively turns all Hash keys into symbols and returns the new Hash.

Returns:

  • (Hash)

    A new copy of the original Hash



43
44
45
# File 'lib/cliutils/ext/hash_extensions.rb', line 43

def deep_symbolize_keys
  deep_transform_keys { |key| key.to_sym rescue key }
end

#deep_symbolize_keys!Hash

Same as deep_symbolize_keys, but destructively alters the original Hash.

Returns:

  • (Hash)

    The original Hash



50
51
52
# File 'lib/cliutils/ext/hash_extensions.rb', line 50

def deep_symbolize_keys!
  deep_transform_keys! { |key| key.to_sym rescue key }
end

#deep_transform_keys { ... } ⇒ Hash

Generic method to perform recursive operations on a Hash.

Yields:

  • &block

Returns:

  • (Hash)

    A new copy of the original Hash



58
59
60
# File 'lib/cliutils/ext/hash_extensions.rb', line 58

def deep_transform_keys(&block)
  _deep_transform_keys_in_object(self, &block)
end

#deep_transform_keys! { ... } ⇒ Hash

Same as deep_transform_keys, but destructively alters the original Hash.

Yields:

  • &block

Returns:

  • (Hash)

    The original Hash



66
67
68
# File 'lib/cliutils/ext/hash_extensions.rb', line 66

def deep_transform_keys!(&block)
  _deep_transform_keys_in_object!(self, &block)
end

#recursive_find_by_key(key) { ... } ⇒ Multiple

Recursively searches a hash for the passed key and returns the value (if there is one). stackoverflow.com/a/2239847/327179

Parameters:

  • key (<Symbol, String>)

    The key to search for

Yields:

Returns:

  • (Multiple)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cliutils/ext/hash_extensions.rb', line 91

def recursive_find_by_key(key)
  # Create a stack of hashes to search through for the needle which
  # is initially this hash
  stack = [ self ]

  # So long as there are more haystacks to search...
  while (to_search = stack.pop)
    # ...keep searching for this particular key...
    to_search.each do |k, v|
      # ...and return the corresponding value if it is found.
      return v if (k == key)

      # If this value can be recursively searched...
      if (v.respond_to?(:recursive_find_by_key))
        # ...push that on to the list of places to search.
        stack << v
      end
    end
  end
end