Class: ActionFlow::Flow::Controller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/action_flow/flow/controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Controller

Returns a new instance of Controller.



10
11
12
13
14
# File 'lib/action_flow/flow/controller.rb', line 10

def initialize(context)
  @context = context
  remove_legacy_objects_from_session!
  load_states_from_session!
end

Instance Method Details

#current_flow(has_next = false) ⇒ Object



24
25
26
27
28
29
# File 'lib/action_flow/flow/controller.rb', line 24

def current_flow(has_next = false)
  status.values.
  sort_by  { |state| state.match_distance(self) }.
  find_all { |state| !has_next or state.next_action }.
  first
end

#in_any_flow?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/action_flow/flow/controller.rb', line 20

def in_any_flow?
  not status.empty?
end

#in_flow?(*names) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/action_flow/flow/controller.rb', line 16

def in_flow?(*names)
  names.any? &status.method(:has_key?)
end

#next_in_flow(*args) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/action_flow/flow/controller.rb', line 43

def next_in_flow(*args)
  flow_name = args.find { |arg| Symbol === arg } || nil
  params    = args.find { |arg| Hash === arg }   || {}
  
  flow_state = status[flow_name] || current_flow(true)
  flow_state ? flow_state.next_action(params) : nil
end

#statusObject



51
52
53
# File 'lib/action_flow/flow/controller.rb', line 51

def status
  @states
end

#update_session!Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/action_flow/flow/controller.rb', line 31

def update_session!
  status.each { |name, state| state.progress!(self) }
  
  status.each do |name, state|
    status.delete(name) if state.terminated? or state.complete?
  end
  
  new_flow_candidates.each { |name| status[name] = State.new(name) }
  
  dump_states_to_session!
end