simple_state_machine 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.3.4
data/examples/user.rb CHANGED
@@ -11,9 +11,9 @@ class User < ActiveRecord::Base
11
11
 
12
12
  def invite
13
13
  self.activation_code = Digest::SHA1.hexdigest("salt #{Time.now.to_f}")
14
- true
14
+ send_activation_email(self.activation_code)
15
15
  end
16
- event :invite, :new => :invited, :on_success => :send_activation_email
16
+ event :invite, :new => :invited
17
17
 
18
18
  def confirm_invitation activation_code
19
19
  if self.activation_code != activation_code
@@ -22,7 +22,15 @@ class User < ActiveRecord::Base
22
22
  end
23
23
  event :confirm_invitation, :invited => :active
24
24
 
25
- def send_activation_email
25
+ #event :log_send_activation_code_failed, :new => :send_activation_code_failed
26
+ #
27
+ # def reset_password(new_password)
28
+ # self.password = new_password
29
+ # end
30
+ # # do not change state, but ensure that we are in proper state
31
+ # event :reset_password, :active => :active
32
+
33
+ def send_activation_email(code)
26
34
  true
27
35
  end
28
36
 
@@ -8,6 +8,10 @@ module SimpleStateMachine::ActiveRecord
8
8
 
9
9
  class Decorator < SimpleStateMachine::Decorator
10
10
 
11
+ # decorates subject with:
12
+ # * {event_name}_and_save
13
+ # * {event_name}_and_save!
14
+ # * {event_name}!
11
15
  def decorate transition
12
16
  super transition
13
17
  event_name = transition.event_name.to_s
@@ -7,6 +7,7 @@ module SimpleStateMachine
7
7
  # Adds state machine methods to extended class
8
8
  module StateMachineMixin
9
9
 
10
+ # mark the method as an event and specify how the state should transition
10
11
  def event event_name, state_transitions
11
12
  state_transitions.each do |from, to|
12
13
  transition = state_machine_definition.add_transition(event_name, from, to)
@@ -59,18 +60,21 @@ module SimpleStateMachine
59
60
  end
60
61
 
61
62
  ##
62
- # The state machine used by the instance
63
+ # Defines the state machine used by the instance
63
64
  class StateMachine
64
65
 
65
66
  def initialize(subject)
66
67
  @subject = subject
67
68
  end
68
-
69
+
70
+ # returns the next state for the subject for event_name
69
71
  def next_state(event_name)
70
72
  transition = transitions.select{|t| t.event_name.to_s == event_name.to_s && @subject.send(state_method).to_s == t.from.to_s}.first
71
73
  transition ? transition.to : nil
72
74
  end
73
-
75
+
76
+ # transitions to the next state if next_state exists
77
+ # calls illegal_event_callback event_name if no next_state is found
74
78
  def transition(event_name)
75
79
  if to = next_state(event_name)
76
80
  result = yield
@@ -105,8 +109,8 @@ module SimpleStateMachine
105
109
  state_machine_definition.state_method
106
110
  end
107
111
 
112
+ # override with your own implementation, like setting errors in your model
108
113
  def illegal_event_callback event_name
109
- # override with your own implementation, like setting errors in your model
110
114
  raise Error.new("You cannot '#{event_name}' when state is '#{@subject.state}'")
111
115
  end
112
116
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_state_machine}
8
- s.version = "0.3.3"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marek de Heus", "Petrik de Heus"]
12
- s.date = %q{2010-09-03}
12
+ s.date = %q{2010-09-04}
13
13
  s.description = %q{A simple DSL to decorate existing methods with logic that guards state transitions.}
14
14
  s.email = ["FIX@example.com"]
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 3
10
- version: 0.3.3
9
+ - 4
10
+ version: 0.3.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marek de Heus
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-09-03 00:00:00 +02:00
19
+ date: 2010-09-04 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency