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.11.15 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.14 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.13 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.12 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.11 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.10 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.8 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.7 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.6 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.5 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.4 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.3 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.2 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.1 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.11.0 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.10.9 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.10.7 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.10.6 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.10.5 lib/jsonapi_compliable/util/hash.rb
jsonapi_compliable-0.10.4 lib/jsonapi_compliable/util/hash.rb