Sha256: de2232a2e26cf2ffe2d07ecb0f337e88912682ab24fee4bad50092c136d4f879

Contents?: true

Size: 1.75 KB

Versions: 25

Compression:

Stored size: 1.75 KB

Contents

module Graphiti
  module Util
    # @api private
    class Hash
      # 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
      def self.keys(hash, collection = [])
        hash.each_pair do |key, value|
          collection << key
          keys(value, collection)
        end

        collection
      end

      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

      # Like ActiveSupport's #deep_merge
      # @return [Hash] the merged hash
      # @api private
      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

      # Like ActiveSupport's #deep_dup
      # @api private
      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
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
graphiti-1.0.beta.4 lib/graphiti/util/hash.rb
graphiti-1.0.beta.3 lib/graphiti/util/hash.rb
graphiti-1.0.beta.2 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.26 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.25 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.24 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.23 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.22 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.21 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.20 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.19 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.18 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.17 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.16 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.15 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.14 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.12 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.11 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.10 lib/graphiti/util/hash.rb
graphiti-1.0.alpha.9 lib/graphiti/util/hash.rb