mm-transitions 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +60 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +70 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/mm-transitions.rb +71 -0
- data/mm-transitions.gemspec +84 -0
- data/spec/mm-transitions_spec.rb +101 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/models.rb +43 -0
- metadata +233 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "transitions"
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'bson_ext', '1.3.0'
|
7
|
+
gem 'mongo_mapper', '0.9.0'
|
8
|
+
gem 'i18n'
|
9
|
+
|
10
|
+
gem 'ruby-debug'
|
11
|
+
gem "rspec", "~> 2.1.0"
|
12
|
+
gem "yard", "~> 0.6.0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.5.1"
|
15
|
+
gem "rcov", ">= 0"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.6)
|
5
|
+
activesupport (= 3.0.6)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.5.0)
|
8
|
+
activesupport (3.0.6)
|
9
|
+
bson (1.3.0)
|
10
|
+
bson_ext (1.3.0)
|
11
|
+
builder (2.1.2)
|
12
|
+
columnize (0.3.2)
|
13
|
+
diff-lcs (1.1.2)
|
14
|
+
git (1.2.5)
|
15
|
+
i18n (0.5.0)
|
16
|
+
jeweler (1.5.2)
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
git (>= 1.2.5)
|
19
|
+
rake
|
20
|
+
linecache (0.43)
|
21
|
+
mongo (1.3.0)
|
22
|
+
bson (>= 1.3.0)
|
23
|
+
mongo_mapper (0.9.0)
|
24
|
+
activemodel (~> 3.0.0)
|
25
|
+
activesupport (~> 3.0.0)
|
26
|
+
plucky (~> 0.3.6)
|
27
|
+
plucky (0.3.7)
|
28
|
+
mongo (~> 1.1)
|
29
|
+
rake (0.8.7)
|
30
|
+
rcov (0.9.9)
|
31
|
+
rspec (2.1.0)
|
32
|
+
rspec-core (~> 2.1.0)
|
33
|
+
rspec-expectations (~> 2.1.0)
|
34
|
+
rspec-mocks (~> 2.1.0)
|
35
|
+
rspec-core (2.1.0)
|
36
|
+
rspec-expectations (2.1.0)
|
37
|
+
diff-lcs (~> 1.1.2)
|
38
|
+
rspec-mocks (2.1.0)
|
39
|
+
ruby-debug (0.10.4)
|
40
|
+
columnize (>= 0.1)
|
41
|
+
ruby-debug-base (~> 0.10.4.0)
|
42
|
+
ruby-debug-base (0.10.4)
|
43
|
+
linecache (>= 0.3)
|
44
|
+
transitions (0.0.9)
|
45
|
+
yard (0.6.7)
|
46
|
+
|
47
|
+
PLATFORMS
|
48
|
+
ruby
|
49
|
+
|
50
|
+
DEPENDENCIES
|
51
|
+
bson_ext (= 1.3.0)
|
52
|
+
bundler (~> 1.0.0)
|
53
|
+
i18n
|
54
|
+
jeweler (~> 1.5.1)
|
55
|
+
mongo_mapper (= 0.9.0)
|
56
|
+
rcov
|
57
|
+
rspec (~> 2.1.0)
|
58
|
+
ruby-debug
|
59
|
+
transitions
|
60
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Luke Cunningham
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
= mm-transitions
|
2
|
+
|
3
|
+
A simple MongoMapper plugin to add the Transitions State Machine to your documents
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
Install the gem
|
9
|
+
|
10
|
+
gem install mm-transitions
|
11
|
+
|
12
|
+
|
13
|
+
Or add it to your Gemfile
|
14
|
+
|
15
|
+
gem 'mm-transitions'
|
16
|
+
|
17
|
+
|
18
|
+
Then add the mm-transitions plugin to you MongoMapper::Document
|
19
|
+
|
20
|
+
class User
|
21
|
+
include MongoMapper::Document
|
22
|
+
plugin MongoMapper::Plugins::Transitions
|
23
|
+
|
24
|
+
state_machine do
|
25
|
+
state :pending
|
26
|
+
state :active
|
27
|
+
|
28
|
+
event :activate do
|
29
|
+
transitions :to => :active, :from => [:pending]
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
user = User.create
|
36
|
+
user.current_state => :pending
|
37
|
+
|
38
|
+
user.activate
|
39
|
+
user.current_state => :active
|
40
|
+
user.reload
|
41
|
+
user.current_state => :pending
|
42
|
+
|
43
|
+
user.activate!
|
44
|
+
user.current_state => :active
|
45
|
+
user.reload
|
46
|
+
user.current_state => :active
|
47
|
+
|
48
|
+
|
49
|
+
== More Info
|
50
|
+
|
51
|
+
for more information check out:
|
52
|
+
** [the specs](https://github.com/icaruswings/mm-transitions/blob/master/spec/mm-transitions_spec.rb)
|
53
|
+
** [the transitions gem](https://github.com/qoobaa/transitions)
|
54
|
+
|
55
|
+
|
56
|
+
== Contributing to mm-transitions
|
57
|
+
|
58
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
59
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
60
|
+
* Fork the project
|
61
|
+
* Start a feature/bugfix branch
|
62
|
+
* Commit and push until you are happy with your contribution
|
63
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
64
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
65
|
+
|
66
|
+
== Copyright
|
67
|
+
|
68
|
+
Copyright (c) 2011 Luke Cunningham. See LICENSE.txt for
|
69
|
+
further details.
|
70
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "mm-transitions"
|
16
|
+
gem.homepage = "http://github.com/icaruswings/mm-transitions"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{a simple mongomapper plugin to add a full featured state machine to your models}
|
19
|
+
gem.description = %Q{a simple mongomapper plugin to add a full featured state machine to your models}
|
20
|
+
gem.email = "luke@icaruswings.com"
|
21
|
+
gem.authors = ["Luke Cunningham"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "mm-transitions #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require 'transitions'
|
3
|
+
|
4
|
+
module MongoMapper
|
5
|
+
module Plugins
|
6
|
+
module Transitions
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
include ::Transitions
|
12
|
+
|
13
|
+
# adding back after_initialize callback remove in mongomapper
|
14
|
+
# https://github.com/jnunemaker/mongomapper/commit/f19d7725039fe352603e9809b0c47898cb9598f3
|
15
|
+
define_model_callbacks :initialize, :only => [:after]
|
16
|
+
|
17
|
+
after_initialize :set_initial_state
|
18
|
+
|
19
|
+
key :state, String
|
20
|
+
|
21
|
+
validates_presence_of :state #:required => true
|
22
|
+
validate :state_inclusion
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
|
27
|
+
# adding back after_initialize callback remove in mongomapper
|
28
|
+
# https://github.com/jnunemaker/mongomapper/commit/f19d7725039fe352603e9809b0c47898cb9598f3
|
29
|
+
def initialize(attrs = {})
|
30
|
+
super.tap { run_callbacks(:initialize) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def reload
|
34
|
+
super.tap do
|
35
|
+
self.class.state_machines.values.each do |sm|
|
36
|
+
remove_instance_variable(sm.current_state_variable) if instance_variable_defined?(sm.current_state_variable)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def write_state(state_machine, state)
|
44
|
+
ivar = state_machine.current_state_variable
|
45
|
+
prev_state = current_state(state_machine.name)
|
46
|
+
instance_variable_set(ivar, state)
|
47
|
+
self.state = state.to_s
|
48
|
+
save!
|
49
|
+
rescue MongoMapper::DocumentNotValid
|
50
|
+
self.state = prev_state.to_s
|
51
|
+
instance_variable_set(ivar, prev_state)
|
52
|
+
raise
|
53
|
+
end
|
54
|
+
|
55
|
+
def read_state(state_machine)
|
56
|
+
self.state.to_sym
|
57
|
+
end
|
58
|
+
|
59
|
+
def set_initial_state
|
60
|
+
self.state ||= self.class.state_machine.initial_state.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
def state_inclusion
|
64
|
+
unless self.class.state_machine.states.map{ |s| s.name.to_s }.include?(self.state.to_s)
|
65
|
+
self.errors.add(:state, :inclusion, :value => self.state)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mm-transitions}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Luke Cunningham"]
|
12
|
+
s.date = %q{2011-04-14}
|
13
|
+
s.description = %q{a simple mongomapper plugin to add a full featured state machine to your models}
|
14
|
+
s.email = %q{luke@icaruswings.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/mm-transitions.rb",
|
29
|
+
"mm-transitions.gemspec",
|
30
|
+
"spec/mm-transitions_spec.rb",
|
31
|
+
"spec/spec_helper.rb",
|
32
|
+
"spec/support/models.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/icaruswings/mm-transitions}
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.6.2}
|
38
|
+
s.summary = %q{a simple mongomapper plugin to add a full featured state machine to your models}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/mm-transitions_spec.rb",
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/support/models.rb"
|
43
|
+
]
|
44
|
+
|
45
|
+
if s.respond_to? :specification_version then
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_runtime_dependency(%q<transitions>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<bson_ext>, ["= 1.3.0"])
|
51
|
+
s.add_development_dependency(%q<mongo_mapper>, ["= 0.9.0"])
|
52
|
+
s.add_development_dependency(%q<i18n>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
54
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
|
55
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
58
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<transitions>, [">= 0"])
|
61
|
+
s.add_dependency(%q<bson_ext>, ["= 1.3.0"])
|
62
|
+
s.add_dependency(%q<mongo_mapper>, ["= 0.9.0"])
|
63
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
64
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
66
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
67
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
68
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
69
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<transitions>, [">= 0"])
|
73
|
+
s.add_dependency(%q<bson_ext>, ["= 1.3.0"])
|
74
|
+
s.add_dependency(%q<mongo_mapper>, ["= 0.9.0"])
|
75
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
76
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
77
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
78
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
79
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
80
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
81
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "MongoMapper::Plugins::Transitions" do
|
4
|
+
|
5
|
+
describe "New Record" do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@light = TrafficLight.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should set initial state" do
|
12
|
+
@light.should be_off
|
13
|
+
@light.current_state.should equal :off
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should validate presence of state" do
|
17
|
+
@light.should be_valid
|
18
|
+
@light.state = nil
|
19
|
+
@light.should_not be_valid
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "Saved Record" do
|
25
|
+
|
26
|
+
before(:each) do
|
27
|
+
@light = TrafficLight.create!
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should set initial state" do
|
31
|
+
@light.should be_off
|
32
|
+
@light.current_state.should equal :off
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should transition to a valid state" do
|
36
|
+
@light.reset
|
37
|
+
@light.should be_red
|
38
|
+
@light.current_state.should equal :red
|
39
|
+
|
40
|
+
@light.green_on
|
41
|
+
@light.should be_green
|
42
|
+
@light.current_state.should equal :green
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not persist state for event without !" do
|
46
|
+
@light.reset
|
47
|
+
@light.current_state.should equal :red
|
48
|
+
@light.reload
|
49
|
+
@light.state.should eql "off"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should persist state for event with !" do
|
53
|
+
@light.reset!
|
54
|
+
@light.current_state.should equal :red
|
55
|
+
@light.reload
|
56
|
+
@light.state.should eql "red"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should raise error on transition to invalid state" do
|
60
|
+
lambda { @light.yellow_on }.should raise_error(Transitions::InvalidTransition)
|
61
|
+
@light.current_state.should equal :off
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should still persist state if state is protected" do
|
65
|
+
protected_light = ProtectedTrafficLight.create!
|
66
|
+
protected_light.reset!
|
67
|
+
protected_light.current_state.should equal :red
|
68
|
+
protected_light.reload
|
69
|
+
protected_light.state.should eql "red"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should not be valid without inclusive state" do
|
73
|
+
for s in @light.class.state_machine.states
|
74
|
+
@light.state = s.name
|
75
|
+
@light.should be_valid
|
76
|
+
end
|
77
|
+
|
78
|
+
@light.state = "invalid_one"
|
79
|
+
@light.should_not be_valid
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should raise error on event! if model is invalid" do
|
83
|
+
validating_light = ValidatingTrafficLight.create!(:name => 'Foobar')
|
84
|
+
lambda { validating_light.reset! }.should raise_error(MongoMapper::DocumentNotValid)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should be able to use state? method in validation conditions" do
|
88
|
+
validating_light = ConditionalValidatingTrafficLight.create!
|
89
|
+
lambda { validating_light.reset! }.should raise_error(MongoMapper::DocumentNotValid)
|
90
|
+
validating_light.should be_off
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should reset current state on model reload" do
|
94
|
+
@light.reset
|
95
|
+
@light.should be_red
|
96
|
+
@light.update_attribute(:state, 'green')
|
97
|
+
@light.reload.should be_green, "reloaded state should come from database, not instance variable"
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
|
+
|
7
|
+
require 'rspec'
|
8
|
+
|
9
|
+
require 'mm-transitions'
|
10
|
+
|
11
|
+
# Requires supporting files with custom matchers and macros, etc,
|
12
|
+
# in ./support/ and its subdirectories.
|
13
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
14
|
+
|
15
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017)
|
16
|
+
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
17
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class TrafficLight
|
2
|
+
include MongoMapper::Document
|
3
|
+
|
4
|
+
plugin MongoMapper::Plugins::Transitions
|
5
|
+
|
6
|
+
key :name, String
|
7
|
+
|
8
|
+
state_machine do
|
9
|
+
state :off
|
10
|
+
|
11
|
+
state :red
|
12
|
+
state :green
|
13
|
+
state :yellow
|
14
|
+
|
15
|
+
event :red_on do
|
16
|
+
transitions :to => :red, :from => [:yellow]
|
17
|
+
end
|
18
|
+
|
19
|
+
event :green_on do
|
20
|
+
transitions :to => :green, :from => [:red]
|
21
|
+
end
|
22
|
+
|
23
|
+
event :yellow_on do
|
24
|
+
transitions :to => :yellow, :from => [:green]
|
25
|
+
end
|
26
|
+
|
27
|
+
event :reset do
|
28
|
+
transitions :to => :red, :from => [:off]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ProtectedTrafficLight < TrafficLight
|
34
|
+
attr_protected :state
|
35
|
+
end
|
36
|
+
|
37
|
+
class ValidatingTrafficLight < TrafficLight
|
38
|
+
validate {|t| errors.add(:base, 'This TrafficLight will never validate after creation') unless t.new_record? }
|
39
|
+
end
|
40
|
+
|
41
|
+
class ConditionalValidatingTrafficLight < TrafficLight
|
42
|
+
validates(:name, :presence => true, :if => :red?)
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mm-transitions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Luke Cunningham
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-14 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :runtime
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
name: transitions
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :development
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - "="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 27
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 3
|
47
|
+
- 0
|
48
|
+
version: 1.3.0
|
49
|
+
name: bson_ext
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
prerelease: false
|
53
|
+
type: :development
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - "="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 59
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
- 9
|
63
|
+
- 0
|
64
|
+
version: 0.9.0
|
65
|
+
name: mongo_mapper
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
name: i18n
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
prerelease: false
|
83
|
+
type: :development
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
name: ruby-debug
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
prerelease: false
|
97
|
+
type: :development
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ~>
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 11
|
104
|
+
segments:
|
105
|
+
- 2
|
106
|
+
- 1
|
107
|
+
- 0
|
108
|
+
version: 2.1.0
|
109
|
+
name: rspec
|
110
|
+
version_requirements: *id006
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
prerelease: false
|
113
|
+
type: :development
|
114
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ~>
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
hash: 7
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
- 6
|
123
|
+
- 0
|
124
|
+
version: 0.6.0
|
125
|
+
name: yard
|
126
|
+
version_requirements: *id007
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
prerelease: false
|
129
|
+
type: :development
|
130
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ~>
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 23
|
136
|
+
segments:
|
137
|
+
- 1
|
138
|
+
- 0
|
139
|
+
- 0
|
140
|
+
version: 1.0.0
|
141
|
+
name: bundler
|
142
|
+
version_requirements: *id008
|
143
|
+
- !ruby/object:Gem::Dependency
|
144
|
+
prerelease: false
|
145
|
+
type: :development
|
146
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
147
|
+
none: false
|
148
|
+
requirements:
|
149
|
+
- - ~>
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
hash: 1
|
152
|
+
segments:
|
153
|
+
- 1
|
154
|
+
- 5
|
155
|
+
- 1
|
156
|
+
version: 1.5.1
|
157
|
+
name: jeweler
|
158
|
+
version_requirements: *id009
|
159
|
+
- !ruby/object:Gem::Dependency
|
160
|
+
prerelease: false
|
161
|
+
type: :development
|
162
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
163
|
+
none: false
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
hash: 3
|
168
|
+
segments:
|
169
|
+
- 0
|
170
|
+
version: "0"
|
171
|
+
name: rcov
|
172
|
+
version_requirements: *id010
|
173
|
+
description: a simple mongomapper plugin to add a full featured state machine to your models
|
174
|
+
email: luke@icaruswings.com
|
175
|
+
executables: []
|
176
|
+
|
177
|
+
extensions: []
|
178
|
+
|
179
|
+
extra_rdoc_files:
|
180
|
+
- LICENSE.txt
|
181
|
+
- README.rdoc
|
182
|
+
files:
|
183
|
+
- .document
|
184
|
+
- .rspec
|
185
|
+
- Gemfile
|
186
|
+
- Gemfile.lock
|
187
|
+
- LICENSE.txt
|
188
|
+
- README.rdoc
|
189
|
+
- Rakefile
|
190
|
+
- VERSION
|
191
|
+
- lib/mm-transitions.rb
|
192
|
+
- mm-transitions.gemspec
|
193
|
+
- spec/mm-transitions_spec.rb
|
194
|
+
- spec/spec_helper.rb
|
195
|
+
- spec/support/models.rb
|
196
|
+
has_rdoc: true
|
197
|
+
homepage: http://github.com/icaruswings/mm-transitions
|
198
|
+
licenses:
|
199
|
+
- MIT
|
200
|
+
post_install_message:
|
201
|
+
rdoc_options: []
|
202
|
+
|
203
|
+
require_paths:
|
204
|
+
- lib
|
205
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
206
|
+
none: false
|
207
|
+
requirements:
|
208
|
+
- - ">="
|
209
|
+
- !ruby/object:Gem::Version
|
210
|
+
hash: 3
|
211
|
+
segments:
|
212
|
+
- 0
|
213
|
+
version: "0"
|
214
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
215
|
+
none: false
|
216
|
+
requirements:
|
217
|
+
- - ">="
|
218
|
+
- !ruby/object:Gem::Version
|
219
|
+
hash: 3
|
220
|
+
segments:
|
221
|
+
- 0
|
222
|
+
version: "0"
|
223
|
+
requirements: []
|
224
|
+
|
225
|
+
rubyforge_project:
|
226
|
+
rubygems_version: 1.6.2
|
227
|
+
signing_key:
|
228
|
+
specification_version: 3
|
229
|
+
summary: a simple mongomapper plugin to add a full featured state machine to your models
|
230
|
+
test_files:
|
231
|
+
- spec/mm-transitions_spec.rb
|
232
|
+
- spec/spec_helper.rb
|
233
|
+
- spec/support/models.rb
|