Class: ActionFlow::Context

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/action_flow.rb

Defined Under Namespace

Classes: InvalidState, TransitionFired

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



113
114
115
# File 'lib/action_flow.rb', line 113

def controller
  @controller
end

Class Method Details

.find_or_create(key = nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/action_flow.rb', line 72

def self.find_or_create(key = nil)
  context = find_by_key(key) if key
  context || create(
    :states     => [initial],
    :state_data => {},
    :key        => generate_key
  )
end

.generate_keyObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/action_flow.rb', line 81

def self.generate_key
  sha = Digest::SHA1::new
  now = Time.now
  sha.update(now.to_s)
  sha.update(String(now.usec))
  sha.update(String(rand))
  sha.update(String($$))
  sha.update('go with the flow')
  sha.hexdigest
end

.state(state, &block) ⇒ Object



109
110
111
# File 'lib/action_flow.rb', line 109

def self.state(state, &block)
  transitions[state] = block
end

.statesObject



105
106
107
# File 'lib/action_flow.rb', line 105

def self.states
  transitions.keys
end

.transition(state) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/action_flow.rb', line 96

def self.transition(state)
  ancestors.each do |klass|
    return if klass == ActionFlow::Context
    next unless klass.respond_to?(:transitions)
    transition = klass.transitions[state]
    return transition if transition
  end
end

.transitionsObject



92
93
94
# File 'lib/action_flow.rb', line 92

def self.transitions
  @transitions ||= {}
end

Instance Method Details

#after_findObject



65
66
67
# File 'lib/action_flow.rb', line 65

def after_find
  after_save
end

#after_saveObject



60
61
62
63
# File 'lib/action_flow.rb', line 60

def after_save
  self.states     = Marshal.load(states)
  self.state_data = Marshal.load(state_data)
end

#at_state(state) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/action_flow.rb', line 116

def at_state(state)
  state = state.to_sym
  if states.include?(state)
    @state = state
    states.slice!(states.index(state) + 1..-1)
  else
    raise InvalidState, "state #{state} not valid in this context"
  end
end

#before_saveObject



55
56
57
58
# File 'lib/action_flow.rb', line 55

def before_save
  self.states     = Marshal.dump(states)
  self.state_data = Marshal.dump(state_data)
end

#dataObject



130
131
132
# File 'lib/action_flow.rb', line 130

def data
  state_data[:state] ||= {}
end

#fire_transition(controller) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/action_flow.rb', line 134

def fire_transition(controller)
  @controller = controller
  begin
    transition = self.class.transition(state)
    transition.bind(self).call
  rescue TransitionFired
  end
end

#stateObject



126
127
128
# File 'lib/action_flow.rb', line 126

def state
  @state ||= states.last
end

#transition(state) ⇒ Object

Raises:



143
144
145
146
147
148
# File 'lib/action_flow.rb', line 143

def transition(state)
  @state = state
  self.states << state
  self.save
  raise TransitionFired
end