Class: JsonapiCompliable::Util::Hash Private

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/util/hash.rb

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 Method Summary collapse

Class Method Details

.deep_dup(hash) ⇒ 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.

Like ActiveSupport's #deep_dup



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jsonapi_compliable/util/hash.rb', line 36

def self.deep_dup(hash)
  if hash.respond_to?(:deep_dup)
    hash.deep_dup
  else
    {}.tap do |duped|
      hash.each_pair do |key, value|
        value = deep_dup(value) if value.is_a?(Hash)
        value = value.dup if value && value.respond_to?(:dup) && ![Symbol, Fixnum].include?(value.class)
        duped[key] = value
      end
    end
  end
end

.deep_merge!(hash, other) ⇒ 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.

Like ActiveSupport's #deep_merge

Returns:

  • (Hash)

    the merged hash



29
30
31
32
# File 'lib/jsonapi_compliable/util/hash.rb', line 29

def self.deep_merge!(hash, other)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  hash.merge!(other, &merger)
end

.keys(hash, collection = []) ⇒ Array<Symbol, String>

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.

Grab all keys at any level of the hash.

{ foo: { bar: { baz: {} } } }

Becomes

:foo, :bar, :bar

Parameters:

  • hash

    the hash we want to process

  • collection (Array<Symbol, String>) (defaults to: [])

    the memoized collection of keys

Returns:

  • (Array<Symbol, String>)

    the keys



17
18
19
20
21
22
23
24
# File 'lib/jsonapi_compliable/util/hash.rb', line 17

def self.keys(hash, collection = [])
  hash.each_pair do |key, value|
    collection << key
    keys(value, collection)
  end

  collection
end