lib/dry/schema/processor.rb in dry-schema-1.4.3 vs lib/dry/schema/processor.rb in dry-schema-1.5.0
- old
+ new
@@ -1,16 +1,17 @@
# frozen_string_literal: true
-require 'dry/configurable'
-require 'dry/initializer'
+require "dry/configurable"
+require "dry/initializer"
+require "dry/logic/operators"
-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'
+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`.
@@ -22,10 +23,12 @@
# @api public
class Processor
extend Dry::Initializer
extend Dry::Configurable
+ include Dry::Logic::Operators
+
setting :key_map_type
setting :type_registry_namespace, :strict
setting :filter_empty_string, false
option :steps, default: -> { ProcessorSteps.new }
@@ -66,11 +69,11 @@
yield(processor) if block
processor
elsif definition
definition.call
else
- raise ArgumentError, 'Cannot create a schema without a definition'
+ raise ArgumentError, "Cannot create a schema without a definition"
end
end
end
# Apply processing steps to the provided input
@@ -79,32 +82,40 @@
#
# @return [Result]
#
# @api public
def call(input)
- Result.new(input, message_compiler: message_compiler) do |result|
+ Result.new(input, input: input, message_compiler: message_compiler) do |result|
steps.call(result)
end
end
alias_method :[], :call
- # Return a proc that acts like a schema object
+ # @api public
+ def xor(other)
+ raise NotImplementedError, "composing schemas using `xor` operator is not supported yet"
+ end
+ alias_method :^, :xor
+
+ # Merge with another schema
#
- # @return [Proc]
+ # @param [Processor] other
#
+ # @return [Processor, Params, JSON]
+ #
# @api public
- def to_proc
- ->(input) { call(input) }
+ def merge(other)
+ schema_dsl.merge(other.schema_dsl).()
end
- # Return the key map
+ # Return a proc that acts like a schema object
#
- # @return [KeyMap]
+ # @return [Proc]
#
# @api public
- def key_map
- steps[:key_coercer].key_map
+ def to_proc
+ ->(input) { call(input) }
end
# Return string represntation
#
# @return [String]
@@ -114,19 +125,36 @@
<<~STR.strip
#<#{self.class.name} keys=#{key_map.map(&:dump)} rules=#{rules.map { |k, v| [k, v.to_s] }.to_h}>
STR
end
+ # Return the key map
+ #
+ # @return [KeyMap]
+ #
+ # @api public
+ def key_map
+ steps.key_map
+ end
+
# Return the type schema
#
# @return [Dry::Types::Safe]
#
# @api private
def type_schema
- steps[:value_coercer].type_schema
+ steps.type_schema
end
+ # Return the rule applier
+ #
+ # @api private
+ def rule_applier
+ steps.rule_applier
+ end
+ alias_method :to_rule, :rule_applier
+
# Return the rules config
#
# @return [Dry::Types::Config]
#
# @api private
@@ -135,13 +163,14 @@
end
# Return AST representation of the rules
#
# @api private
- def to_ast
+ def to_ast(*)
rule_applier.to_ast
end
+ alias_method :ast, :to_ast
# Return the message compiler
#
# @return [MessageCompiler]
#
@@ -156,17 +185,9 @@
#
# @api private
def rules
rule_applier.rules
end
-
- # Return the rule applier
- #
- # @api private
- def rule_applier
- steps[:rule_applier]
- end
- alias_method :to_rule, :rule_applier
# Check if there are filter rules
#
# @api private
def filter_rules?