Class: Dry::Schema::Processor
- Inherits:
-
Object
- Object
- Dry::Schema::Processor
- Extended by:
- Configurable, Initializer
- Defined in:
- lib/dry/schema/processor.rb
Overview
Processes input data using objects configured within the DSL
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
Class Method Summary collapse
-
.define(&block) ⇒ Class
Define a schema for your processor class.
-
.definition ⇒ DSL
private
Return DSL configured via #define.
-
.new(&block) ⇒ Processor
Build a new processor object.
Instance Method Summary collapse
-
#<<(step) ⇒ Processor
private
Append a step.
-
#call(input) ⇒ Result
Apply processing steps to the provided input.
-
#key_map ⇒ KeyMap
Return the key map.
-
#message_compiler ⇒ MessageCompiler
private
Return the message compiler.
-
#rule_applier ⇒ Object
(also: #to_rule)
private
Return the rule applier.
-
#rules ⇒ MessageCompiler
private
Return the rules from rule applier.
-
#to_ast ⇒ Object
private
Return AST representation of the rules.
-
#type_schema ⇒ Dry::Types::Safe
private
Return the type schema.
Class Method Details
.define(&block) ⇒ Class
Define a schema for your processor class
41 42 43 44 45 46 |
# File 'lib/dry/schema/processor.rb', line 41 def self.define(&block) @__definition__ ||= DSL.new( processor_type: self, parent: superclass.definition, **config, &block ) self end |
.definition ⇒ DSL
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return DSL configured via #define
53 54 55 |
# File 'lib/dry/schema/processor.rb', line 53 def self.definition @__definition__ ||= nil end |
.new(&block) ⇒ Processor
Build a new processor object
62 63 64 65 66 67 68 69 70 |
# File 'lib/dry/schema/processor.rb', line 62 def self.new(&block) if block super.tap(&block) elsif definition definition.call else raise ArgumentError, 'Cannot create a schema without a definition' end end |
Instance Method Details
#<<(step) ⇒ Processor
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Append a step
77 78 79 80 |
# File 'lib/dry/schema/processor.rb', line 77 def <<(step) steps << step self end |
#call(input) ⇒ Result
Apply processing steps to the provided input
89 90 91 92 93 94 95 96 |
# File 'lib/dry/schema/processor.rb', line 89 def call(input) Result.new(input, message_compiler: ) do |result| steps.each do |step| output = step.(result) result.replace(output) if output.is_a?(::Hash) end end end |
#key_map ⇒ KeyMap
Return the key map
103 104 105 |
# File 'lib/dry/schema/processor.rb', line 103 def key_map @__key_map__ ||= steps.detect { |s| s.is_a?(KeyCoercer) }.key_map end |
#message_compiler ⇒ MessageCompiler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the message compiler
128 129 130 |
# File 'lib/dry/schema/processor.rb', line 128 def rule_applier. end |
#rule_applier ⇒ Object Also known as: to_rule
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the rule applier
144 145 146 147 |
# File 'lib/dry/schema/processor.rb', line 144 def rule_applier # TODO: make this more explicit through class types @__rule_applier__ ||= steps.last end |
#rules ⇒ MessageCompiler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the rules from rule applier
137 138 139 |
# File 'lib/dry/schema/processor.rb', line 137 def rules rule_applier.rules end |
#to_ast ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return AST representation of the rules
119 120 121 |
# File 'lib/dry/schema/processor.rb', line 119 def to_ast rule_applier.to_ast end |
#type_schema ⇒ Dry::Types::Safe
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the type schema
112 113 114 |
# File 'lib/dry/schema/processor.rb', line 112 def type_schema @__type_schema__ ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema end |