Sha256: 95e02a8e833d4754cc174cee23df804afc9e1e90aacb6965d4124d4c10ee1d41

Contents?: true

Size: 1.51 KB

Versions: 15

Compression:

Stored size: 1.51 KB

Contents

require 'pact_broker/string_refinements'

module PactBroker
  module HashRefinements

    refine Hash do
      using PactBroker::StringRefinements

      def deep_merge(other_hash, &block)
        block_actual = Proc.new {|key, oldval, newval|
            newval = block.call(key, oldval, newval) if block_given?
            [oldval, newval].all? {|v| v.is_a?(Hash)} ? oldval.merge(newval, &block_actual) : newval
        }
        merge(other_hash, &block_actual)
      end

      def symbolize_keys
        symbolize_keys_private(self)
      end

      def snakecase_keys
        snakecase_keys_private(self)
      end

      private

      def snakecase_keys_private(params)
        case params
        when Hash
          params.inject({}) do |result, (key, value)|
            snake_key = case key
            when String then key.snakecase
            when Symbol then key.to_s.snakecase.to_sym
            else
              key
            end
            result.merge(snake_key => snakecase_keys_private(value))
          end
        when Array
          params.collect { |value| snakecase_keys_private(value) }
        else
          params
        end

      end

      def symbolize_keys_private(params)
        case params
        when Hash
          params.inject({}) do |result, (key, value)|
            result.merge(key.to_sym => symbolize_keys_private(value))
          end
        when Array
          params.collect { |value| symbolize_keys_private(value) }
        else
          params
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pact_broker-2.52.1 lib/pact_broker/hash_refinements.rb
pact_broker-2.52.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.51.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.50.1 lib/pact_broker/hash_refinements.rb
pact_broker-2.50.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.49.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.48.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.47.1 lib/pact_broker/hash_refinements.rb
pact_broker-2.47.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.46.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.45.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.44.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.43.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.42.0 lib/pact_broker/hash_refinements.rb
pact_broker-2.41.0 lib/pact_broker/hash_refinements.rb