Class: Graphiti::Util::Hash Private
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.
- .include_removed?(new, old) ⇒ Boolean private
-
.keys(hash, collection = []) ⇒ Array<Symbol, String>
private
Grab all keys at any level of the hash.
- .split_json(string) ⇒ Object private
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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/graphiti/util/hash.rb', line 52 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 = if value.is_a?(Hash) deep_dup(value) elsif value.is_a?(Array) value.map { |element| element.is_a?(Hash) ? deep_dup(element) : element } elsif value&.respond_to?(:dup) && ![Symbol, Integer].include?(value.class) value.dup else value end 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
45 46 47 48 |
# File 'lib/graphiti/util/hash.rb', line 45 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 |
.include_removed?(new, old) ⇒ Boolean
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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/graphiti/util/hash.rb', line 26 def self.include_removed?(new, old) new = JSONAPI::IncludeDirective.new(new).to_hash old = JSONAPI::IncludeDirective.new(old).to_hash old.each_pair do |k, v| if new[k] if include_removed?(new[k], v) return true end else return true end end false 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/graphiti/util/hash.rb', line 17 def self.keys(hash, collection = []) hash.each_pair do |key, value| collection << key keys(value, collection) end collection end |
.split_json(string) ⇒ 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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/graphiti/util/hash.rb', line 73 def self.split_json(string) start, opens, closes, index = 0, 0, 0, -1 [].tap do |jsons| string[0..string.length].each_char do |char| index += 1 opens += 1 if char == "{" if char == "}" closes += 1 if opens == closes jsons << string.slice(start..index) start = index + 2 end end end end end |