Class: Dry::Schema::ProcessorSteps

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/dry/schema/processor_steps.rb

Overview

Steps for the Dry::Schema::Processor

There are 4 main steps:

1. `key_coercer` - Prepare input hash using a key map
2. `filter_schema` - Apply pre-coercion filtering rules
   (optional step, used only when `filter` was used)
3. `value_coercer` - Apply value coercions based on type specifications
4. `rule_applier` - Apply rules

See Also:

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Returns step by name

Parameters:

  • name (Symbol)

    The step name



67
68
69
# File 'lib/dry/schema/processor_steps.rb', line 67

def [](name)
  steps[name]
end

#[]=(name, value) ⇒ Object

Sets step by name

Parameters:

  • name (Symbol)

    The step name



76
77
78
# File 'lib/dry/schema/processor_steps.rb', line 76

def []=(name, value)
  steps[name] = Step.new(type: :core, name: name, executor: value)
end

#after(name, &block) ⇒ ProcessorSteps

Add passed block before mentioned step

Parameters:

  • name (Symbol)

    The step name

Returns:



87
88
89
90
91
92
# File 'lib/dry/schema/processor_steps.rb', line 87

def after(name, &block)
  after_steps[name] ||= EMPTY_ARRAY.dup
  after_steps[name] << Step.new(type: :after, name: name, executor: block)
  after_steps[name].sort_by!(&:path)
  self
end

#before(name, &block) ⇒ ProcessorSteps

Add passed block before mentioned step

Parameters:

  • name (Symbol)

    The step name

Returns:



101
102
103
104
105
106
# File 'lib/dry/schema/processor_steps.rb', line 101

def before(name, &block)
  before_steps[name] ||= EMPTY_ARRAY.dup
  before_steps[name] << Step.new(type: :before, name: name, executor: block)
  before_steps[name].sort_by!(&:path)
  self
end

#call(result) ⇒ Result

Executes steps and callbacks in order

Parameters:

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/dry/schema/processor_steps.rb', line 37

def call(result)
  STEPS_IN_ORDER.each do |name|
    before_steps[name]&.each { |step| step&.(result) }
    steps[name]&.(result)
    after_steps[name]&.each { |step| step&.(result) }
  end

  result
end

#import_callbacks(path, other) ⇒ 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.



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dry/schema/processor_steps.rb', line 130

def import_callbacks(path, other)
  other.before_steps.each do |name, steps|
    before_steps[name] ||= []
    before_steps[name].concat(steps.map { |step| step.scoped(path) }).sort_by!(&:path)
  end

  other.after_steps.each do |name, steps|
    after_steps[name] ||= []
    after_steps[name].concat(steps.map { |step| step.scoped(path) }).sort_by!(&:path)
  end
end

#key_mapObject

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.



53
54
55
# File 'lib/dry/schema/processor_steps.rb', line 53

def key_map
  @key_map ||= self[:key_coercer].executor.key_map
end

#merge(other) ⇒ ProcessorSteps

Stacks callback steps and returns new ProcessorSteps instance

Parameters:

Returns:



115
116
117
118
119
120
# File 'lib/dry/schema/processor_steps.rb', line 115

def merge(other)
  ProcessorSteps.new(
    before_steps: merge_callbacks(before_steps, other.before_steps),
    after_steps: merge_callbacks(after_steps, other.after_steps)
  )
end

#merge_callbacks(left, right) ⇒ 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.



123
124
125
126
127
# File 'lib/dry/schema/processor_steps.rb', line 123

def merge_callbacks(left, right)
  left.merge(right) do |_key, oldval, newval|
    (oldval + newval).sort_by(&:path)
  end
end

#rule_applierObject

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.



48
49
50
# File 'lib/dry/schema/processor_steps.rb', line 48

def rule_applier
  @rule_applier ||= steps[:rule_applier].executor
end

#type_schemaObject

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.



58
59
60
# File 'lib/dry/schema/processor_steps.rb', line 58

def type_schema
  @type_schema ||= steps[:value_coercer].executor.type_schema
end