Class: CarryOut::Plan::Node
- Inherits:
-
Object
- Object
- CarryOut::Plan::Node
- Defined in:
- lib/carry_out/plan/node.rb
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#connects_to ⇒ Object
Returns the value of attribute connects_to.
-
#guarded_by ⇒ Object
readonly
Returns the value of attribute guarded_by.
-
#return_transform ⇒ Object
Returns the value of attribute return_transform.
-
#returns_as ⇒ Object
Returns the value of attribute returns_as.
Instance Method Summary collapse
- #call(context = {}) ⇒ Object
- #guard_with(guard) ⇒ Object
- #guard_with_inverse(guard) ⇒ Object
-
#initialize(action = nil) ⇒ Node
constructor
A new instance of Node.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to?(method, private = false) ⇒ Boolean
Constructor Details
#initialize(action = nil) ⇒ Node
Returns a new instance of Node.
13 14 15 16 |
# File 'lib/carry_out/plan/node.rb', line 13 def initialize(action = nil) @action = action = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
53 54 55 56 57 |
# File 'lib/carry_out/plan/node.rb', line 53 def method_missing(method, *args, &block) if respond_to?(method) .push({ method: method, source: block || (args.length == 0 ? true : args.first) }) end end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
7 8 9 |
# File 'lib/carry_out/plan/node.rb', line 7 def action @action end |
#connects_to ⇒ Object
Returns the value of attribute connects_to.
8 9 10 |
# File 'lib/carry_out/plan/node.rb', line 8 def connects_to @connects_to end |
#guarded_by ⇒ Object (readonly)
Returns the value of attribute guarded_by.
9 10 11 |
# File 'lib/carry_out/plan/node.rb', line 9 def guarded_by @guarded_by end |
#return_transform ⇒ Object
Returns the value of attribute return_transform.
11 12 13 |
# File 'lib/carry_out/plan/node.rb', line 11 def return_transform @return_transform end |
#returns_as ⇒ Object
Returns the value of attribute returns_as.
10 11 12 |
# File 'lib/carry_out/plan/node.rb', line 10 def returns_as @returns_as end |
Instance Method Details
#call(context = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/carry_out/plan/node.rb', line 18 def call(context = {}) return NodeResult.new unless @action return unless guard(context) result = @action.call do |a| .map do |m| value = m[:source] if value.respond_to?(:call) value = GuardContext.new(context).instance_exec(context, &value) end a.send(m[:method], value) end end result = return_transform.call(result) unless return_transform.nil? NodeResult.new(result) end |
#guard_with(guard) ⇒ Object
39 40 41 42 |
# File 'lib/carry_out/plan/node.rb', line 39 def guard_with(guard) guard = guard.respond_to?(:call) ? guard : Proc.new { guard } guarded_by.push Guard.new(guard) end |
#guard_with_inverse(guard) ⇒ Object
44 45 46 47 |
# File 'lib/carry_out/plan/node.rb', line 44 def guard_with_inverse(guard) guard_with(guard) guarded_by.last.invert end |
#respond_to?(method, private = false) ⇒ Boolean
59 60 61 |
# File 'lib/carry_out/plan/node.rb', line 59 def respond_to?(method, private = false) (@action && @action.respond_to?(:has_parameter?) && @action.has_parameter?(method)) || super end |