simple_state_machine 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -83,12 +83,14 @@ To add a state machine with ActiveRecord persistence:
83
83
  class User < ActiveRecord::Base
84
84
 
85
85
  extend SimpleStateMachine::ActiveRecord
86
-
86
+ # define a custum state_method (state is default)
87
+ state_machine_definition.state_method = :ssm_state
88
+
87
89
  def after_initialize
88
- self.state ||= 'new'
90
+ self.ssm_state ||= 'new'
89
91
  # if you get an ActiveRecord::MissingAttributeError
90
92
  # you'll probably need to do (http://bit.ly/35q23b):
91
- # write_attribute(:state, "new") unless read_attribute(:state)
93
+ # write_attribute(:ssm_state, "new") unless read_attribute(:ssm_state)
92
94
  end
93
95
 
94
96
  def invite
@@ -104,6 +106,8 @@ To add a state machine with ActiveRecord persistence:
104
106
  end
105
107
  event :confirm_invitation, :invited => :active
106
108
 
109
+ # :all can be used to catch all from states
110
+ event :suspend, :all => :suspended
107
111
  end
108
112
 
109
113
  This generates the following methods
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.4.3
@@ -28,7 +28,6 @@ class Conversation
28
28
  end
29
29
 
30
30
  event :view, :unread => :read
31
- event :close, :unread => :closed,
32
- :read => :closed
31
+ event :close, :all => :closed
33
32
 
34
- end
33
+ end
@@ -71,7 +71,7 @@ module SimpleStateMachine
71
71
 
72
72
  # Returns the next state for the subject for event_name
73
73
  def next_state(event_name)
74
- transition = transitions.select{|t| t.is_transition_for?(event_name) && @subject.send(state_method).to_s == t.from.to_s}.first
74
+ transition = transitions.select{|t| t.is_transition_for?(event_name, @subject.send(state_method))}.first
75
75
  transition ? transition.to : nil
76
76
  end
77
77
 
@@ -144,14 +144,24 @@ module SimpleStateMachine
144
144
  end
145
145
 
146
146
  # returns true if it's a transition for event_name
147
- def is_transition_for?(event_name)
148
- self.event_name == event_name.to_s
147
+ def is_transition_for?(event_name, subject_state)
148
+ is_same_event?(event_name) && is_same_from?(subject_state)
149
149
  end
150
150
 
151
151
  # returns true if it's a error transition for event_name and error
152
152
  def is_error_transition_for?(event_name, error)
153
- is_transition_for?(event_name) && error.class == from
153
+ is_same_event?(event_name) && error.class == from
154
154
  end
155
+
156
+ private
157
+
158
+ def is_same_event?(event_name)
159
+ self.event_name == event_name.to_s
160
+ end
161
+
162
+ def is_same_from?(subject_from)
163
+ from.to_s == 'all' || subject_from.to_s == from.to_s
164
+ end
155
165
  end
156
166
 
157
167
  ##
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_state_machine}
8
- s.version = "0.4.2"
8
+ s.version = "0.4.3"
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"]
@@ -13,6 +13,7 @@ class SimpleExample
13
13
  end
14
14
  event :event2, :state2 => :state3
15
15
  event :event_with_multiple_from, [:state1, :state2] => :state3
16
+ event :event_from_all, :all => :state3
16
17
  end
17
18
 
18
19
  class SimpleExampleWithCustomStateMethod
@@ -57,6 +58,16 @@ describe SimpleStateMachine do
57
58
  example.should be_state3
58
59
  end
59
60
 
61
+ it "changes state if event has all as from" do
62
+ example = SimpleExample.new
63
+ example.event_from_all
64
+ example.should be_state3
65
+ example = SimpleExample.new 'state2'
66
+ example.should be_state2
67
+ example.event_from_all
68
+ example.should be_state3
69
+ end
70
+
60
71
  it "changes state when state is a symbol instead of a string" do
61
72
  example = SimpleExample.new :state1
62
73
  example.state.should == :state1
@@ -100,6 +111,15 @@ describe SimpleStateMachine do
100
111
 
101
112
  describe 'custom state method' do
102
113
 
114
+ it "changes state when calling events" do
115
+ example = SimpleExampleWithCustomStateMethod.new
116
+ example.should be_state1
117
+ example.event1
118
+ example.should be_state2
119
+ example.event2
120
+ example.should be_state3
121
+ end
122
+
103
123
  it "raise an error if an invalid state_transition is called" do
104
124
  example = SimpleExampleWithCustomStateMethod.new
105
125
  lambda { example.event2 }.should raise_error(SimpleStateMachine::Error, "You cannot 'event2' when state is 'state1'")
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: 11
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 2
10
- version: 0.4.2
9
+ - 3
10
+ version: 0.4.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marek de Heus