Class: JsonapiCompliable::Util::Hash Private
- Inherits:
-
Object
- Object
- JsonapiCompliable::Util::Hash
- 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
-
.deep_dup(hash) ⇒ Object
private
Like ActiveSupport's #deep_dup.
-
.deep_merge!(hash, other) ⇒ Hash
private
Like ActiveSupport's #deep_merge.
-
.keys(hash, collection = []) ⇒ Array<Symbol, String>
private
Grab all keys at any level of the hash.
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
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
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 |