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 Attribute Summary collapse
-
.definition ⇒ DSL
readonly
private
Return DSL configured via #define.
Class Method Summary collapse
-
.define(&block) ⇒ Class
Define a schema for your processor class.
-
.new(&block) ⇒ Processor
Build a new processor object.
Instance Method Summary collapse
-
#<<(step) ⇒ Processor
private
Append a step.
-
#call(input) ⇒ Result
(also: #[])
Apply processing steps to the provided input.
-
#config ⇒ Dry::Types::Config
private
Return the rules config.
-
#inspect ⇒ String
Return string represntation.
-
#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.
-
#to_proc ⇒ Proc
Return a proc that acts like a schema object.
-
#type_schema ⇒ Dry::Types::Safe
private
Return the type schema.
Class Attribute Details
.definition ⇒ DSL (readonly)
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
40 41 42 |
# File 'lib/dry/schema/processor.rb', line 40 def definition @definition end |
Class Method Details
.define(&block) ⇒ Class
Define a schema for your processor class
51 52 53 54 55 56 |
# File 'lib/dry/schema/processor.rb', line 51 def define(&block) @definition ||= DSL.new( processor_type: self, parent: superclass.definition, **config, &block ) self end |
.new(&block) ⇒ Processor
Build a new processor object
63 64 65 66 67 68 69 70 71 |
# File 'lib/dry/schema/processor.rb', line 63 def 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
79 80 81 82 |
# File 'lib/dry/schema/processor.rb', line 79 def <<(step) steps << step self end |
#call(input) ⇒ Result Also known as: []
Apply processing steps to the provided input
91 92 93 94 95 96 97 98 |
# File 'lib/dry/schema/processor.rb', line 91 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 |
#config ⇒ Dry::Types::Config
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 config
144 145 146 |
# File 'lib/dry/schema/processor.rb', line 144 def config @config ||= rule_applier.config end |
#inspect ⇒ String
Return string represntation
124 125 126 127 128 |
# File 'lib/dry/schema/processor.rb', line 124 def inspect " #<\#{self.class.name} keys=\#{key_map.map(&:dump)} rules=\#{rules.map { |k, v| [k, v.to_s] }.to_h}>\n STR\nend\n".strip |
#key_map ⇒ KeyMap
Return the key map
115 116 117 |
# File 'lib/dry/schema/processor.rb', line 115 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
160 161 162 |
# File 'lib/dry/schema/processor.rb', line 160 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
176 177 178 |
# File 'lib/dry/schema/processor.rb', line 176 def rule_applier @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
169 170 171 |
# File 'lib/dry/schema/processor.rb', line 169 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
151 152 153 |
# File 'lib/dry/schema/processor.rb', line 151 def to_ast rule_applier.to_ast end |
#to_proc ⇒ Proc
Return a proc that acts like a schema object
106 107 108 |
# File 'lib/dry/schema/processor.rb', line 106 def to_proc ->(input) { call(input) } 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
135 136 137 |
# File 'lib/dry/schema/processor.rb', line 135 def type_schema @type_schema ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema end |