super_state 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/super_state.rb +9 -0
- data/test/common_states_test.rb +3 -7
- data/test/super_state_test.rb +15 -0
- data/test/test_helper.rb +56 -0
- metadata +7 -7
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.
|
48
|
+
s.version = "0.3.0"
|
49
49
|
s.summary = "Super Simple State Machine"
|
50
50
|
s.author = "Matthew Rudy Jacobs"
|
51
51
|
s.email = "MatthewRudyJacobs@gmail.com"
|
data/lib/super_state.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'super_state/common_states'
|
2
|
+
require 'active_support/ordered_hash'
|
2
3
|
|
3
4
|
module SuperState
|
4
5
|
|
@@ -15,6 +16,8 @@ module SuperState
|
|
15
16
|
|
16
17
|
# the initial_state only takes effect when we say record.valid?
|
17
18
|
before_validation :set_initial_super_state, :on => :create
|
19
|
+
|
20
|
+
validate :ensure_super_state
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
@@ -102,6 +105,12 @@ module SuperState
|
|
102
105
|
self.current_super_state.humanize
|
103
106
|
end
|
104
107
|
|
108
|
+
def ensure_super_state
|
109
|
+
unless self.class.__super_states.include?(self.current_super_state)
|
110
|
+
errors[self.class.super_state_column] << "is not a valid super state"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
105
114
|
end
|
106
115
|
|
107
116
|
class BadState < ArgumentError ; end
|
data/test/common_states_test.rb
CHANGED
@@ -2,19 +2,15 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class CommonStatesTest < Test::Unit::TestCase
|
4
4
|
|
5
|
-
class MyClass < ActiveRecord::Base
|
6
|
-
include SuperState::CommonStates
|
7
|
-
end
|
8
|
-
|
9
5
|
# a DD::Batch is a simple example
|
10
6
|
def setup
|
11
|
-
@object =
|
7
|
+
@object = Commoner.create!()
|
12
8
|
assert @object.pending?
|
13
|
-
assert @object.class.ancestors.include?(CommonStates), "mate, it's not a CommonState thing"
|
9
|
+
assert @object.class.ancestors.include?(SuperState::CommonStates), "mate, it's not a CommonState thing"
|
14
10
|
end
|
15
11
|
|
16
12
|
test "a new record is pending?" do
|
17
|
-
@object =
|
13
|
+
@object = Commoner.new
|
18
14
|
assert @object.pending?
|
19
15
|
end
|
20
16
|
|
data/test/super_state_test.rb
CHANGED
@@ -2,6 +2,21 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class SuperStateTest < Test::Unit::TestCase
|
4
4
|
|
5
|
+
test "status must be valid" do
|
6
|
+
record = Something.new
|
7
|
+
assert record.valid?
|
8
|
+
|
9
|
+
record.save!
|
10
|
+
assert record.start?
|
11
|
+
|
12
|
+
record.status = "not_valid"
|
13
|
+
assert !record.valid?
|
14
|
+
assert_errors record, :status => "is not a valid super state"
|
15
|
+
|
16
|
+
record.status = "start"
|
17
|
+
assert record.valid?
|
18
|
+
end
|
19
|
+
|
5
20
|
test "abstract the tests" do
|
6
21
|
puts "this has been abstracted from a live project"
|
7
22
|
puts "as such the tests have not yet been abstracted"
|
data/test/test_helper.rb
CHANGED
@@ -3,6 +3,62 @@ require 'test/unit'
|
|
3
3
|
require 'active_support/testing/declarative'
|
4
4
|
$: << File.expand_path(File.dirname(__FILE__)+"/../lib")
|
5
5
|
|
6
|
+
require 'super_state'
|
7
|
+
|
8
|
+
require 'active_record'
|
9
|
+
require 'mocha'
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(
|
12
|
+
:adapter => (RUBY_PLATFORM=="java" ? "jdbcsqlite3" : "sqlite3"),
|
13
|
+
:database => ":memory:"
|
14
|
+
)
|
15
|
+
|
16
|
+
ActiveRecord::Schema.define(:version => 0) do
|
17
|
+
create_table :somethings, :force => true do |t|
|
18
|
+
t.string :status
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Something < ActiveRecord::Base
|
23
|
+
|
24
|
+
include SuperState
|
25
|
+
|
26
|
+
super_state :start, :initial => true
|
27
|
+
super_state :middle
|
28
|
+
super_state :end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::Schema.define(:version => 0) do
|
33
|
+
create_table :commoners, :force => true do |t|
|
34
|
+
t.string :status
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Commoner < ActiveRecord::Base
|
39
|
+
include SuperState::CommonStates
|
40
|
+
end
|
41
|
+
|
6
42
|
class Test::Unit::TestCase
|
7
43
|
extend ActiveSupport::Testing::Declarative
|
44
|
+
|
45
|
+
def before_and_after_reload(record)
|
46
|
+
yield
|
47
|
+
record.reload
|
48
|
+
yield
|
49
|
+
end
|
50
|
+
|
51
|
+
def assert_errors(record, fields)
|
52
|
+
fields.each do |field, errors|
|
53
|
+
assert_errors_on(errors, record, field)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def assert_errors_on(errors, record, field)
|
58
|
+
assert_equal Array(errors), Array(record.errors[field]), "unexpected errors on #{field.inspect}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def assert_no_errors_on(record, field)
|
62
|
+
assert_errors_on([], record, field)
|
63
|
+
end
|
8
64
|
end
|
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:
|
5
|
-
prerelease:
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.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:
|
18
|
+
date: 2011-03-15 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
requirements: []
|
68
68
|
|
69
69
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.5.2
|
71
71
|
signing_key:
|
72
72
|
specification_version: 3
|
73
73
|
summary: Super Simple State Machine
|