Class: Newflow::Transition
- Inherits:
-
Object
- Object
- Newflow::Transition
- Defined in:
- lib/newflow/transition.rb
Instance Attribute Summary collapse
-
#predicate ⇒ Object
readonly
Returns the value of attribute predicate.
-
#predicate_name ⇒ Object
readonly
Returns the value of attribute predicate_name.
-
#target_state ⇒ Object
readonly
Returns the value of attribute target_state.
-
#trigger ⇒ Object
readonly
Returns the value of attribute trigger.
Instance Method Summary collapse
- #can_transition?(workflow) ⇒ Boolean
-
#initialize(target_state, opts) ⇒ Transition
constructor
A new instance of Transition.
- #logger ⇒ Object
- #trigger!(workflow) ⇒ Object
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
#predicate ⇒ Object (readonly)
Returns the value of attribute predicate.
3 4 5 |
# File 'lib/newflow/transition.rb', line 3 def predicate @predicate end |
#predicate_name ⇒ Object (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_state ⇒ Object (readonly)
Returns the value of attribute target_state.
3 4 5 |
# File 'lib/newflow/transition.rb', line 3 def target_state @target_state end |
#trigger ⇒ Object (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
33 34 35 |
# File 'lib/newflow/transition.rb', line 33 def can_transition?(workflow) predicate.call(workflow) end |
#trigger!(workflow) ⇒ Object
37 38 39 |
# File 'lib/newflow/transition.rb', line 37 def trigger!(workflow) trigger.run!(workflow) end |