super_state 0.3.0 → 0.4.0

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/Rakefile CHANGED
@@ -45,7 +45,7 @@ spec = Gem::Specification.new do |s|
45
45
 
46
46
  # Change these as appropriate
47
47
  s.name = "super_state"
48
- s.version = "0.3.0"
48
+ s.version = "0.4.0"
49
49
  s.summary = "Super Simple State Machine"
50
50
  s.author = "Matthew Rudy Jacobs"
51
51
  s.email = "MatthewRudyJacobs@gmail.com"
@@ -17,7 +17,7 @@ module SuperState
17
17
  # the initial_state only takes effect when we say record.valid?
18
18
  before_validation :set_initial_super_state, :on => :create
19
19
 
20
- validate :ensure_super_state
20
+ validate :validate_super_state
21
21
  end
22
22
  end
23
23
 
@@ -105,12 +105,23 @@ module SuperState
105
105
  self.current_super_state.humanize
106
106
  end
107
107
 
108
- def ensure_super_state
108
+ def validate_super_state
109
109
  unless self.class.__super_states.include?(self.current_super_state)
110
110
  errors[self.class.super_state_column] << "is not a valid super state"
111
111
  end
112
112
  end
113
113
 
114
+ def ensure_super_state!(state_name)
115
+ self.transaction do
116
+ self.lock!
117
+
118
+ unless self.current_super_state == SuperState.canonicalise(state_name)
119
+ raise BadState, "the super state is not what we expected"
120
+ end
121
+
122
+ yield
123
+ end
124
+ end
114
125
  end
115
126
 
116
127
  class BadState < ArgumentError ; end
@@ -17,6 +17,37 @@ class SuperStateTest < Test::Unit::TestCase
17
17
  assert record.valid?
18
18
  end
19
19
 
20
+ test "ensure_super_state! - when the state has changed" do
21
+ record = Something.create!
22
+ assert record.start?
23
+
24
+ # lets suppose a 2nd process has already kicked this off
25
+
26
+ other = Something.find(record.id)
27
+ other.kick_off!
28
+
29
+ # but the original is still in the start state
30
+
31
+ assert record.start?
32
+
33
+ assert_raise SuperState::BadState do
34
+ record.ensure_super_state!(:start) do
35
+ record.kick_off!
36
+ end
37
+ end
38
+ end
39
+
40
+ test "ensure_super_state! - when the state is what we expect" do
41
+ record = Something.create!
42
+ assert record.start?
43
+
44
+ record.ensure_super_state!(:start) do
45
+ record.kick_off!
46
+ end
47
+ assert record.middle?
48
+ assert record.reload.middle?
49
+ end
50
+
20
51
  test "abstract the tests" do
21
52
  puts "this has been abstracted from a live project"
22
53
  puts "as such the tests have not yet been abstracted"
@@ -27,6 +27,8 @@ class Something < ActiveRecord::Base
27
27
  super_state :middle
28
28
  super_state :end
29
29
 
30
+ state_transition :kick_off, :start => :middle
31
+ state_transition :finish, :middle => :end
30
32
  end
31
33
 
32
34
  ActiveRecord::Schema.define(:version => 0) do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_state
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Rudy Jacobs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-15 00:00:00 +08:00
18
+ date: 2011-04-26 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21