stately 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +10 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +7 -1
- data/README.md +19 -17
- data/lib/stately/state.rb +4 -4
- data/lib/stately/version.rb +1 -1
- data/spec/functional/stately_spec.rb +11 -11
- data/spec/unit/stately/machine_spec.rb +4 -4
- data/spec/unit/stately/state_spec.rb +18 -11
- data/spec/unit/stately_spec.rb +1 -1
- data/stately.gemspec +3 -1
- metadata +26 -5
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
stately (0.
|
4
|
+
stately (0.3.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
8
8
|
specs:
|
9
9
|
diff-lcs (1.1.3)
|
10
|
+
json (1.8.0)
|
11
|
+
rake (10.0.4)
|
12
|
+
rdoc (4.0.1)
|
13
|
+
json (~> 1.4)
|
10
14
|
redcarpet (2.2.2)
|
11
15
|
rspec (2.11.0)
|
12
16
|
rspec-core (~> 2.11.0)
|
@@ -22,6 +26,8 @@ PLATFORMS
|
|
22
26
|
ruby
|
23
27
|
|
24
28
|
DEPENDENCIES
|
29
|
+
rake
|
30
|
+
rdoc (= 4.0.1)
|
25
31
|
redcarpet (~> 2.2.2)
|
26
32
|
rspec (~> 2.0)
|
27
33
|
stately!
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Stately
|
2
2
|
|
3
|
+
[![Build Status](https://api.travis-ci.org/rtwomey/stately.png)](https://travis-ci.org/rtwomey/stately)
|
4
|
+
|
3
5
|
A minimal, elegant state machine for your ruby objects.
|
4
6
|
|
5
7
|
![A stately fellow.](https://dl.dropbox.com/u/2754528/exquisite_cat.jpg "A stately fellow.")
|
@@ -10,12 +12,12 @@ Stately is a state machine for ruby objects, with an elegant, easy-to-read DSL.
|
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
class Order
|
13
|
-
stately start
|
15
|
+
stately :start => :processing do
|
14
16
|
state :completed do
|
15
17
|
prevent_from :refunded
|
16
18
|
|
17
|
-
before_transition from
|
18
|
-
after_transition do
|
19
|
+
before_transition :from => :processing, :do => :calculate_total
|
20
|
+
after_transition :do => :email_receipt
|
19
21
|
|
20
22
|
validate :validates_credit_card
|
21
23
|
end
|
@@ -27,7 +29,7 @@ class Order
|
|
27
29
|
state :refunded do
|
28
30
|
allow_from :completed
|
29
31
|
|
30
|
-
after_transition do
|
32
|
+
after_transition :do => :email_receipt
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -71,12 +73,12 @@ Be sure to run `bundle install` afterwards.
|
|
71
73
|
The first step is to add the following to your object:
|
72
74
|
|
73
75
|
```ruby
|
74
|
-
stately start
|
76
|
+
stately :start => :initial_state, :attr => :my_state_attr do
|
75
77
|
# ...
|
76
78
|
end
|
77
79
|
```
|
78
80
|
|
79
|
-
This sets up Stately to look for an attribute named `my_state_attr`, and initially set it to `initial_state`. If you omit
|
81
|
+
This sets up Stately to look for an attribute named `my_state_attr`, and initially set it to `initial_state`. If you omit `:attr => :my_state_attr`, Stately will automatically look for an attribute named `state`.
|
80
82
|
|
81
83
|
## Defining a state
|
82
84
|
|
@@ -84,8 +86,8 @@ States make up the core of Stately and define two things: the name of the state
|
|
84
86
|
|
85
87
|
```ruby
|
86
88
|
class Order
|
87
|
-
stately start
|
88
|
-
state :my_state, action
|
89
|
+
stately :start => :processing do
|
90
|
+
state :my_state, :action => transition_to_my_state
|
89
91
|
end
|
90
92
|
end
|
91
93
|
|
@@ -127,13 +129,13 @@ If you're using Stately with some kind of persistence layer, sych as activerecor
|
|
127
129
|
|
128
130
|
```ruby
|
129
131
|
class Order
|
130
|
-
stately start
|
132
|
+
stately :start => :processing do
|
131
133
|
# ...
|
132
134
|
|
133
135
|
state :completed do
|
134
|
-
before_transition from
|
135
|
-
before_transition from
|
136
|
-
after_transition do
|
136
|
+
before_transition :from => :processing, :do => :before_completed
|
137
|
+
before_transition :from => :invalid, :do => :cleanup_invalid
|
138
|
+
after_transition :do => :after_completed
|
137
139
|
end
|
138
140
|
end
|
139
141
|
|
@@ -155,9 +157,9 @@ Let's say you are modeling a Bicycle object for your rental shop and you're usin
|
|
155
157
|
|
156
158
|
```ruby
|
157
159
|
class Bicycle < ActiveRecord::Base
|
158
|
-
stately start
|
159
|
-
state :rented, action
|
160
|
-
after_transition do
|
160
|
+
stately :start => :available do
|
161
|
+
state :rented, :action => :rent do
|
162
|
+
after_transition :do => :save
|
161
163
|
end
|
162
164
|
end
|
163
165
|
end
|
@@ -169,8 +171,8 @@ As you can see, Stately is slightly more verbose than other state machine gems,
|
|
169
171
|
|
170
172
|
## Requirements
|
171
173
|
|
172
|
-
Stately requires Ruby 1.
|
174
|
+
Stately requires Ruby 1.8.7 or newer. If you'd like to contribute to Stately, you'll need Rspec 2.0+.
|
173
175
|
|
174
176
|
## License
|
175
177
|
|
176
|
-
Stately is Copyright ©
|
178
|
+
Stately is Copyright © 2013 Ryan Twomey. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.
|
data/lib/stately/state.rb
CHANGED
@@ -51,12 +51,12 @@ module Stately
|
|
51
51
|
|
52
52
|
private
|
53
53
|
|
54
|
-
ACTIONS = { completed
|
55
|
-
preparing
|
56
|
-
saving
|
54
|
+
ACTIONS = { :completed => :complete, :converting => :convert, :invalid => :invalidate,
|
55
|
+
:preparing => :prepare, :processing => :process, :refunded => :refund, :reticulating => :reticulate,
|
56
|
+
:saving => :save, :searching => :search, :started => :start, :stopped => :stop }
|
57
57
|
|
58
58
|
def guess_action_for(name)
|
59
|
-
ACTIONS
|
59
|
+
ACTIONS.fetch(name.to_sym, name)
|
60
60
|
end
|
61
61
|
|
62
62
|
class StateConfigurator
|
data/lib/stately/version.rb
CHANGED
@@ -4,13 +4,13 @@ require 'spec_helper'
|
|
4
4
|
describe Stately do
|
5
5
|
before do
|
6
6
|
@order_class = Class.new(OpenStruct) do
|
7
|
-
stately start
|
7
|
+
stately :start => :processing do
|
8
8
|
state :completed do
|
9
9
|
prevent_from :refunded
|
10
10
|
|
11
|
-
before_transition from
|
12
|
-
before_transition from
|
13
|
-
after_transition do
|
11
|
+
before_transition :from => :processing, :do => :before_completed
|
12
|
+
before_transition :from => :invalid, :do => :cleanup_invalid
|
13
|
+
after_transition :do => :after_completed
|
14
14
|
|
15
15
|
validate :validates_amount
|
16
16
|
validate :validates_credit_card
|
@@ -27,8 +27,8 @@ describe Stately do
|
|
27
27
|
state :refunded do
|
28
28
|
allow_from :completed
|
29
29
|
|
30
|
-
before_transition from
|
31
|
-
after_transition from
|
30
|
+
before_transition :from => :completed, :do => :before_refunded
|
31
|
+
after_transition :from => :completed, :do => :after_refunded
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -115,7 +115,7 @@ describe Stately do
|
|
115
115
|
|
116
116
|
def self.should_prevent_transition(from, to, action)
|
117
117
|
before do
|
118
|
-
@order = @order_class.new(amount
|
118
|
+
@order = @order_class.new(:amount => 99, :cc_number => 123)
|
119
119
|
@order.state = from
|
120
120
|
end
|
121
121
|
|
@@ -141,7 +141,7 @@ describe Stately do
|
|
141
141
|
|
142
142
|
describe 'initial state' do
|
143
143
|
before do
|
144
|
-
@order = @order_class.new(amount
|
144
|
+
@order = @order_class.new(:amount => 99, :cc_number => 123)
|
145
145
|
end
|
146
146
|
|
147
147
|
it 'creates actions for each state' do
|
@@ -179,7 +179,7 @@ describe Stately do
|
|
179
179
|
|
180
180
|
describe '#complete' do
|
181
181
|
before do
|
182
|
-
@order = @order_class.new(amount
|
182
|
+
@order = @order_class.new(:amount => 99, :cc_number => 123)
|
183
183
|
end
|
184
184
|
|
185
185
|
describe 'from processing' do
|
@@ -243,7 +243,7 @@ describe Stately do
|
|
243
243
|
describe '#invalidate' do
|
244
244
|
describe 'from processing' do
|
245
245
|
before do
|
246
|
-
@order = @order_class.new(amount
|
246
|
+
@order = @order_class.new(:amount => 99, :cc_number => 123)
|
247
247
|
@order.invalidate
|
248
248
|
end
|
249
249
|
|
@@ -272,7 +272,7 @@ describe Stately do
|
|
272
272
|
|
273
273
|
describe 'from completed' do
|
274
274
|
before do
|
275
|
-
@order = @order_class.new(amount
|
275
|
+
@order = @order_class.new(:amount => 99, :cc_number => 123)
|
276
276
|
@order.state = 'completed'
|
277
277
|
end
|
278
278
|
|
@@ -43,7 +43,7 @@ describe Stately::Machine do
|
|
43
43
|
describe 'with name and action' do
|
44
44
|
describe 'of a new state' do
|
45
45
|
before do
|
46
|
-
@machine.state(:new_state, action
|
46
|
+
@machine.state(:new_state, :action => :transition_to_new_state)
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'adds a new state' do
|
@@ -57,7 +57,7 @@ describe Stately::Machine do
|
|
57
57
|
|
58
58
|
describe 'of a previously defined state' do
|
59
59
|
before do
|
60
|
-
@machine.state(:processing, action
|
60
|
+
@machine.state(:processing, :action => :transition_to_processing)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "doesn't add a new state" do
|
@@ -73,7 +73,7 @@ describe Stately::Machine do
|
|
73
73
|
describe 'with name, action, and block' do
|
74
74
|
describe 'of a new state' do
|
75
75
|
before do
|
76
|
-
@machine.state(:new_state, action
|
76
|
+
@machine.state(:new_state, :action => :transition_to_new_state) do
|
77
77
|
allow_from :completed
|
78
78
|
end
|
79
79
|
|
@@ -95,7 +95,7 @@ describe Stately::Machine do
|
|
95
95
|
|
96
96
|
describe 'of a previously defined state' do
|
97
97
|
before do
|
98
|
-
@machine.state(:processing, action
|
98
|
+
@machine.state(:processing, :action => :transition_to_processing) do
|
99
99
|
allow_from :completed
|
100
100
|
end
|
101
101
|
|
@@ -2,6 +2,13 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Stately::State do
|
4
4
|
describe 'initialize' do
|
5
|
+
describe 'with an unrecognized action' do
|
6
|
+
it 'should not have an empty string for an action' do
|
7
|
+
state = Stately::State.new :pending
|
8
|
+
state.action.should == 'pending'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
5
12
|
describe 'with a block given' do
|
6
13
|
describe 'new' do
|
7
14
|
before do
|
@@ -9,10 +16,10 @@ describe Stately::State do
|
|
9
16
|
allow_from :completed
|
10
17
|
prevent_from :completed, :refunded
|
11
18
|
|
12
|
-
before_transition do
|
13
|
-
before_transition from
|
14
|
-
after_transition do
|
15
|
-
after_transition from
|
19
|
+
before_transition :do => :prepare
|
20
|
+
before_transition :from => :processing, :do => :before_completed
|
21
|
+
after_transition :do => :cleanup
|
22
|
+
after_transition :from => :processing, :do => :after_processing
|
16
23
|
|
17
24
|
validate :validates_amount
|
18
25
|
validate :validates_credit_card
|
@@ -25,10 +32,10 @@ describe Stately::State do
|
|
25
32
|
@state.allow_from_states.should == [:completed]
|
26
33
|
@state.prevent_from_states.should == [:completed, :refunded]
|
27
34
|
|
28
|
-
@state.before_transitions.should == [{do
|
29
|
-
do
|
30
|
-
@state.after_transitions.should == [{do
|
31
|
-
do
|
35
|
+
@state.before_transitions.should == [{:do => :prepare}, {:from => :processing,
|
36
|
+
:do => :before_completed}]
|
37
|
+
@state.after_transitions.should == [{:do => :cleanup}, {:from => :processing,
|
38
|
+
:do => :after_processing}]
|
32
39
|
@state.validations.should == [:validates_amount, :validates_credit_card]
|
33
40
|
end
|
34
41
|
end
|
@@ -64,9 +71,9 @@ describe Stately::State do
|
|
64
71
|
|
65
72
|
describe 'without a given action' do
|
66
73
|
before do
|
67
|
-
@actions = { completed
|
68
|
-
preparing
|
69
|
-
saving
|
74
|
+
@actions = { :completed => :complete, :converting => :convert, :invalid => :invalidate,
|
75
|
+
:preparing => :prepare, :processing => :process, :refunded => :refund, :reticulating => :reticulate,
|
76
|
+
:saving => :save, :searching => :search, :started => :start, :stopped => :stop }
|
70
77
|
end
|
71
78
|
|
72
79
|
it 'should set the correct action verb' do
|
data/spec/unit/stately_spec.rb
CHANGED
data/stately.gemspec
CHANGED
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.homepage = 'http://github.com/rtwomey/stately'
|
10
10
|
s.summary = 'A simple, elegant state machine for Ruby'
|
11
11
|
s.description = 'Add an elegant state machine to your ruby objects with a simple DSL'
|
12
|
+
s.license = 'MIT'
|
12
13
|
|
13
14
|
s.files = `git ls-files`.split("\n")
|
14
15
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
@@ -16,7 +17,8 @@ Gem::Specification.new do |s|
|
|
16
17
|
s.add_development_dependency 'redcarpet', '~> 2.2.2'
|
17
18
|
s.add_development_dependency 'rspec', '~> 2.0'
|
18
19
|
s.add_development_dependency 'yard', '~> 0.8.3'
|
20
|
+
s.add_development_dependency 'rdoc', '4.0.1'
|
19
21
|
|
20
|
-
s.required_ruby_version = Gem::Requirement.new('>= 1.
|
22
|
+
s.required_ruby_version = Gem::Requirement.new('>= 1.8.7')
|
21
23
|
s.require_paths = ['lib']
|
22
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stately
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redcarpet
|
@@ -59,6 +59,22 @@ dependencies:
|
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 0.8.3
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rdoc
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - '='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 4.0.1
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - '='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 4.0.1
|
62
78
|
description: Add an elegant state machine to your ruby objects with a simple DSL
|
63
79
|
email:
|
64
80
|
- rtwomey@gmail.com
|
@@ -68,6 +84,7 @@ extra_rdoc_files: []
|
|
68
84
|
files:
|
69
85
|
- .gitignore
|
70
86
|
- .rspec
|
87
|
+
- .travis.yml
|
71
88
|
- .yardopts
|
72
89
|
- Gemfile
|
73
90
|
- Gemfile.lock
|
@@ -87,7 +104,8 @@ files:
|
|
87
104
|
- spec/unit/stately_spec.rb
|
88
105
|
- stately.gemspec
|
89
106
|
homepage: http://github.com/rtwomey/stately
|
90
|
-
licenses:
|
107
|
+
licenses:
|
108
|
+
- MIT
|
91
109
|
post_install_message:
|
92
110
|
rdoc_options: []
|
93
111
|
require_paths:
|
@@ -97,16 +115,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
115
|
requirements:
|
98
116
|
- - ! '>='
|
99
117
|
- !ruby/object:Gem::Version
|
100
|
-
version: 1.
|
118
|
+
version: 1.8.7
|
101
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
120
|
none: false
|
103
121
|
requirements:
|
104
122
|
- - ! '>='
|
105
123
|
- !ruby/object:Gem::Version
|
106
124
|
version: '0'
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
hash: -4046456697569839142
|
107
128
|
requirements: []
|
108
129
|
rubyforge_project:
|
109
|
-
rubygems_version: 1.8.
|
130
|
+
rubygems_version: 1.8.25
|
110
131
|
signing_key:
|
111
132
|
specification_version: 3
|
112
133
|
summary: A simple, elegant state machine for Ruby
|