Class: Newflow::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



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

def transitions
  @transitions
end

Instance Method Details

#loggerObject



51
52
53
# File 'lib/newflow/state.rb', line 51

def logger
  Newflow.logger
end

#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

Returns:

  • (Boolean)


35
36
37
# File 'lib/newflow/state.rb', line 35

def start?
  @opts[:start]
end

#stop?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/newflow/state.rb', line 39

def stop?
  @opts[:stop]
end

#to_sObject



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