class Graphiti::Util::Hash

@api private

Public Class Methods

deep_dup(hash) click to toggle source

Like ActiveSupport’s deep_dup @api private

# 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) click to toggle source

Like ActiveSupport’s deep_merge @return [Hash] the merged hash @api private

# 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) click to toggle source
# 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 = []) click to toggle source

Grab all keys at any level of the hash.

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

Becomes

:foo, :bar, :bar

@param hash the hash we want to process @param [Array<Symbol, String>] collection the memoized collection of keys @return [Array<Symbol, String>] the keys @api private

# 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) click to toggle source
# 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