Sha256: 0969567f898139f278077cb9ddada1d95d95a76a5db1a8f7e363461468d17bfa

Contents?: true

Size: 661 Bytes

Versions: 2

Compression:

Stored size: 661 Bytes

Contents

require 'dry/equalizer'
require 'dry/initializer'

module Dry
  module Schema
    # Used by the processors to coerce values in the input hash
    #
    # @api private
    class ValueCoercer
      extend Dry::Initializer
      include ::Dry::Equalizer(:type_schema)

      # @api private
      param :type_schema

      # @api private
      def call(input)
        if input.success?
          type_schema[Hash(input)]
        else
          type_schema.member_types.reduce(EMPTY_HASH.dup) do |hash, (name, type)|
            hash[name] = input.error?(name) ? input[name] : type[input[name]]
            hash
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/value_coercer.rb
dry-schema-0.1.1 lib/dry/schema/value_coercer.rb