Class: Laminar::Flow::Step

Inherits:
Object
  • Object
show all
Includes:
OptionsValidator
Defined in:
lib/laminar/flow/step.rb

Overview

Specification for an executable step in a Flow.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OptionsValidator

included

Constructor Details

#initialize(name, options = {}, &block) ⇒ Step

Returns a new instance of Step.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/laminar/flow/step.rb', line 16

def initialize(name, options = {}, &block)
  raise ArgumentError, 'invalid name' unless name.class.method_defined?(:to_sym)

  validate_options(options)
  @class_name = (options[:class] || name).to_s.camelize
  @name = name.to_sym
  @branches = []

  instance_eval(&block) if block
end

Instance Attribute Details

#branchesObject (readonly)

Returns the value of attribute branches.



12
13
14
# File 'lib/laminar/flow/step.rb', line 12

def branches
  @branches
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



12
13
14
# File 'lib/laminar/flow/step.rb', line 12

def class_name
  @class_name
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/laminar/flow/step.rb', line 12

def name
  @name
end

Instance Method Details

#after(*args, &block) ⇒ Object

Defines a callback to run after the flow executes the step.



65
66
67
68
# File 'lib/laminar/flow/step.rb', line 65

def after(*args, &block)
  after_callbacks.concat(args)
  after_callbacks << block if block
end

#after_callbacksObject

Return the list of after callbacks.



76
77
78
# File 'lib/laminar/flow/step.rb', line 76

def after_callbacks
  @after_callbacks ||= []
end

#before(*args, &block) ⇒ Object

Defines a callback to run before the flow executes the step.



59
60
61
62
# File 'lib/laminar/flow/step.rb', line 59

def before(*args, &block)
  before_callbacks.concat(args)
  before_callbacks << block if block
end

#before_callbacksObject

Return the list of before callbacks.



71
72
73
# File 'lib/laminar/flow/step.rb', line 71

def before_callbacks
  @before_callbacks ||= []
end

#branch(target, options = {}) ⇒ Object Also known as: goto

Add a branch specification. This is typically called as part of a flow specification:

flow do

step :step1

end



39
40
41
# File 'lib/laminar/flow/step.rb', line 39

def branch(target, options = {})
  branches << Branch.new(target, options)
end

#endflow(options = {}) ⇒ Object



44
45
46
# File 'lib/laminar/flow/step.rb', line 44

def endflow(options = {})
  branches << Branch.new(:endflow, options)
end

#first_applicable_branch(target) ⇒ Object

Return the first branch that satisfies its condition.



81
82
83
84
85
86
# File 'lib/laminar/flow/step.rb', line 81

def first_applicable_branch(target)
  branches.each do |branch|
    return branch if branch.meets_condition?(target)
  end
  nil
end

#next_step_name(impl_context) ⇒ Object

Find the next rule in the flow. Examines the branches associated with the current rule and returns the name of the first branch that satisfies its condition.



51
52
53
54
55
56
# File 'lib/laminar/flow/step.rb', line 51

def next_step_name(impl_context)
  branch = first_applicable_branch(impl_context)
  return if branch.nil?

  branch.name
end

#particleObject

Return class instance of the associated particle.



28
29
30
# File 'lib/laminar/flow/step.rb', line 28

def particle
  class_name.constantize
end