Module: Workflow::Join

Defined in:
lib/workflow/join.rb,
lib/workflow/join/version.rb

Constant Summary collapse

GUARD_PARAMS_ERROR =
'Either guard instance variable name or a code block is required'.freeze
GUARD_POINTCUT_ERROR =
'Both :inner and :outer states are required'.freeze
GUARD_IS_NOT_WORKFLOW =
'Guard given must be a workflow instance'.freeze
VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#guard(getter = nil, inner: nil, outer: nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/workflow/join.rb', line 70

def guard(getter = nil, inner: nil, outer: nil)
  fail Workflow::WorkflowDefinitionError, GUARD_PARAMS_ERROR unless !getter.nil? ^ block_given?
  fail Workflow::WorkflowDefinitionError, GUARD_POINTCUT_ERROR unless inner && outer

  guard = if block_given?
            Proc.new
          else
            g = getter.to_sym
            lambda do |host|
              case
              when /\A@/ =~ g.to_s && host.instance_variable_defined?(g)
                host.instance_variable_get(g)
              when host.methods.include?(g) && host.method(g).arity.zero?
                host.send g
              end.tap do |guard_instance|
                fail Workflow::WorkflowDefinitionError, GUARD_IS_NOT_WORKFLOW unless guard_instance.is_a?(Workflow)
              end
            end
          end
  (guards[inner.to_sym] ||= []) << [guard, outer.to_sym]
end

#guardsObject



66
67
68
# File 'lib/workflow/join.rb', line 66

def guards
  @guards ||= {}
end