Sha256: 6e0755eaf298ad5806ab71c702422248d95616b562b7049befa7efffbee7a505

Contents?: true

Size: 862 Bytes

Versions: 6

Compression:

Stored size: 862 Bytes

Contents

# frozen_string_literal: true

require "dry/core/cache"
require "dry/core/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

6 entries across 6 versions & 1 rubygems

Version Path
dry-schema-1.8.0 lib/dry/schema/key_coercer.rb
dry-schema-1.7.1 lib/dry/schema/key_coercer.rb
dry-schema-1.7.0 lib/dry/schema/key_coercer.rb
dry-schema-1.6.2 lib/dry/schema/key_coercer.rb
dry-schema-1.6.1 lib/dry/schema/key_coercer.rb
dry-schema-1.6.0 lib/dry/schema/key_coercer.rb