Class: Graphiti::Util::Hash Private

Inherits:
Object show all
Defined in:
lib/graphiti/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



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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 = deep_dup(value) if value.is_a?(Hash)
        value = value.dup if value&.respond_to?(:dup) && ![Symbol, Integer].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



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.

Returns:

  • (Boolean)


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

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/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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/graphiti/util/hash.rb', line 66

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