Sha256: f03c602a9d42ef6a774d47372adefd1cdd6b4d4dafdddfce2d225147805d062f

Contents?: true

Size: 1.38 KB

Versions: 65

Compression:

Stored size: 1.38 KB

Contents

module JsonapiCompliable
  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

65 entries across 65 versions & 1 rubygems

Version Path
jsonapi_compliable-0.6.9 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.6.8 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.6.7 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.6.6 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.6.5 lib/jsonapi_compliable/util/hash.rb