Class: Laminar::Flow::Step
- Inherits:
-
Object
- Object
- Laminar::Flow::Step
- Includes:
- OptionsValidator
- Defined in:
- lib/laminar/flow/step.rb
Overview
Specification for an executable step in a Flow.
Instance Attribute Summary collapse
-
#branches ⇒ Object
readonly
Returns the value of attribute branches.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#after(*args, &block) ⇒ Object
Defines a callback to run after the flow executes the step.
-
#after_callbacks ⇒ Object
Return the list of after callbacks.
-
#before(*args, &block) ⇒ Object
Defines a callback to run before the flow executes the step.
-
#before_callbacks ⇒ Object
Return the list of before callbacks.
-
#branch(target, options = {}) ⇒ Object
(also: #goto)
Add a branch specification.
- #endflow(options = {}) ⇒ Object
-
#first_applicable_branch(target) ⇒ Object
Return the first branch that satisfies its condition.
-
#initialize(name, options = {}, &block) ⇒ Step
constructor
A new instance of Step.
-
#next_step_name(impl_context) ⇒ Object
Find the next rule in the flow.
-
#particle ⇒ Object
Return class instance of the associated particle.
Methods included from OptionsValidator
Constructor Details
#initialize(name, options = {}, &block) ⇒ Step
Returns a new instance of Step.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/laminar/flow/step.rb', line 16 def initialize(name, = {}, &block) raise ArgumentError, 'invalid name' unless name.class.method_defined?(:to_sym) () @class_name = ([:class] || name).to_s.camelize @name = name.to_sym @branches = [] instance_eval(&block) if block end |
Instance Attribute Details
#branches ⇒ Object (readonly)
Returns the value of attribute branches.
12 13 14 |
# File 'lib/laminar/flow/step.rb', line 12 def branches @branches end |
#class_name ⇒ Object (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 |
#name ⇒ Object (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_callbacks ⇒ Object
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_callbacks ⇒ Object
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, = {}) branches << Branch.new(target, ) end |
#endflow(options = {}) ⇒ Object
44 45 46 |
# File 'lib/laminar/flow/step.rb', line 44 def endflow( = {}) branches << Branch.new(:endflow, ) 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 |
#particle ⇒ Object
Return class instance of the associated particle.
28 29 30 |
# File 'lib/laminar/flow/step.rb', line 28 def particle class_name.constantize end |