dm-is-state_machine 0.9.11 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{History.txt → History.rdoc} +4 -0
- data/Manifest.txt +2 -3
- data/{README.markdown → README.rdoc} +27 -27
- data/Rakefile +2 -3
- data/lib/dm-is-state_machine/is/state_machine.rb +2 -2
- data/lib/dm-is-state_machine/is/version.rb +1 -1
- data/lib/dm-is-state_machine.rb +8 -25
- data/spec/examples/slot_machine.rb +1 -1
- data/spec/examples/traffic_light.rb +0 -9
- data/spec/integration/invalid_events_spec.rb +2 -3
- data/spec/integration/invalid_states_spec.rb +2 -3
- data/spec/integration/invalid_transitions_spec.rb +3 -4
- data/spec/integration/slot_machine_spec.rb +2 -3
- data/spec/integration/traffic_light_spec.rb +8 -9
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -5
- data/spec/unit/data/event_spec.rb +1 -2
- data/spec/unit/data/machine_spec.rb +1 -2
- data/spec/unit/data/state_spec.rb +1 -2
- data/spec/unit/dsl/event_dsl_spec.rb +1 -2
- data/spec/unit/dsl/state_dsl_spec.rb +1 -2
- data/spec/unit/state_machine_spec.rb +1 -7
- data/tasks/install.rb +1 -1
- data/tasks/spec.rb +4 -4
- metadata +14 -23
- data/README.txt +0 -5
data/Manifest.txt
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
= dm-is-state_machine
|
2
2
|
|
3
3
|
DataMapper plugin that adds state machine functionality to your models.
|
4
4
|
|
5
|
-
|
5
|
+
== Why is this plugin useful?
|
6
6
|
|
7
7
|
Your DataMapper resource might benefit from a state machine if it:
|
8
8
|
|
@@ -14,46 +14,46 @@ And you want a clean, high-level way of describing these modes / behaviors
|
|
14
14
|
and how the resource moves between them. This plugin allows you to
|
15
15
|
declaratively describe the states and transitions involved.
|
16
16
|
|
17
|
-
|
17
|
+
== Installation
|
18
18
|
|
19
19
|
1. Download dm-more.
|
20
20
|
2. Install dm-is-state_machine using the supplied rake files.
|
21
21
|
|
22
|
-
|
22
|
+
== Setting up with Merb ##
|
23
23
|
|
24
24
|
Add this line to your init.rb:
|
25
25
|
|
26
|
-
|
26
|
+
dependency "dm-is-state_machine"
|
27
27
|
|
28
28
|
## Example DataMapper resource (i.e. model) ##
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
# /app/models/traffic_light.rb
|
31
|
+
class TrafficLight
|
32
|
+
include DataMapper::Resource
|
33
33
|
|
34
|
-
|
34
|
+
property :id, Serial
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
is :state_machine, :initial => :green, :column => :color do
|
37
|
+
state :green
|
38
|
+
state :yellow
|
39
|
+
state :red, :enter => :red_hook
|
40
|
+
state :broken
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
42
|
+
event :forward do
|
43
|
+
transition :from => :green, :to => :yellow
|
44
|
+
transition :from => :yellow, :to => :red
|
45
|
+
transition :from => :red, :to => :green
|
47
46
|
end
|
47
|
+
end
|
48
48
|
|
49
|
-
|
50
|
-
|
51
|
-
end
|
49
|
+
def red_hook
|
50
|
+
# Do something
|
52
51
|
end
|
52
|
+
end
|
53
53
|
|
54
|
-
|
54
|
+
== What this gives you
|
55
55
|
|
56
|
-
|
56
|
+
=== Explained in words
|
57
57
|
|
58
58
|
The above DSL (domain specific language) does these things "behind the scenes":
|
59
59
|
|
@@ -67,7 +67,7 @@ The above DSL (domain specific language) does these things "behind the scenes":
|
|
67
67
|
it with an invalid state (such as :broken, see above). After the method
|
68
68
|
runs successfully, the state machine will be left in the :to state.
|
69
69
|
|
70
|
-
|
70
|
+
=== Explained with some code examples
|
71
71
|
|
72
72
|
# Somewhere in your controller, perhaps
|
73
73
|
light = TrafficLight.new
|
@@ -85,11 +85,11 @@ The above DSL (domain specific language) does these things "behind the scenes":
|
|
85
85
|
# do something red-related
|
86
86
|
end
|
87
87
|
|
88
|
-
|
88
|
+
== Specific examples
|
89
89
|
|
90
90
|
We would also like to hear how *you* are using state machines in your code.
|
91
91
|
|
92
|
-
|
92
|
+
== See also
|
93
93
|
|
94
94
|
Here are some other projects you might want to look at. Most of them
|
95
95
|
are probably intended for ActiveRecord. They take different approaches,
|
data/Rakefile
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'pathname'
|
2
|
-
require 'rubygems'
|
3
2
|
|
4
3
|
ROOT = Pathname(__FILE__).dirname.expand_path
|
5
4
|
JRUBY = RUBY_PLATFORM =~ /java/
|
@@ -14,10 +13,10 @@ GEM_NAME = 'dm-is-state_machine'
|
|
14
13
|
GEM_VERSION = DataMapper::Is::StateMachine::VERSION
|
15
14
|
GEM_DEPENDENCIES = [['dm-core', GEM_VERSION]]
|
16
15
|
GEM_CLEAN = %w[ log pkg coverage ]
|
17
|
-
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.
|
16
|
+
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.rdoc LICENSE TODO History.rdoc] }
|
18
17
|
|
19
18
|
PROJECT_NAME = 'datamapper'
|
20
|
-
PROJECT_URL = "http://github.com/
|
19
|
+
PROJECT_URL = "http://github.com/datamapper/dm-more/tree/master/#{GEM_NAME}"
|
21
20
|
PROJECT_DESCRIPTION = PROJECT_SUMMARY = 'DataMapper plugin for creating state machines'
|
22
21
|
|
23
22
|
[ ROOT, ROOT.parent ].each do |dir|
|
@@ -86,7 +86,7 @@ module DataMapper
|
|
86
86
|
def initialize(*args)
|
87
87
|
super
|
88
88
|
# ===== Run :enter hook if present =====
|
89
|
-
return unless is_sm =
|
89
|
+
return unless is_sm = model.instance_variable_get(:@is_state_machine)
|
90
90
|
return unless machine = is_sm[:machine]
|
91
91
|
return unless initial = machine.initial
|
92
92
|
return unless initial_state = machine.find_state(initial)
|
@@ -104,7 +104,7 @@ module DataMapper
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def transition!(event_name)
|
107
|
-
machine =
|
107
|
+
machine = model.instance_variable_get(:@is_state_machine)[:machine]
|
108
108
|
column = machine.column
|
109
109
|
machine.current_state_name = attribute_get(:"#{column}")
|
110
110
|
machine.fire_event(event_name, self)
|
data/lib/dm-is-state_machine.rb
CHANGED
@@ -1,25 +1,8 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
# Require plugin-files
|
10
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'state_machine'
|
11
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'data' / 'event'
|
12
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'data' / 'machine'
|
13
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'data' / 'state'
|
14
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'dsl' / 'event_dsl'
|
15
|
-
require Pathname(__FILE__).dirname.expand_path / 'dm-is-state_machine' / 'is' / 'dsl' / 'state_dsl'
|
16
|
-
|
17
|
-
# Include the plugin in Resource
|
18
|
-
module DataMapper
|
19
|
-
module Model
|
20
|
-
include DataMapper::Is::StateMachine
|
21
|
-
end # module Model
|
22
|
-
end # module DataMapper
|
23
|
-
|
24
|
-
# An alternative way to do the same thing as above:
|
25
|
-
# DataMapper::Model.append_extensions DataMapper::Is::StateMachine
|
1
|
+
require 'dm-is-state_machine/is/state_machine'
|
2
|
+
require 'dm-is-state_machine/is/data/event'
|
3
|
+
require 'dm-is-state_machine/is/data/machine'
|
4
|
+
require 'dm-is-state_machine/is/data/state'
|
5
|
+
require 'dm-is-state_machine/is/dsl/event_dsl'
|
6
|
+
require 'dm-is-state_machine/is/dsl/state_dsl'
|
7
|
+
|
8
|
+
DataMapper::Model.append_extensions DataMapper::Is::StateMachine
|
@@ -46,12 +46,3 @@ class TrafficLight
|
|
46
46
|
end
|
47
47
|
|
48
48
|
TrafficLight.auto_migrate!
|
49
|
-
|
50
|
-
# ===== Note 1 =====
|
51
|
-
#
|
52
|
-
# One would expect that these two would be the same:
|
53
|
-
# property :id, Serial
|
54
|
-
# property :id, Integer, :serial => true
|
55
|
-
#
|
56
|
-
# But on 2008-07-05, the 2nd led to problems with an in-memory SQLite
|
57
|
-
# database.
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
describe "InvalidEvents" do
|
5
4
|
|
6
5
|
it "should get InvalidContext when requiring" do
|
7
6
|
lambda {
|
8
|
-
require
|
7
|
+
require 'examples/invalid_events'
|
9
8
|
}.should raise_error(DataMapper::Is::StateMachine::InvalidContext)
|
10
9
|
end
|
11
10
|
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
describe "InvalidStates" do
|
5
4
|
|
6
5
|
it "should get InvalidContext when requiring" do
|
7
6
|
lambda {
|
8
|
-
require
|
7
|
+
require 'examples/invalid_states'
|
9
8
|
}.should raise_error(DataMapper::Is::StateMachine::InvalidContext)
|
10
9
|
end
|
11
10
|
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
describe "InvalidTransitions1" do
|
5
4
|
|
6
5
|
it "should get InvalidContext when requiring" do
|
7
6
|
lambda {
|
8
|
-
require
|
7
|
+
require 'examples/invalid_transitions_1'
|
9
8
|
}.should raise_error(DataMapper::Is::StateMachine::InvalidContext)
|
10
9
|
end
|
11
10
|
|
@@ -15,7 +14,7 @@ describe "InvalidTransitions2" do
|
|
15
14
|
|
16
15
|
it "should get InvalidContext when requiring" do
|
17
16
|
lambda {
|
18
|
-
require
|
17
|
+
require 'examples/invalid_transitions_2'
|
19
18
|
}.should raise_error(DataMapper::Is::StateMachine::InvalidContext)
|
20
19
|
end
|
21
20
|
|
@@ -1,6 +1,5 @@
|
|
1
|
-
require '
|
2
|
-
require
|
3
|
-
require Pathname(__FILE__).dirname.expand_path.parent + 'examples/traffic_light'
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'examples/traffic_light'
|
4
3
|
|
5
4
|
describe TrafficLight do
|
6
5
|
|
@@ -55,7 +54,7 @@ describe TrafficLight do
|
|
55
54
|
@t.forward!
|
56
55
|
@t.color.should == "green"
|
57
56
|
@t.log.should == %w(G Y R G)
|
58
|
-
@t.
|
57
|
+
@t.should be_new
|
59
58
|
end
|
60
59
|
|
61
60
|
it "should skip to :yellow then transition to :red, :green, :yellow" do
|
@@ -71,7 +70,7 @@ describe TrafficLight do
|
|
71
70
|
@t.forward!
|
72
71
|
@t.color.should == "yellow"
|
73
72
|
@t.log.should == %w(G R G Y)
|
74
|
-
@t.
|
73
|
+
@t.should be_new
|
75
74
|
end
|
76
75
|
|
77
76
|
it "should skip to :red then transition to :green, :yellow, :red" do
|
@@ -87,7 +86,7 @@ describe TrafficLight do
|
|
87
86
|
@t.forward!
|
88
87
|
@t.color.should == "red"
|
89
88
|
@t.log.should == %w(G G Y R)
|
90
|
-
@t.
|
89
|
+
@t.should be_new
|
91
90
|
end
|
92
91
|
|
93
92
|
end
|
@@ -110,7 +109,7 @@ describe TrafficLight do
|
|
110
109
|
@t.backward!
|
111
110
|
@t.color.should == "green"
|
112
111
|
@t.log.should == %w(G R Y G)
|
113
|
-
@t.
|
112
|
+
@t.should be_new
|
114
113
|
end
|
115
114
|
|
116
115
|
it "should skip to :yellow then transition to :green, :red, :yellow" do
|
@@ -126,7 +125,7 @@ describe TrafficLight do
|
|
126
125
|
@t.backward!
|
127
126
|
@t.color.should == "yellow"
|
128
127
|
@t.log.should == %w(G G R Y)
|
129
|
-
@t.
|
128
|
+
@t.should be_new
|
130
129
|
end
|
131
130
|
|
132
131
|
it "should skip to :red then transition to :yellow, :green, :red" do
|
@@ -142,7 +141,7 @@ describe TrafficLight do
|
|
142
141
|
@t.backward!
|
143
142
|
@t.color.should == "red"
|
144
143
|
@t.log.should == %w(G Y G R)
|
145
|
-
@t.
|
144
|
+
@t.should be_new
|
146
145
|
end
|
147
146
|
|
148
147
|
end
|
data/spec/spec.opts
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
require 'pathname'
|
2
1
|
require 'rubygems'
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
# use local dm-core if running from a typical dev checkout.
|
4
|
+
lib = File.join('..', '..', 'dm-core', 'lib')
|
5
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib)
|
6
|
+
require 'dm-core'
|
6
7
|
|
7
|
-
|
8
|
+
# Support running specs with 'rake spec' and 'spec'
|
9
|
+
$LOAD_PATH.unshift('lib') unless $LOAD_PATH.include?('lib')
|
10
|
+
|
11
|
+
require 'dm-is-state_machine'
|
8
12
|
|
9
13
|
def load_driver(name, default_uri)
|
10
14
|
return false if ENV['ADAPTER'] != name.to_s
|
11
15
|
|
12
16
|
begin
|
13
17
|
DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
|
14
|
-
DataMapper::Repository.adapters[:default] =
|
18
|
+
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
|
15
19
|
true
|
16
20
|
rescue LoadError => e
|
17
21
|
warn "Could not load do_#{name}: #{e}"
|
@@ -1,5 +1,4 @@
|
|
1
|
-
require '
|
2
|
-
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
1
|
+
require 'spec_helper'
|
3
2
|
|
4
3
|
describe "StateMachine" do
|
5
4
|
|
@@ -27,8 +26,3 @@ describe "StateMachine" do
|
|
27
26
|
|
28
27
|
end
|
29
28
|
end
|
30
|
-
|
31
|
-
# is_state_machine
|
32
|
-
# push_state_machine_context(label)
|
33
|
-
# pop_state_machine_context
|
34
|
-
# state_machine_context?(label)
|
data/tasks/install.rb
CHANGED
@@ -4,7 +4,7 @@ end
|
|
4
4
|
|
5
5
|
desc "Install #{GEM_NAME} #{GEM_VERSION}"
|
6
6
|
task :install => [ :package ] do
|
7
|
-
sudo_gem "install
|
7
|
+
sudo_gem "install pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
|
8
8
|
end
|
9
9
|
|
10
10
|
desc "Uninstall #{GEM_NAME} #{GEM_VERSION}"
|
data/tasks/spec.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
begin
|
2
|
-
gem 'rspec', '~>1.2'
|
3
|
-
require 'spec'
|
4
2
|
require 'spec/rake/spectask'
|
5
3
|
|
6
4
|
task :default => [ :spec ]
|
@@ -8,16 +6,18 @@ begin
|
|
8
6
|
desc 'Run specifications'
|
9
7
|
Spec::Rake::SpecTask.new(:spec) do |t|
|
10
8
|
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
|
11
|
-
t.
|
9
|
+
t.libs << 'lib' << 'spec' # needed for CI rake spec task, duplicated in spec_helper
|
12
10
|
|
13
11
|
begin
|
14
|
-
|
12
|
+
require 'rcov'
|
15
13
|
t.rcov = JRUBY ? false : (ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true)
|
16
14
|
t.rcov_opts << '--exclude' << 'spec'
|
17
15
|
t.rcov_opts << '--text-summary'
|
18
16
|
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
|
19
17
|
rescue LoadError
|
20
18
|
# rcov not installed
|
19
|
+
rescue SyntaxError
|
20
|
+
# rcov syntax invalid
|
21
21
|
end
|
22
22
|
end
|
23
23
|
rescue LoadError
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-is-state_machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David James
|
@@ -9,19 +9,10 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: dm-core
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - "="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.11
|
24
|
-
version:
|
14
|
+
dependencies: []
|
15
|
+
|
25
16
|
description: DataMapper plugin for creating state machines
|
26
17
|
email:
|
27
18
|
- djwonk [a] collectiveinsight [d] net
|
@@ -30,17 +21,15 @@ executables: []
|
|
30
21
|
extensions: []
|
31
22
|
|
32
23
|
extra_rdoc_files:
|
33
|
-
- README.
|
34
|
-
- README.markdown
|
24
|
+
- README.rdoc
|
35
25
|
- LICENSE
|
36
26
|
- TODO
|
37
|
-
- History.
|
27
|
+
- History.rdoc
|
38
28
|
files:
|
39
|
-
- History.
|
29
|
+
- History.rdoc
|
40
30
|
- LICENSE
|
41
31
|
- Manifest.txt
|
42
|
-
- README.
|
43
|
-
- README.txt
|
32
|
+
- README.rdoc
|
44
33
|
- Rakefile
|
45
34
|
- TODO
|
46
35
|
- lib/dm-is-state_machine.rb
|
@@ -73,11 +62,13 @@ files:
|
|
73
62
|
- tasks/install.rb
|
74
63
|
- tasks/spec.rb
|
75
64
|
has_rdoc: true
|
76
|
-
homepage: http://github.com/
|
65
|
+
homepage: http://github.com/datamapper/dm-more/tree/master/dm-is-state_machine
|
66
|
+
licenses: []
|
67
|
+
|
77
68
|
post_install_message:
|
78
69
|
rdoc_options:
|
79
70
|
- --main
|
80
|
-
- README.
|
71
|
+
- README.rdoc
|
81
72
|
require_paths:
|
82
73
|
- lib
|
83
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -95,9 +86,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
86
|
requirements: []
|
96
87
|
|
97
88
|
rubyforge_project: datamapper
|
98
|
-
rubygems_version: 1.3.
|
89
|
+
rubygems_version: 1.3.5
|
99
90
|
signing_key:
|
100
|
-
specification_version:
|
91
|
+
specification_version: 3
|
101
92
|
summary: DataMapper plugin for creating state machines
|
102
93
|
test_files: []
|
103
94
|
|