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(options = nil, &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.
-
#filter_rules? ⇒ Boolean
private
Check if there are filter rules.
-
#filter_schema ⇒ Object
private
Return filter schema.
-
#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
44 45 46 |
# File 'lib/dry/schema/processor.rb', line 44 def definition @definition end |
Class Method Details
.define(&block) ⇒ Class
Define a schema for your processor class
55 56 57 58 59 60 |
# File 'lib/dry/schema/processor.rb', line 55 def define(&block) @definition ||= DSL.new( processor_type: self, parent: superclass.definition, **config, &block ) self end |
.new(options = nil, &block) ⇒ Processor
Build a new processor object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dry/schema/processor.rb', line 67 def new( = nil, &block) if || block processor = super yield(processor) if block processor 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
85 86 87 88 |
# File 'lib/dry/schema/processor.rb', line 85 def <<(step) steps << step self end |
#call(input) ⇒ Result Also known as: []
Apply processing steps to the provided input
97 98 99 100 101 102 103 104 |
# File 'lib/dry/schema/processor.rb', line 97 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
150 151 152 |
# File 'lib/dry/schema/processor.rb', line 150 def config @config ||= rule_applier.config end |
#filter_rules? ⇒ Boolean
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.
Check if there are filter rules
190 191 192 |
# File 'lib/dry/schema/processor.rb', line 190 def filter_rules? @filter_rules_predicate ||= schema_dsl.filter_rules? end |
#filter_schema ⇒ 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 filter schema
197 198 199 |
# File 'lib/dry/schema/processor.rb', line 197 def filter_schema @filter_schema ||= schema_dsl.filter_schema end |
#inspect ⇒ String
Return string represntation
130 131 132 133 134 |
# File 'lib/dry/schema/processor.rb', line 130 def inspect <<~STR.strip #<#{self.class.name} keys=#{key_map.map(&:dump)} rules=#{rules.map { |k, v| [k, v.to_s] }.to_h}> STR end |
#key_map ⇒ KeyMap
Return the key map
121 122 123 |
# File 'lib/dry/schema/processor.rb', line 121 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
166 167 168 |
# File 'lib/dry/schema/processor.rb', line 166 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
182 183 184 |
# File 'lib/dry/schema/processor.rb', line 182 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
175 176 177 |
# File 'lib/dry/schema/processor.rb', line 175 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
157 158 159 |
# File 'lib/dry/schema/processor.rb', line 157 def to_ast rule_applier.to_ast end |
#to_proc ⇒ Proc
Return a proc that acts like a schema object
112 113 114 |
# File 'lib/dry/schema/processor.rb', line 112 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
141 142 143 |
# File 'lib/dry/schema/processor.rb', line 141 def type_schema @type_schema ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema end |