Sha256: a194f0af027c18a9fd8234289f6928332660ee20e15a9e3399ff16a75178f095

Contents?: true

Size: 858 Bytes

Versions: 5

Compression:

Stored size: 858 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(source)
        key_map.write(Hash(source))
      end
      alias_method :[], :call
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-schema-0.6.0 lib/dry/schema/key_coercer.rb
dry-schema-0.5.1 lib/dry/schema/key_coercer.rb
dry-schema-0.5.0 lib/dry/schema/key_coercer.rb
dry-schema-0.4.0 lib/dry/schema/key_coercer.rb
dry-schema-0.3.0 lib/dry/schema/key_coercer.rb