Sha256: 8b3b52f1a9498071fd78891c77fe3ff9aaac233104e08edea8f0c662986f036c

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 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

      # 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

2 entries across 2 versions & 2 rubygems

Version Path
graphiti-1.0.alpha.1 lib/graphiti/util/hash.rb
graphiti-rb-1.0.alpha.1 lib/graphiti/util/hash.rb