simple_states 2.0.0 → 2.0.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +12 -13
- data/README.md +24 -84
- data/lib/simple_states/event.rb +5 -1
- data/lib/simple_states/version.rb +1 -1
- metadata +8 -10
- data/simple_states.gemspec +0 -20
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a68d333f828efbb2ced448294618c846da08b4d1
|
|
4
|
+
data.tar.gz: ea6fbcdb518fed14d6883ef44c8c887f3e63b58b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3137b1e0780c03f5a045c4e1df7e381e6a7b7eec8bdf5a2317c979694b097f92f6fe56ba92191cc127eb086916809578a63b1af6c1f362abf9ebb569dbb1661
|
|
7
|
+
data.tar.gz: 795e118a9f6a5cb73d673b7d7bf393ac5bc7ade2039136a45ad2d5264f907981cb7839a6800fc810faf471fa45af3eb4ee4ec4fbf3c79c15b6a4d9260db0c588
|
data/Gemfile.lock
CHANGED
|
@@ -5,22 +5,21 @@ GEM
|
|
|
5
5
|
metaclass (0.0.4)
|
|
6
6
|
mocha (1.1.0)
|
|
7
7
|
metaclass (~> 0.0.1)
|
|
8
|
-
rspec (3.
|
|
9
|
-
rspec-core (~> 3.
|
|
10
|
-
rspec-expectations (~> 3.
|
|
11
|
-
rspec-mocks (~> 3.
|
|
12
|
-
rspec-core (3.
|
|
13
|
-
rspec-support (~> 3.
|
|
14
|
-
rspec-expectations (3.
|
|
8
|
+
rspec (3.4.0)
|
|
9
|
+
rspec-core (~> 3.4.0)
|
|
10
|
+
rspec-expectations (~> 3.4.0)
|
|
11
|
+
rspec-mocks (~> 3.4.0)
|
|
12
|
+
rspec-core (3.4.4)
|
|
13
|
+
rspec-support (~> 3.4.0)
|
|
14
|
+
rspec-expectations (3.4.0)
|
|
15
15
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
16
|
-
rspec-support (~> 3.
|
|
17
|
-
rspec-mocks (3.
|
|
16
|
+
rspec-support (~> 3.4.0)
|
|
17
|
+
rspec-mocks (3.4.1)
|
|
18
18
|
diff-lcs (>= 1.2.0, < 2.0)
|
|
19
|
-
rspec-support (~> 3.
|
|
20
|
-
rspec-support (3.
|
|
19
|
+
rspec-support (~> 3.4.0)
|
|
20
|
+
rspec-support (3.4.1)
|
|
21
21
|
|
|
22
22
|
PLATFORMS
|
|
23
|
-
java
|
|
24
23
|
ruby
|
|
25
24
|
|
|
26
25
|
DEPENDENCIES
|
|
@@ -28,4 +27,4 @@ DEPENDENCIES
|
|
|
28
27
|
rspec
|
|
29
28
|
|
|
30
29
|
BUNDLED WITH
|
|
31
|
-
1.
|
|
30
|
+
1.11.2
|
data/README.md
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
A super-slim (~200 loc) statemachine-like support library focussed on use in
|
|
4
4
|
Travis CI.
|
|
5
5
|
|
|
6
|
+
Note that the current version behaves slightly differently, and comes with
|
|
7
|
+
reduced features compared to the original version. If you are looking for the
|
|
8
|
+
original version see the tag `v1.1.0.rc11`.
|
|
9
|
+
|
|
6
10
|
## Usage
|
|
7
11
|
|
|
8
12
|
Define states and events like this:
|
|
@@ -11,10 +15,8 @@ Define states and events like this:
|
|
|
11
15
|
class Foo
|
|
12
16
|
include SimpleStates
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
event :start, :from => :created, :to => :started, :if => :startable?
|
|
17
|
-
event :finish, :to => :finished, :after => :cleanup
|
|
18
|
+
event :start, if: :start?
|
|
19
|
+
event :finish, to: [:passed, :failed], after: :notify, unless: :finished?
|
|
18
20
|
|
|
19
21
|
attr_accessor :state, :started_at, :finished_at
|
|
20
22
|
|
|
@@ -22,119 +24,57 @@ class Foo
|
|
|
22
24
|
# start foo
|
|
23
25
|
end
|
|
24
26
|
|
|
25
|
-
def
|
|
27
|
+
def start?
|
|
26
28
|
true
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
def
|
|
30
|
-
#
|
|
31
|
+
def notify(event)
|
|
32
|
+
# notify about event on foo
|
|
31
33
|
end
|
|
32
34
|
end
|
|
33
35
|
```
|
|
34
36
|
|
|
35
|
-
Including the SimpleStates module to your class is currently required. We'll add
|
|
36
|
-
hooks for ActiveRecord etc later.
|
|
37
|
-
|
|
38
37
|
SimpleStates expects your model to support attribute accessors for `:state`.
|
|
39
38
|
|
|
40
39
|
Event options have the following well-known meanings:
|
|
41
40
|
|
|
42
41
|
``` ruby
|
|
43
|
-
:
|
|
44
|
-
:to # target state to transition to
|
|
42
|
+
:to # allowed target states to transition to, deferred from the event name if not given
|
|
45
43
|
:if # only proceed if the given method returns true
|
|
46
44
|
:unless # only proceed if the given method returns false
|
|
47
45
|
:before # run the given method before running `super` and setting the new state
|
|
48
46
|
:after # run the given method at the very end
|
|
49
47
|
```
|
|
50
48
|
|
|
51
|
-
All of these options except
|
|
52
|
-
|
|
49
|
+
All of these options except can be given as a single symbol or string or as an
|
|
50
|
+
Array of symbols or strings.
|
|
53
51
|
|
|
54
52
|
Calling `event` will effectively add methods to a proxy module which is
|
|
55
|
-
included to the singleton class of your class'
|
|
56
|
-
:start` in the example above will add
|
|
57
|
-
included to the singleton class of
|
|
53
|
+
prepended to your class (included to the singleton class of your class'
|
|
54
|
+
instances on 1.9). E.g. declaring `event :start` in the example above will add
|
|
55
|
+
methods `start` and `start!` to a module included to the singleton class of
|
|
56
|
+
instances of `Foo`.
|
|
58
57
|
|
|
59
58
|
This method will
|
|
60
59
|
|
|
61
60
|
1. check if `:if`/`:unless` conditions apply (if given) and just return from the method otherwise
|
|
62
|
-
2.
|
|
63
|
-
3.
|
|
64
|
-
4.
|
|
65
|
-
5.
|
|
66
|
-
6.
|
|
67
|
-
7. set the object's `[state]_at` attribute to `Time.now` if the object defines a writer for it
|
|
68
|
-
8. run `:after` callbacks (if given)
|
|
61
|
+
2. run `:before` callbacks (if given)
|
|
62
|
+
3. set the object's `state` to the target state
|
|
63
|
+
4. set the object's `[state]_at` attribute to `Time.now` if the object defines a writer for it
|
|
64
|
+
5. call `super` if Foo defines the current method (i.e. call `start` but not `finish` in the example above)
|
|
65
|
+
6. run `:after` callbacks (if given)
|
|
69
66
|
|
|
70
67
|
You can define options for all events like so:
|
|
71
68
|
|
|
72
69
|
``` ruby
|
|
73
|
-
event :finish, :
|
|
74
|
-
event :all,
|
|
70
|
+
event :finish, after: :cleanup
|
|
71
|
+
event :all, after: :notify
|
|
75
72
|
```
|
|
76
73
|
|
|
77
74
|
This will call :cleanup first and then :notify on :finish.
|
|
78
75
|
|
|
79
76
|
If no target state was given for an event then SimpleStates will try to derive
|
|
80
|
-
it from the
|
|
77
|
+
it from the event name. I.e. for an event `start` it will check the states
|
|
81
78
|
list for a state `started` and use it. If it can not find a target state this
|
|
82
79
|
way then it will raise an exception.
|
|
83
80
|
|
|
84
|
-
By default SimpleStates will assume `:created` as an initial state. You can
|
|
85
|
-
overwrite this using:
|
|
86
|
-
|
|
87
|
-
``` ruby
|
|
88
|
-
# note that we have to use self here!
|
|
89
|
-
self.initial_state = :some_state
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
So with the example above something the following would work:
|
|
93
|
-
|
|
94
|
-
``` ruby
|
|
95
|
-
foo = Foo.new
|
|
96
|
-
|
|
97
|
-
foo.state # :created
|
|
98
|
-
foo.created? # true
|
|
99
|
-
foo.was_created? # true
|
|
100
|
-
foo.state?(:created) # true
|
|
101
|
-
|
|
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.
|
|
105
|
-
|
|
106
|
-
foo.state # :started
|
|
107
|
-
foo.started? # true
|
|
108
|
-
foo.started_at # Time.now
|
|
109
|
-
foo.created? # false
|
|
110
|
-
foo.was_created? # true
|
|
111
|
-
|
|
112
|
-
foo.finish # just performs state logic as there's no Foo#finish
|
|
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
|
-
```
|
|
120
|
-
|
|
121
|
-
In order to treat states as "ordered", the option `ordered: true` can be
|
|
122
|
-
passed. Events will still behave the same, callbacks be called, etc. However,
|
|
123
|
-
the `state` attribute will never be set back to a previous state.
|
|
124
|
-
|
|
125
|
-
For example:
|
|
126
|
-
|
|
127
|
-
```
|
|
128
|
-
class Foo
|
|
129
|
-
include SimpleStates
|
|
130
|
-
|
|
131
|
-
states :created, :started, :finished, ordered: true
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
foo = Foo.new
|
|
135
|
-
foo.finish
|
|
136
|
-
foo.start
|
|
137
|
-
|
|
138
|
-
p foo.state
|
|
139
|
-
# => :finished
|
|
140
|
-
```
|
data/lib/simple_states/event.rb
CHANGED
|
@@ -36,7 +36,11 @@ module SimpleStates
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
def set_attr(obj, key, value)
|
|
39
|
-
obj.send(:"#{key}=", value) if obj.respond_to?(:"#{key}=")
|
|
39
|
+
obj.send(:"#{key}=", value) if obj.respond_to?(:"#{key}=") and not has_timestamp?(obj, key)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def has_timestamp?(obj, key)
|
|
43
|
+
key.to_s.end_with?('_at') && obj.respond_to?(key) && obj.send(key)
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def ordered?(obj, data)
|
metadata
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_states
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.
|
|
4
|
+
version: 2.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sven Fuchs
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-04-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description:
|
|
13
|
+
description: '[description]'
|
|
14
14
|
email: me@svenfuchs.com
|
|
15
15
|
executables: []
|
|
16
16
|
extensions: []
|
|
@@ -27,7 +27,6 @@ files:
|
|
|
27
27
|
- lib/simple_states/helpers.rb
|
|
28
28
|
- lib/simple_states/states.rb
|
|
29
29
|
- lib/simple_states/version.rb
|
|
30
|
-
- simple_states.gemspec
|
|
31
30
|
homepage: https://github.com/svenfuchs/simple_states
|
|
32
31
|
licenses:
|
|
33
32
|
- MIT
|
|
@@ -38,19 +37,18 @@ require_paths:
|
|
|
38
37
|
- lib
|
|
39
38
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
39
|
requirements:
|
|
41
|
-
- -
|
|
40
|
+
- - '>='
|
|
42
41
|
- !ruby/object:Gem::Version
|
|
43
42
|
version: '0'
|
|
44
43
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
44
|
requirements:
|
|
46
|
-
- -
|
|
45
|
+
- - '>='
|
|
47
46
|
- !ruby/object:Gem::Version
|
|
48
47
|
version: '0'
|
|
49
48
|
requirements: []
|
|
50
|
-
rubyforge_project:
|
|
51
|
-
rubygems_version: 2.4.
|
|
49
|
+
rubyforge_project: '[none]'
|
|
50
|
+
rubygems_version: 2.4.8
|
|
52
51
|
signing_key:
|
|
53
52
|
specification_version: 4
|
|
54
|
-
summary:
|
|
53
|
+
summary: '[summary]'
|
|
55
54
|
test_files: []
|
|
56
|
-
has_rdoc:
|
data/simple_states.gemspec
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
|
|
3
|
-
$:.unshift File.expand_path('../lib', __FILE__)
|
|
4
|
-
require 'simple_states/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |s|
|
|
7
|
-
s.name = "simple_states"
|
|
8
|
-
s.version = SimpleStates::VERSION
|
|
9
|
-
s.authors = ["Sven Fuchs"]
|
|
10
|
-
s.email = "me@svenfuchs.com"
|
|
11
|
-
s.homepage = "https://github.com/svenfuchs/simple_states"
|
|
12
|
-
s.licenses = ['MIT']
|
|
13
|
-
s.summary = "[summary]"
|
|
14
|
-
s.description = "[description]"
|
|
15
|
-
|
|
16
|
-
s.files = Dir.glob("{lib/**/*,[A-Z]*}")
|
|
17
|
-
s.platform = Gem::Platform::RUBY
|
|
18
|
-
s.require_path = 'lib'
|
|
19
|
-
s.rubyforge_project = '[none]'
|
|
20
|
-
end
|