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



65
66
67
# File 'lib/dry/schema/processor_steps.rb', line 65

def [](name)
  steps[name]
end

#[]=(name, value) ⇒ Object

Sets step by name

Parameters:

  • name (Symbol)

    The step name



74
75
76
# File 'lib/dry/schema/processor_steps.rb', line 74

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:



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

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:



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

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:



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

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.



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

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.



51
52
53
# File 'lib/dry/schema/processor_steps.rb', line 51

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:



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

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.



121
122
123
124
125
# File 'lib/dry/schema/processor_steps.rb', line 121

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.



46
47
48
# File 'lib/dry/schema/processor_steps.rb', line 46

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.



56
57
58
# File 'lib/dry/schema/processor_steps.rb', line 56

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