Module: Stealth::Flow::InstanceMethods

Defined in:
lib/stealth/flow/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flow_stateObject

Returns the value of attribute flow_state.



71
72
73
# File 'lib/stealth/flow/base.rb', line 71

def flow_state
  @flow_state
end

#user_idObject

Returns the value of attribute user_id.



71
72
73
# File 'lib/stealth/flow/base.rb', line 71

def user_id
  @user_id
end

Instance Method Details

#current_stateObject



73
74
75
76
77
# File 'lib/stealth/flow/base.rb', line 73

def current_state
  loaded_state = load_flow_state
  res = spec.states[loaded_state.to_sym] if loaded_state
  res || spec.initial_state
end

#halt(reason = nil) ⇒ Object



127
128
129
130
# File 'lib/stealth/flow/base.rb', line 127

def halt(reason = nil)
  @halted_because = reason
  @halted = true
end

#halt!(reason = nil) ⇒ Object

Raises:



132
133
134
135
136
# File 'lib/stealth/flow/base.rb', line 132

def halt!(reason = nil)
  @halted_because = reason
  @halted = true
  raise TransitionHalted.new(reason)
end

#halted?Boolean

Return true if the last transition was halted by one of the transition callbacks.

Returns:

  • (Boolean)


80
81
82
# File 'lib/stealth/flow/base.rb', line 80

def halted?
  @halted
end

#halted_becauseObject

Return the reason of the last transition abort as set by the previous call of ‘halt` or `halt!` method.



86
87
88
# File 'lib/stealth/flow/base.rb', line 86

def halted_because
  @halted_because
end

#init_state(state) ⇒ Object



157
158
159
160
161
# File 'lib/stealth/flow/base.rb', line 157

def init_state(state)
  res = spec.states[state.to_sym] if state
  @flow_state = res || spec.initial_state
  self
end

#process_event!(name, *args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/stealth/flow/base.rb', line 90

def process_event!(name, *args)
  event = current_state.events.first_applicable(name, self)
  raise NoTransitionAllowed.new(
    "There is no event #{name.to_sym} defined for the #{current_state} state") \
    if event.nil?
  @halted_because = nil
  @halted = false

  check_transition(event)

  from = current_state
  to = spec.states[event.transitions_to]

  run_before_transition(from, to, name, *args)
  return false if @halted

  begin
    return_value = run_action(event.action, *args) || run_action_callback(event.name, *args)
  rescue StandardError => e
    run_on_error(e, from, to, name, *args)
  end

  return false if @halted

  run_on_transition(from, to, name, *args)

  run_on_exit(from, to, name, *args)

  transition_value = persist_flow_state(to.to_s)

  run_on_entry(to, from, name, *args)

  run_after_transition(from, to, name, *args)

  return_value.nil? ? transition_value : return_value
end

#specObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/stealth/flow/base.rb', line 138

def spec
  # check the singleton class first
  class << self
    return flow_spec if flow_spec
  end

  c = self.class
  # using a simple loop instead of class_inheritable_accessor to avoid
  # dependency on Rails' ActiveSupport
  until c.flow_spec || !(c.include? Stealth::Flow)
    c = c.superclass
  end
  c.flow_spec
end

#statesObject



153
154
155
# File 'lib/stealth/flow/base.rb', line 153

def states
  self.spec.states.keys
end