Class: Newflow::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/newflow/transition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_state, opts) ⇒ Transition

Returns a new instance of Transition.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/newflow/transition.rb', line 5

def initialize(target_state,opts)
  @target_state = target_state
  if_meth      = opts[:if]
  unless_meth  = opts[:unless]
  @trigger      = Trigger.new(opts[:trigger])
  logger.debug "State.transitions_to: target_state=#{target_state} if=#{if_meth.inspect} unless=#{unless_meth.inspect} trigger=#{@trigger}"
  unless @target_state \
      &&  (if_meth || unless_meth) \
      && !(if_meth && unless_meth) 
    raise "You must specify a target state(#@target_state) and (if_method OR unless_method)" 
  end
  @predicate_name = (if_meth || "!#{unless_meth}").to_s
  @predicate = if if_meth
                 # TODO: be smart
                 if if_meth.respond_to?(:call)
                   lambda { |wf| if_meth.call }
                 else
                   lambda { |wf| wf.send(if_meth) }
                 end
               else
                 if unless_meth.respond_to?(:call)
                   lambda { |wf| !unless_meth.call }
                 else
                   lambda { |wf| !wf.send(unless_meth) }
                 end
               end
end

Instance Attribute Details

#predicateObject (readonly)

Returns the value of attribute predicate.



3
4
5
# File 'lib/newflow/transition.rb', line 3

def predicate
  @predicate
end

#predicate_nameObject (readonly)

Returns the value of attribute predicate_name.



3
4
5
# File 'lib/newflow/transition.rb', line 3

def predicate_name
  @predicate_name
end

#target_stateObject (readonly)

Returns the value of attribute target_state.



3
4
5
# File 'lib/newflow/transition.rb', line 3

def target_state
  @target_state
end

#triggerObject (readonly)

Returns the value of attribute trigger.



3
4
5
# File 'lib/newflow/transition.rb', line 3

def trigger
  @trigger
end

Instance Method Details

#can_transition?(workflow) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/newflow/transition.rb', line 33

def can_transition?(workflow)
  predicate.call(workflow)
end

#loggerObject



41
42
43
# File 'lib/newflow/transition.rb', line 41

def logger
  Newflow.logger
end

#trigger!(workflow) ⇒ Object



37
38
39
# File 'lib/newflow/transition.rb', line 37

def trigger!(workflow)
  trigger.run!(workflow)
end