Class: JsonapiCompliable::Util::Hash

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

Class Method Summary collapse

Class Method Details

.deep_dup(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jsonapi_compliable/util/hash.rb', line 18

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



13
14
15
16
# File 'lib/jsonapi_compliable/util/hash.rb', line 13

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 = []) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/jsonapi_compliable/util/hash.rb', line 4

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

  collection
end