Sha256: 5b42e4d861137c7368b20d68842da8f077e54fbfdde146f107ba1c147fd1c93a

Contents?: true

Size: 814 Bytes

Versions: 1

Compression:

Stored size: 814 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)
        fetch_or_store(*args) { super }
      end

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-1.14.0 lib/dry/schema/key_coercer.rb