Sha256: 006125fbecc6185d315b2f41ef1c8d2e70d088fa42c5b1e9f4afeeb8a84e2d2a

Contents?: true

Size: 857 Bytes

Versions: 7

Compression:

Stored size: 857 Bytes

Contents

# frozen_string_literal: true

require "dry/core/cache"
require "dry/equalizer"

module Dry
  module Schema
    # Coerces keys in a hash using provided coercer function
    #
    # @api private
    class KeyCoercer
      extend Dry::Core::Cache
      include ::Dry::Equalizer(:key_map, :coercer)

      TO_SYM = :to_sym.to_proc.freeze

      attr_reader :key_map, :coercer

      # @api private
      def self.new(*args, &coercer)
        fetch_or_store(*args, coercer) { super(*args, &coercer) }
      end

      # @api private
      def self.symbolized(*args)
        new(*args, &TO_SYM)
      end

      # @api private
      def initialize(key_map, &coercer)
        @key_map = key_map.coercible(&coercer)
      end

      # @api private
      def call(result)
        key_map.write(result.to_h)
      end
      alias_method :[], :call
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-schema-1.5.6 lib/dry/schema/key_coercer.rb
dry-schema-1.5.5 lib/dry/schema/key_coercer.rb
dry-schema-1.5.4 lib/dry/schema/key_coercer.rb
dry-schema-1.5.3 lib/dry/schema/key_coercer.rb
dry-schema-1.5.2 lib/dry/schema/key_coercer.rb
dry-schema-1.5.1 lib/dry/schema/key_coercer.rb
dry-schema-1.5.0 lib/dry/schema/key_coercer.rb