Class: Newflow::State
- Inherits:
-
Object
- Object
- Newflow::State
- Defined in:
- lib/newflow/state.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
-
#initialize(name, opts = {}, &transitions_block) ⇒ State
constructor
A new instance of State.
- #logger ⇒ Object
- #run(workflow, do_trigger = Newflow::WITH_SIDE_EFFECTS) ⇒ Object
- #run_on_entry(workflow, do_trigger = Newflow::WITH_SIDE_EFFECTS) ⇒ Object
-
#start? ⇒ Boolean
TODO: use convention of name == :start instead of a :start opt, not same for stop.
- #stop? ⇒ Boolean
- #to_s ⇒ Object
- #transitions_to(target_state, opts = {}) ⇒ Object
Constructor Details
#initialize(name, opts = {}, &transitions_block) ⇒ State
Returns a new instance of State.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/newflow/state.rb', line 5 def initialize(name, opts={}, &transitions_block) logger.debug "State.initialize: name=#{name} opts=#{opts.inspect}" @name = name @opts = opts @is_start = opts[:start] @is_stop = opts[:stop] @on_entry = Trigger.new(opts[:on_entry]) @transitions = [] check_validity instance_eval &transitions_block if transitions_block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/newflow/state.rb', line 3 def name @name end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
3 4 5 |
# File 'lib/newflow/state.rb', line 3 def transitions @transitions end |
Instance Method Details
#run(workflow, do_trigger = Newflow::WITH_SIDE_EFFECTS) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/newflow/state.rb', line 21 def run(workflow, do_trigger=Newflow::WITH_SIDE_EFFECTS) return @name unless @transitions # We may want to consider looking at all transitions and letting user know # that you can move in multiple directions transition_to = @transitions.detect { |t| t.can_transition?(workflow) } if transition_to transition_to.trigger!(workflow) if do_trigger # TODO: TEST transition_to.target_state else @name end end |
#run_on_entry(workflow, do_trigger = Newflow::WITH_SIDE_EFFECTS) ⇒ Object
43 44 45 |
# File 'lib/newflow/state.rb', line 43 def run_on_entry(workflow, do_trigger=Newflow::WITH_SIDE_EFFECTS) @on_entry.run!(workflow) if do_trigger && @on_entry end |
#start? ⇒ Boolean
TODO: use convention of name == :start instead of a :start opt, not same for stop
35 36 37 |
# File 'lib/newflow/state.rb', line 35 def start? @opts[:start] end |
#stop? ⇒ Boolean
39 40 41 |
# File 'lib/newflow/state.rb', line 39 def stop? @opts[:stop] end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/newflow/state.rb', line 47 def to_s @name.to_s end |
#transitions_to(target_state, opts = {}) ⇒ Object
17 18 19 |
# File 'lib/newflow/state.rb', line 17 def transitions_to(target_state, opts={}) @transitions << Transition.new(target_state,opts) end |