simple_states 0.1.0.pre2 → 0.1.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -47
- data/lib/simple_states.rb +1 -1
- data/lib/simple_states/version.rb +1 -1
- metadata +13 -14
data/README.md
CHANGED
@@ -7,28 +7,30 @@ Travis CI.
|
|
7
7
|
|
8
8
|
Define states and events like this:
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
``` ruby
|
11
|
+
class Foo
|
12
|
+
include SimpleStates
|
12
13
|
|
13
|
-
|
14
|
+
states :created, :started, :finished
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
event :start, :from => :created, :to => :started, :if => :startable?
|
17
|
+
event :finish, :to => :finished, :after => :cleanup
|
17
18
|
|
18
|
-
|
19
|
+
attr_accessor :state, :started_at, :finished_at
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def start
|
22
|
+
# start foo
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def startable?
|
26
|
+
true
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
def cleanup
|
30
|
+
# cleanup foo
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
32
34
|
|
33
35
|
Including the SimpleStates module to your class is currently required. We'll add
|
34
36
|
hooks for ActiveRecord etc later.
|
@@ -37,24 +39,26 @@ SimpleStates expects your model to support attribute accessors for `:state`.
|
|
37
39
|
|
38
40
|
Event options have the following well-known meanings:
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
``` ruby
|
43
|
+
:from # valid states to transition from
|
44
|
+
:to # target state to transition to
|
45
|
+
:if # only proceed if the given method returns true
|
46
|
+
:unless # only proceed if the given method returns false
|
47
|
+
:before # run the given method before running `super` and setting the new state
|
48
|
+
:after # run the given method at the very end
|
49
|
+
```
|
46
50
|
|
47
51
|
All of these options except for `:to` can be given as a single symbol or string or
|
48
52
|
as an Array of symbols or strings.
|
49
53
|
|
50
54
|
Calling `event` will effectively add methods to a proxy module which is
|
51
55
|
included to the singleton class of your class' instances. E.g. declaring `event
|
52
|
-
:start` in the example above will add
|
53
|
-
the singleton class of instances of `Foo`.
|
56
|
+
:start` in the example above will add methods `start` and `start!` to a module
|
57
|
+
included to the singleton class of instances of `Foo`.
|
54
58
|
|
55
59
|
This method will
|
56
60
|
|
57
|
-
1. check if `:if`/`:
|
61
|
+
1. check if `:if`/`:unless` conditions apply (if given) and just return from the method otherwise
|
58
62
|
2. check if the object currently is in a valid `:from` state (if given) and raise an exception otherwise
|
59
63
|
3. run `:before` callbacks (if given)
|
60
64
|
4. call `super` if Foo defines the current method (i.e. call `start` but not `finish` in the example above)
|
@@ -65,8 +69,10 @@ This method will
|
|
65
69
|
|
66
70
|
You can define options for all events like so:
|
67
71
|
|
68
|
-
|
69
|
-
|
72
|
+
``` ruby
|
73
|
+
event :finish, :to => :finished, :after => :cleanup
|
74
|
+
event :all, :after => :notify
|
75
|
+
```
|
70
76
|
|
71
77
|
This will call :cleanup first and then :notify on :finish.
|
72
78
|
|
@@ -78,31 +84,36 @@ way then it will raise an exception.
|
|
78
84
|
By default SimpleStates will assum `:created` as an initial state. You can
|
79
85
|
overwrite this using:
|
80
86
|
|
81
|
-
|
87
|
+
``` ruby
|
88
|
+
# note that we have to use self here!
|
89
|
+
self.initial_state = :some_state
|
90
|
+
```
|
82
91
|
|
83
92
|
So with the example above something the following would work:
|
84
93
|
|
85
|
-
|
94
|
+
``` ruby
|
95
|
+
foo = Foo.new
|
86
96
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
97
|
+
foo.state # :created
|
98
|
+
foo.created? # true
|
99
|
+
foo.was_created? # true
|
100
|
+
foo.state?(:created) # true
|
91
101
|
|
92
|
-
|
102
|
+
foo.start # checks Foo#startable? and then calls Foo#start
|
103
|
+
# calling foo.start! (with exclamation mark) would perform same actions as foo.start, but
|
104
|
+
# also call foo.save! afterwards.
|
93
105
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
foo.finish # just performs state logic as there's no Foo#finish
|
101
|
-
|
102
|
-
foo.state # :finished
|
103
|
-
foo.finished? # true
|
104
|
-
foo.finished_at # Time.now
|
105
|
-
foo.was_created? # true
|
106
|
-
foo.was_started? # true
|
106
|
+
foo.state # :started
|
107
|
+
foo.started? # true
|
108
|
+
foo.started_at # Time.now
|
109
|
+
foo.created? # false
|
110
|
+
foo.was_created? # true
|
107
111
|
|
112
|
+
foo.finish # just performs state logic as there's no Foo#finish
|
108
113
|
|
114
|
+
foo.state # :finished
|
115
|
+
foo.finished? # true
|
116
|
+
foo.finished_at # Time.now
|
117
|
+
foo.was_created? # true
|
118
|
+
foo.was_started? # true
|
119
|
+
```
|
data/lib/simple_states.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_states
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre3
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-04-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70262350435840 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70262350435840
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: hashr
|
27
|
-
requirement: &
|
27
|
+
requirement: &70262350434940 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.0.10
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70262350434940
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70262350434360 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.9.2
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70262350434360
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: test_declarative
|
49
|
-
requirement: &
|
49
|
+
requirement: &70262350433840 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70262350433840
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: mocha
|
60
|
-
requirement: &
|
60
|
+
requirement: &70262350433120 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70262350433120
|
69
69
|
description: ! '[description]'
|
70
70
|
email: svenfuchs@artweb-design.de
|
71
71
|
executables: []
|
@@ -102,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: 1.3.1
|
103
103
|
requirements: []
|
104
104
|
rubyforge_project: ! '[none]'
|
105
|
-
rubygems_version: 1.8.
|
105
|
+
rubygems_version: 1.8.11
|
106
106
|
signing_key:
|
107
107
|
specification_version: 3
|
108
108
|
summary: ! '[summary]'
|
109
109
|
test_files: []
|
110
|
-
has_rdoc:
|