lib/dry/schema/processor.rb in dry-schema-1.3.4 vs lib/dry/schema/processor.rb in dry-schema-1.4.0
- old
+ new
@@ -3,25 +3,21 @@
require 'dry/configurable'
require 'dry/initializer'
require 'dry/schema/type_registry'
require 'dry/schema/type_container'
+require 'dry/schema/processor_steps'
require 'dry/schema/rule_applier'
require 'dry/schema/key_coercer'
require 'dry/schema/value_coercer'
module Dry
module Schema
# Processes input data using objects configured within the DSL
+ # Processing is split into steps represented by `ProcessorSteps`.
#
- # Processing is split into 4 main steps:
- #
- # 1. Prepare input hash using a key map
- # 2. Apply pre-coercion filtering rules (optional step, used only when `filter` was used)
- # 3. Apply value coercions based on type specifications
- # 4. Apply rules
- #
+ # @see ProcessorSteps
# @see Params
# @see JSON
#
# @api public
class Processor
@@ -30,11 +26,11 @@
setting :key_map_type
setting :type_registry_namespace, :strict
setting :filter_empty_string, false
- option :steps, default: -> { EMPTY_ARRAY.dup }
+ option :steps, default: -> { ProcessorSteps.new }
option :schema_dsl
class << self
# Return DSL configured via #define
@@ -75,33 +71,20 @@
raise ArgumentError, 'Cannot create a schema without a definition'
end
end
end
- # Append a step
- #
- # @return [Processor]
- #
- # @api private
- def <<(step)
- steps << step
- self
- end
-
# Apply processing steps to the provided input
#
# @param [Hash] input
#
# @return [Result]
#
# @api public
def call(input)
Result.new(input, message_compiler: message_compiler) do |result|
- steps.each do |step|
- output = step.(result)
- result.replace(output) if output.is_a?(::Hash)
- end
+ steps.call(result)
end
end
alias_method :[], :call
# Return a proc that acts like a schema object
@@ -117,11 +100,11 @@
#
# @return [KeyMap]
#
# @api public
def key_map
- @key_map ||= steps.detect { |s| s.is_a?(KeyCoercer) }.key_map
+ steps[:key_coercer].key_map
end
# Return string represntation
#
# @return [String]
@@ -137,11 +120,11 @@
#
# @return [Dry::Types::Safe]
#
# @api private
def type_schema
- @type_schema ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema
+ steps[:value_coercer].type_schema
end
# Return the rules config
#
# @return [Dry::Types::Config]
@@ -178,10 +161,10 @@
# Return the rule applier
#
# @api private
def rule_applier
- @rule_applier ||= steps.last
+ steps[:rule_applier]
end
alias_method :to_rule, :rule_applier
# Check if there are filter rules
#