simple_state_machine 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ - allow other state methods
2
+ - alias ..._and_save with ...!
3
+ - Document after_initialize bug
4
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -14,11 +14,16 @@ module SimpleStateMachine::ActiveRecord
14
14
  @subject.send(:define_method, "#{event_name}_and_save") do |*args|
15
15
  old_state = state
16
16
  send "#{event_name}", *args
17
- if save
18
- return true
19
- else
17
+ if !self.errors.entries.empty?
20
18
  self.state = old_state
21
19
  return false
20
+ else
21
+ if save
22
+ return true
23
+ else
24
+ self.state = old_state
25
+ return false
26
+ end
22
27
  end
23
28
  end
24
29
  end
@@ -5,16 +5,17 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{simple_state_machine}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
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-08-12}
12
+ s.date = %q{2010-08-29}
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 = [
16
16
  "LICENSE",
17
- "README.rdoc"
17
+ "README.rdoc",
18
+ "TODO"
18
19
  ]
19
20
  s.files = [
20
21
  "LICENSE",
@@ -42,7 +42,7 @@ describe User do
42
42
 
43
43
  # TODO needs nesting/grouping, seems to have some duplication
44
44
 
45
- describe "events" do
45
+ describe "and_save" do
46
46
  it "persists transitions" do
47
47
  user = User.create!(:name => 'name')
48
48
  user.invite_and_save.should == true
@@ -50,77 +50,62 @@ describe User do
50
50
  User.find(user.id).activation_code.should_not be_nil
51
51
  end
52
52
 
53
- it "persists transitions with !" do
54
- user = User.create!(:name => 'name')
55
- user.invite_and_save!.should == true
56
- User.find(user.id).should be_invited
57
- User.find(user.id).activation_code.should_not be_nil
58
- end
59
-
60
53
  it "raises an error if an invalid state_transition is called" do
61
54
  user = User.create!(:name => 'name')
62
55
  l = lambda { user.confirm_invitation_and_save 'abc' }
63
56
  l.should raise_error(RuntimeError, "You cannot 'confirm_invitation' when state is 'new'")
64
57
  end
65
58
 
66
- it "returns false if event called and record is invalid" do
59
+ it "returns false and keeps state if record is invalid" do
67
60
  user = User.new
68
- user.should_not be_valid
69
- user.invite_and_save.should == false
70
- end
71
-
72
- it "keeps state if event called and record is invalid" do
73
- user = User.new
74
- user.should_not be_valid
75
- user.invite_and_save.should == false
76
61
  user.should be_new
77
- end
78
-
79
- it "raises a RecordInvalid event if called with ! and record is invalid" do
80
- user = User.new
81
- user.should_not be_valid
82
- l = lambda { user.invite_and_save! }
83
- l.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
84
- end
85
-
86
- it "keeps state if event! called and record is invalid" do
87
- user = User.new
88
62
  user.should_not be_valid
89
- begin
90
- user.invite_and_save!
91
- rescue ActiveRecord::RecordInvalid;end
63
+ user.invite_and_save.should == false
92
64
  user.should be_new
93
65
  end
94
66
 
95
- it "will inspect errors after event and reset state" do
67
+ it "returns false, keeps state and keeps errors if event adds errors" do
96
68
  user = User.create!(:name => 'name')
97
69
  user.invite_and_save!
98
70
  user.should be_invited
99
- user.confirm_invitation('x')
100
- user.errors.entries.should == [['activation_code', 'is invalid']]
71
+ user.confirm_invitation_and_save('x').should == false
101
72
  user.should be_invited
73
+ user.errors.entries.should == [['activation_code', 'is invalid']]
102
74
  end
103
75
 
104
- it "returns false if event adds errors" do
76
+ end
77
+
78
+ describe "and_save!" do
79
+
80
+ it "persists transitions" do
105
81
  user = User.create!(:name => 'name')
106
- user.invite_and_save!
107
- user.confirm_invitation('x').should == false
82
+ user.invite_and_save!.should == true
83
+ User.find(user.id).should be_invited
84
+ User.find(user.id).activation_code.should_not be_nil
108
85
  end
109
86
 
110
- it "raises a RecordInvalid if 'event'_and_save! is called and event adds errors" do
87
+ it "raises an error if an invalid state_transition is called" do
111
88
  user = User.create!(:name => 'name')
112
- user.invite_and_save!
113
- l = lambda { user.confirm_invitation_and_save!('x') }
114
- l.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Activation code is invalid")
89
+ l = lambda { user.confirm_invitation_and_save! 'abc' }
90
+ l.should raise_error(RuntimeError, "You cannot 'confirm_invitation' when state is 'new'")
91
+ end
92
+
93
+
94
+ it "raises a RecordInvalid and keeps state if record is invalid" do
95
+ user = User.new
96
+ user.should be_new
97
+ user.should_not be_valid
98
+ l = lambda { user.invite_and_save! }
99
+ l.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
100
+ user.should be_new
115
101
  end
116
102
 
117
- it "keeps state if record is valid but event adds errors" do
103
+ it "raises a RecordInvalid and keeps state if event adds errors" do
118
104
  user = User.create!(:name => 'name')
119
105
  user.invite_and_save!
120
106
  user.should be_invited
121
- begin
122
- user.confirm_invitation_and_save!('x')
123
- rescue ActiveRecord::RecordInvalid;end
107
+ l = lambda { user.confirm_invitation_and_save!('x') }
108
+ l.should raise_error(ActiveRecord::RecordInvalid, "Validation failed: Activation code is invalid")
124
109
  user.should be_invited
125
110
  end
126
111
 
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: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
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-08-12 00:00:00 +02:00
19
+ date: 2010-08-29 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ extensions: []
45
45
  extra_rdoc_files:
46
46
  - LICENSE
47
47
  - README.rdoc
48
+ - TODO
48
49
  files:
49
50
  - LICENSE
50
51
  - README.rdoc
@@ -65,6 +66,7 @@ files:
65
66
  - spec/simple_state_machine_spec.rb
66
67
  - spec/spec.opts
67
68
  - spec/spec_helper.rb
69
+ - TODO
68
70
  has_rdoc: true
69
71
  homepage: http://github.com/p8/simple_state_machine
70
72
  licenses: []