Class: SmashTheState::Operation::ValidationStep

Inherits:
Step
  • Object
show all
Defined in:
lib/smash_the_state/operation/validation_step.rb

Instance Attribute Summary collapse

Attributes inherited from Step

#error_handler, #name, #options

Instance Method Summary collapse

Methods inherited from Step

#side_effect_free?

Constructor Details

#initialize(options = {}) ⇒ ValidationStep

Returns a new instance of ValidationStep.



6
7
8
9
10
11
12
# File 'lib/smash_the_state/operation/validation_step.rb', line 6

def initialize(options = {})
  @name            = :validate
  @implementations = []
  @options         = {
    side_effect_free: true
  }.merge(options)
end

Instance Attribute Details

#implementationsObject

Returns the value of attribute implementations.



4
5
6
# File 'lib/smash_the_state/operation/validation_step.rb', line 4

def implementations
  @implementations
end

Instance Method Details

#add_implementation(&block) ⇒ Object



14
15
16
17
18
# File 'lib/smash_the_state/operation/validation_step.rb', line 14

def add_implementation(&block)
  tap do
    @implementations << block
  end
end

#dupObject



38
39
40
41
42
43
44
# File 'lib/smash_the_state/operation/validation_step.rb', line 38

def dup
  # it's not enough to duplicate the step, we should also duplicate our
  # implementations. otherwise the list of implementations will be shared
  super.tap do |s|
    s.implementations = s.implementations.dup
  end
end

#implementationObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/smash_the_state/operation/validation_step.rb', line 20

def implementation
  blocks = @implementations

  proc do
    # self here should be a state
    blocks.reduce(self) do |memo, i|
      memo.class_eval(&i)
    end
  end
end

#validate!(state) ⇒ Object



31
32
33
34
35
36
# File 'lib/smash_the_state/operation/validation_step.rb', line 31

def validate!(state)
  state.tap do
    SmashTheState::Operation::State.
      eval_validation_directives_block(state, &implementation)
  end
end