Class: ActionFlow::Flow::State

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flow_name) ⇒ State

Returns a new instance of State.



7
8
9
10
11
12
13
14
15
# File 'lib/action_flow/flow/state.rb', line 7

def initialize(flow_name)
  @name       = flow_name
  @flow       = ActionFlow.flows[flow_name]
  @index      = 0
  @max_index  = 0
  @variables  = {}
  @complete   = false
  @terminated = false
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



5
6
7
# File 'lib/action_flow/flow/state.rb', line 5

def variables
  @variables
end

Class Method Details

.from_session_object(flow_name, data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/action_flow/flow/state.rb', line 53

def self.from_session_object(flow_name, data)
  instance = new(flow_name)
  instance.instance_eval do
    @index     = data[0]
    @max_index = data[1]
    @variables = data[2]
    @complete  = data[3]
  end
  instance
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/action_flow/flow/state.rb', line 37

def complete?
  @complete
end

#match_distance(context) ⇒ Object



17
18
19
# File 'lib/action_flow/flow/state.rb', line 17

def match_distance(context)
  @flow.match_distance(@index, context)
end

#next_action(params = {}) ⇒ Object



45
46
47
# File 'lib/action_flow/flow/state.rb', line 45

def next_action(params = {})
  @flow.action_at(@index + 1, variables, params)
end

#progress!(context) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/action_flow/flow/state.rb', line 21

def progress!(context)
  if @flow.terminates_on?(context)
    @terminated = true
    return
  end
  @index += 1 if @flow.match_at?(@index + 1, context)
  @max_index = [@max_index, @index].max
  return if current_matches?(context)
  
  0.upto(@max_index) do |backtrack|
    @index = backtrack if @flow.match_at?(backtrack, context)
  end
  
  @complete = true if @index == @flow.length - 1
end

#terminated?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/action_flow/flow/state.rb', line 41

def terminated?
  @terminated
end

#to_session_objectObject



49
50
51
# File 'lib/action_flow/flow/state.rb', line 49

def to_session_object
  [@index, @max_index, @variables, @complete]
end