Class: CarryOut::Plan::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/carry_out/plan/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
  @messages = []
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)
    @messages.push({ method: method, source: block || (args.length == 0 ? true : args.first) })
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



7
8
9
# File 'lib/carry_out/plan/node.rb', line 7

def action
  @action
end

#connects_toObject

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_byObject (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_transformObject

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_asObject

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|
    @messages.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

Returns:

  • (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