state_machine-mongoid 0.1.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.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/Gemfile +9 -0
- data/LICENSE +20 -0
- data/README.rdoc +21 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/state_machine/integrations/mongoid.rb +321 -0
- data/lib/state_machine-mongoid.rb +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/state_machine-mongoid_spec.rb +36 -0
- data/spec/vehicle.rb +100 -0
- data/state_machine-mongoid.gemspec +71 -0
- metadata +162 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Marcin Ciunelis
|
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,21 @@
|
|
1
|
+
= state_machine-mongoid
|
2
|
+
|
3
|
+
The mongoid integration for pluginaweek's state_machine
|
4
|
+
|
5
|
+
== TODO
|
6
|
+
|
7
|
+
* tests!
|
8
|
+
|
9
|
+
== Note on Patches/Pull Requests
|
10
|
+
|
11
|
+
* Fork the project.
|
12
|
+
* Make your feature addition or bug fix.
|
13
|
+
* Add tests for it. This is important so I don't break it in a
|
14
|
+
future version unintentionally.
|
15
|
+
* Commit, do not mess with rakefile, version, or history.
|
16
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
17
|
+
* Send me a pull request. Bonus points for topic branches.
|
18
|
+
|
19
|
+
== Copyright
|
20
|
+
|
21
|
+
Copyright (c) 2010 Marcin Ciunelis. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "state_machine-mongoid"
|
9
|
+
gem.summary = %Q{state_machine mongoid integration}
|
10
|
+
gem.description = %Q{a little lack of tests but it works!}
|
11
|
+
gem.email = "marcin.ciunelis@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/martinciu/state_machine-mongoid"
|
13
|
+
gem.authors = ["Marcin Ciunelis"]
|
14
|
+
gem.add_bundler_dependencies
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "state_machine-mongoid #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,321 @@
|
|
1
|
+
module StateMachine
|
2
|
+
module Integrations
|
3
|
+
module Mongoid
|
4
|
+
# The default options to use for state machines using this integration
|
5
|
+
class << self; attr_reader :defaults; end
|
6
|
+
@defaults = {:action => :save, :use_transactions => false}
|
7
|
+
|
8
|
+
# Should this integration be used for state machines in the given class?
|
9
|
+
# Classes that include Mongoid::Resource will automatically use the
|
10
|
+
# Mongoid integration.
|
11
|
+
def self.matches?(klass)
|
12
|
+
defined?(::Mongoid::Document) && klass <= ::Mongoid::Document
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
# Defines an initialization hook into the owner class for setting the
|
18
|
+
# initial state of the machine *before* any attributes are set on the
|
19
|
+
# object
|
20
|
+
def define_state_initializer
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
# module StateMachine
|
28
|
+
# module Integrations #:nodoc:
|
29
|
+
# # Adds support for integrating state machines with Mongoid models.
|
30
|
+
# #
|
31
|
+
# # == Examples
|
32
|
+
# #
|
33
|
+
# # Below is an example of a simple state machine defined within a
|
34
|
+
# # Mongoid model:
|
35
|
+
# #
|
36
|
+
# # class Vehicle
|
37
|
+
# # include Mongoid::Document
|
38
|
+
# #
|
39
|
+
# # state_machine :initial => :parked do
|
40
|
+
# # event :ignite do
|
41
|
+
# # transition :parked => :idling
|
42
|
+
# # end
|
43
|
+
# # end
|
44
|
+
# # end
|
45
|
+
# #
|
46
|
+
# # The examples in the sections below will use the above class as a
|
47
|
+
# # reference.
|
48
|
+
# #
|
49
|
+
# # == Actions
|
50
|
+
# #
|
51
|
+
# # By default, the action that will be invoked when a state is transitioned
|
52
|
+
# # is the +save+ action. This will cause the record to save the changes
|
53
|
+
# # made to the state machine's attribute. *Note* that if any other changes
|
54
|
+
# # were made to the record prior to transition, then those changes will
|
55
|
+
# # be saved as well.
|
56
|
+
# #
|
57
|
+
# # For example,
|
58
|
+
# #
|
59
|
+
# # vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
60
|
+
# # vehicle.name = 'Ford Explorer'
|
61
|
+
# # vehicle.ignite # => true
|
62
|
+
# # vehicle.reload # => #<Vehicle id: 1, name: "Ford Explorer", state: "idling">
|
63
|
+
# #
|
64
|
+
# # == Events
|
65
|
+
# #
|
66
|
+
# # As described in StateMachine::InstanceMethods#state_machine, event
|
67
|
+
# # attributes are created for every machine that allow transitions to be
|
68
|
+
# # performed automatically when the object's action (in this case, :save)
|
69
|
+
# # is called.
|
70
|
+
# #
|
71
|
+
# # In Mongoid, these automated events are run in the following order:
|
72
|
+
# # * before validation - Run before callbacks and persist new states, then validate
|
73
|
+
# # * before save - If validation was skipped, run before callbacks and persist new states, then save
|
74
|
+
# # * after save - Run after callbacks
|
75
|
+
# #
|
76
|
+
# # For example,
|
77
|
+
# #
|
78
|
+
# # vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
|
79
|
+
# # vehicle.state_event # => nil
|
80
|
+
# # vehicle.state_event = 'invalid'
|
81
|
+
# # vehicle.valid? # => false
|
82
|
+
# # vehicle.errors.full_messages # => ["State event is invalid"]
|
83
|
+
# #
|
84
|
+
# # vehicle.state_event = 'ignite'
|
85
|
+
# # vehicle.valid? # => true
|
86
|
+
# # vehicle.save # => true
|
87
|
+
# # vehicle.state # => "idling"
|
88
|
+
# # vehicle.state_event # => nil
|
89
|
+
# #
|
90
|
+
# # Note that this can also be done on a mass-assignment basis:
|
91
|
+
# #
|
92
|
+
# # vehicle = Vehicle.create(:state_event => 'ignite') # => #<Vehicle id: 1, name: nil, state: "idling">
|
93
|
+
# # vehicle.state # => "idling"
|
94
|
+
# #
|
95
|
+
# # This technique is always used for transitioning states when the +save+
|
96
|
+
# # action (which is the default) is configured for the machine.
|
97
|
+
# #
|
98
|
+
# # === Security implications
|
99
|
+
# #
|
100
|
+
# # Beware that public event attributes mean that events can be fired
|
101
|
+
# # whenever mass-assignment is being used. If you want to prevent malicious
|
102
|
+
# # users from tampering with events through URLs / forms, the attribute
|
103
|
+
# # should be protected like so:
|
104
|
+
# #
|
105
|
+
# # class Vehicle
|
106
|
+
# # include Mongoid::Document
|
107
|
+
# #
|
108
|
+
# # attr_protected :state_event
|
109
|
+
# # # attr_accessible ... # Alternative technique
|
110
|
+
# #
|
111
|
+
# # state_machine do
|
112
|
+
# # ...
|
113
|
+
# # end
|
114
|
+
# # end
|
115
|
+
# #
|
116
|
+
# # If you want to only have *some* events be able to fire via mass-assignment,
|
117
|
+
# # you can build two state machines (one public and one protected) like so:
|
118
|
+
# #
|
119
|
+
# # class Vehicle
|
120
|
+
# # include Mongoid::Document
|
121
|
+
# #
|
122
|
+
# # attr_protected :state_event # Prevent access to events in the first machine
|
123
|
+
# #
|
124
|
+
# # state_machine do
|
125
|
+
# # # Define private events here
|
126
|
+
# # end
|
127
|
+
# #
|
128
|
+
# # # Public machine targets the same state as the private machine
|
129
|
+
# # state_machine :public_state, :attribute => :state do
|
130
|
+
# # # Define public events here
|
131
|
+
# # end
|
132
|
+
# # end
|
133
|
+
# #
|
134
|
+
# # == Validation errors
|
135
|
+
# #
|
136
|
+
# # If an event fails to successfully fire because there are no matching
|
137
|
+
# # transitions for the current record, a validation error is added to the
|
138
|
+
# # record's state attribute to help in determining why it failed and for
|
139
|
+
# # reporting via the UI.
|
140
|
+
# #
|
141
|
+
# # For example,
|
142
|
+
# #
|
143
|
+
# # vehicle = Vehicle.create(:state => 'idling') # => #<Vehicle id: 1, name: nil, state: "idling">
|
144
|
+
# # vehicle.ignite # => false
|
145
|
+
# # vehicle.errors.full_messages # => ["State cannot transition via \"ignite\""]
|
146
|
+
# #
|
147
|
+
# # If an event fails to fire because of a validation error on the record and
|
148
|
+
# # *not* because a matching transition was not available, no error messages
|
149
|
+
# # will be added to the state attribute.
|
150
|
+
# #
|
151
|
+
# # == Scopes
|
152
|
+
# #
|
153
|
+
# # To assist in filtering models with specific states, a series of basic
|
154
|
+
# # scopes are defined on the model for finding records with or without a
|
155
|
+
# # particular set of states.
|
156
|
+
# #
|
157
|
+
# # These scopes are essentially the functional equivalent of the following
|
158
|
+
# # definitions:
|
159
|
+
# #
|
160
|
+
# # class Vehicle
|
161
|
+
# # include Mongoid::Document
|
162
|
+
# #
|
163
|
+
# # def self.with_states(*states)
|
164
|
+
# # all(:conditions => {:state => {'$in' => states}})
|
165
|
+
# # end
|
166
|
+
# # # with_states also aliased to with_state
|
167
|
+
# #
|
168
|
+
# # def self.without_states(*states)
|
169
|
+
# # all(:conditions => {:state => {'$nin' => states}})
|
170
|
+
# # end
|
171
|
+
# # # without_states also aliased to without_state
|
172
|
+
# # end
|
173
|
+
# #
|
174
|
+
# # *Note*, however, that the states are converted to their stored values
|
175
|
+
# # before being passed into the query.
|
176
|
+
# #
|
177
|
+
# # Because of the way named scopes work in Mongoid, they *cannot* be
|
178
|
+
# # chained.
|
179
|
+
# #
|
180
|
+
# # == Callbacks
|
181
|
+
# #
|
182
|
+
# # All before/after transition callbacks defined for Mongoid models
|
183
|
+
# # behave in the same way that other Mongoid callbacks behave. The
|
184
|
+
# # object involved in the transition is passed in as an argument.
|
185
|
+
# #
|
186
|
+
# # For example,
|
187
|
+
# #
|
188
|
+
# # class Vehicle
|
189
|
+
# # include Mongoid::Document
|
190
|
+
# #
|
191
|
+
# # state_machine :initial => :parked do
|
192
|
+
# # before_transition any => :idling do |vehicle|
|
193
|
+
# # vehicle.put_on_seatbelt
|
194
|
+
# # end
|
195
|
+
# #
|
196
|
+
# # before_transition do |vehicle, transition|
|
197
|
+
# # # log message
|
198
|
+
# # end
|
199
|
+
# #
|
200
|
+
# # event :ignite do
|
201
|
+
# # transition :parked => :idling
|
202
|
+
# # end
|
203
|
+
# # end
|
204
|
+
# #
|
205
|
+
# # def put_on_seatbelt
|
206
|
+
# # ...
|
207
|
+
# # end
|
208
|
+
# # end
|
209
|
+
# #
|
210
|
+
# # Note, also, that the transition can be accessed by simply defining
|
211
|
+
# # additional arguments in the callback block.
|
212
|
+
# module Mongoid
|
213
|
+
# include ActiveModel
|
214
|
+
#
|
215
|
+
# # The default options to use for state machines using this integration
|
216
|
+
# @defaults = {:action => :save}
|
217
|
+
#
|
218
|
+
# # Should this integration be used for state machines in the given class?
|
219
|
+
# # Classes that include Mongoid::Document will automatically use the
|
220
|
+
# # Mongoid integration.
|
221
|
+
# def self.matches?(klass)
|
222
|
+
# defined?(::Mongoid::Document) && klass <= ::Mongoid::Document
|
223
|
+
# end
|
224
|
+
#
|
225
|
+
# # Adds a validation error to the given object (no i18n support)
|
226
|
+
# def invalidate(object, attribute, message, values = [])
|
227
|
+
# object.errors.add(self.attribute(attribute), generate_message(message, values))
|
228
|
+
# end
|
229
|
+
#
|
230
|
+
# protected
|
231
|
+
# # Does not support observers
|
232
|
+
# def supports_observers?
|
233
|
+
# false
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
# # Always adds validation support
|
237
|
+
# def supports_validations?
|
238
|
+
# true
|
239
|
+
# end
|
240
|
+
#
|
241
|
+
# # Only runs validations on the action if using <tt>:save</tt>
|
242
|
+
# def runs_validations_on_action?
|
243
|
+
# action == :save
|
244
|
+
# end
|
245
|
+
#
|
246
|
+
# # Always adds dirty tracking support
|
247
|
+
# def supports_dirty_tracking?(object)
|
248
|
+
# true
|
249
|
+
# end
|
250
|
+
#
|
251
|
+
# # Don't allow callback terminators
|
252
|
+
# def callback_terminator
|
253
|
+
# end
|
254
|
+
#
|
255
|
+
# # Defines an initialization hook into the owner class for setting the
|
256
|
+
# # initial state of the machine *before* any attributes are set on the
|
257
|
+
# # object
|
258
|
+
# def define_state_initializer
|
259
|
+
# @instance_helper_module.class_eval <<-end_eval, __FILE__, __LINE__
|
260
|
+
# def initialize(attrs = {}, *args)
|
261
|
+
# from_database = args.first
|
262
|
+
#
|
263
|
+
# if !from_database && (!attrs || !attrs.stringify_keys.key?('_id'))
|
264
|
+
# filtered = respond_to?(:filter_protected_attrs) ? filter_protected_attrs(attrs) : attrs
|
265
|
+
# ignore = filtered ? filtered.keys : []
|
266
|
+
#
|
267
|
+
# initialize_state_machines(:dynamic => false, :ignore => ignore)
|
268
|
+
# super
|
269
|
+
# initialize_state_machines(:dynamic => true, :ignore => ignore)
|
270
|
+
# else
|
271
|
+
# super
|
272
|
+
# end
|
273
|
+
# end
|
274
|
+
# end_eval
|
275
|
+
# end
|
276
|
+
#
|
277
|
+
# # Skips defining reader/writer methods since this is done automatically
|
278
|
+
# def define_state_accessor
|
279
|
+
# owner_class.field(attribute, :type => String) unless owner_class.fields.nil? #.include?(attribute)
|
280
|
+
#
|
281
|
+
# name = self.name
|
282
|
+
# owner_class.validates_each(attribute, :logic => lambda {|*|
|
283
|
+
# machine = self.class.state_machine(name)
|
284
|
+
# machine.invalidate(self, :state, :invalid) unless machine.states.match(self)
|
285
|
+
# })
|
286
|
+
# end
|
287
|
+
#
|
288
|
+
# # Adds support for defining the attribute predicate, while providing
|
289
|
+
# # compatibility with the default predicate which determines whether
|
290
|
+
# # *anything* is set for the attribute's value
|
291
|
+
# def define_state_predicate
|
292
|
+
# name = self.name
|
293
|
+
#
|
294
|
+
# # Still use class_eval here instance of define_instance_method since
|
295
|
+
# # we need to be able to call +super+
|
296
|
+
# @instance_helper_module.class_eval do
|
297
|
+
# define_method("#{name}?") do |*args|
|
298
|
+
# args.empty? ? super(*args) : self.class.state_machine(name).states.matches?(self, *args)
|
299
|
+
# end
|
300
|
+
# end
|
301
|
+
# end
|
302
|
+
#
|
303
|
+
# # Adds hooks into validation for automatically firing events
|
304
|
+
# def define_action_helpers
|
305
|
+
# super(action == :save ? :create_or_update : action)
|
306
|
+
# end
|
307
|
+
#
|
308
|
+
# # Creates a scope for finding records *with* a particular state or
|
309
|
+
# # states for the attribute
|
310
|
+
# def create_with_scope(name)
|
311
|
+
# lambda {|model, values| model.all(:conditions => {attribute => {'$in' => values}})}
|
312
|
+
# end
|
313
|
+
#
|
314
|
+
# # Creates a scope for finding records *without* a particular state or
|
315
|
+
# # states for the attribute
|
316
|
+
# def create_without_scope(name)
|
317
|
+
# lambda {|model, values| model.all(:conditions => {attribute => {'$nin' => values}})}
|
318
|
+
# end
|
319
|
+
# end
|
320
|
+
# end
|
321
|
+
# end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'state_machine/integrations/mongoid'
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'active_model'
|
13
|
+
require 'state_machine'
|
14
|
+
require 'mongoid'
|
15
|
+
|
16
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
17
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
18
|
+
require 'state_machine-mongoid'
|
19
|
+
require 'spec'
|
20
|
+
require 'spec/autorun'
|
21
|
+
require 'vehicle'
|
22
|
+
|
23
|
+
Spec::Runner.configure do |config|
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "StateMachineMongoid integration" do
|
4
|
+
before(:all) do
|
5
|
+
Mongoid.configure do |config|
|
6
|
+
name = "state_machine-mongoid"
|
7
|
+
host = "localhost"
|
8
|
+
config.allow_dynamic_fields = false
|
9
|
+
config.master = Mongo::Connection.new.db(name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "new vehicle" do
|
14
|
+
before(:each) do
|
15
|
+
@vehicle = Vehicle.new
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be parked" do
|
19
|
+
@vehicle.parked?.should be_true
|
20
|
+
@vehicle.state.should == "parked"
|
21
|
+
end
|
22
|
+
|
23
|
+
context "after igniting" do
|
24
|
+
before(:each) do
|
25
|
+
@vehicle.ignite
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be ignited" do
|
29
|
+
@vehicle.idling?.should be_true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/vehicle.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
class Vehicle
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
field :state, :type => String, :default => "parked"
|
5
|
+
field :alarm_state, :type => String, :default => "active"
|
6
|
+
|
7
|
+
state_machine :state, :initial => :parked do
|
8
|
+
before_transition :parked => any - :parked, :do => :put_on_seatbelt
|
9
|
+
|
10
|
+
after_transition :on => :crash, :do => :tow
|
11
|
+
after_transition :on => :repair, :do => :fix
|
12
|
+
after_transition any => :parked do |vehicle, transition|
|
13
|
+
vehicle.seatbelt_on = false
|
14
|
+
end
|
15
|
+
|
16
|
+
event :park do
|
17
|
+
transition [:idling, :first_gear] => :parked
|
18
|
+
end
|
19
|
+
|
20
|
+
event :ignite do
|
21
|
+
transition :stalled => same, :parked => :idling
|
22
|
+
end
|
23
|
+
|
24
|
+
event :idle do
|
25
|
+
transition :first_gear => :idling
|
26
|
+
end
|
27
|
+
|
28
|
+
event :shift_up do
|
29
|
+
transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear
|
30
|
+
end
|
31
|
+
|
32
|
+
event :shift_down do
|
33
|
+
transition :third_gear => :second_gear, :second_gear => :first_gear
|
34
|
+
end
|
35
|
+
|
36
|
+
event :crash do
|
37
|
+
transition all - [:parked, :stalled] => :stalled, :unless => :auto_shop_busy?
|
38
|
+
end
|
39
|
+
|
40
|
+
event :repair do
|
41
|
+
# The first transition that matches the state and passes its conditions
|
42
|
+
# will be used
|
43
|
+
transition :stalled => :parked, :if => :auto_shop_busy?
|
44
|
+
transition :stalled => same
|
45
|
+
end
|
46
|
+
|
47
|
+
state :parked do
|
48
|
+
def speed
|
49
|
+
0
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
state :idling, :first_gear do
|
54
|
+
def speed
|
55
|
+
10
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
state :second_gear do
|
60
|
+
def speed
|
61
|
+
20
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
state_machine :alarm_state, :initial => :active, :namespace => 'alarm' do
|
67
|
+
event :enable do
|
68
|
+
transition all => :active
|
69
|
+
end
|
70
|
+
|
71
|
+
event :disable do
|
72
|
+
transition all => :off
|
73
|
+
end
|
74
|
+
|
75
|
+
state :active, :value => 1
|
76
|
+
state :off, :value => 0
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize
|
80
|
+
@seatbelt_on = false
|
81
|
+
@time_used = 0
|
82
|
+
super() # NOTE: This *must* be called, otherwise states won't get initialized
|
83
|
+
end
|
84
|
+
|
85
|
+
def put_on_seatbelt
|
86
|
+
@seatbelt_on = true
|
87
|
+
end
|
88
|
+
|
89
|
+
def auto_shop_busy?
|
90
|
+
false
|
91
|
+
end
|
92
|
+
|
93
|
+
def tow
|
94
|
+
# tow the vehicle
|
95
|
+
end
|
96
|
+
|
97
|
+
def fix
|
98
|
+
# get the vehicle fixed by a mechanic
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{state_machine-mongoid}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Marcin Ciunelis"]
|
12
|
+
s.date = %q{2010-06-27}
|
13
|
+
s.description = %q{a little lack of tests but it works!}
|
14
|
+
s.email = %q{marcin.ciunelis@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/state_machine-mongoid.rb",
|
28
|
+
"lib/state_machine/integrations/mongoid.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb",
|
31
|
+
"spec/state_machine-mongoid_spec.rb",
|
32
|
+
"spec/vehicle.rb",
|
33
|
+
"state_machine-mongoid.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/martinciu/state_machine-mongoid}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{state_machine mongoid integration}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/spec_helper.rb",
|
42
|
+
"spec/state_machine-mongoid_spec.rb",
|
43
|
+
"spec/vehicle.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<bson_ext>, ["= 1.0.1"])
|
52
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
|
53
|
+
s.add_runtime_dependency(%q<state_machine>, [">= 0.9.2"])
|
54
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
|
55
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<bson_ext>, ["= 1.0.1"])
|
58
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
|
59
|
+
s.add_dependency(%q<state_machine>, [">= 0.9.2"])
|
60
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
|
61
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<bson_ext>, ["= 1.0.1"])
|
65
|
+
s.add_dependency(%q<mongoid>, [">= 2.0.0.beta7"])
|
66
|
+
s.add_dependency(%q<state_machine>, [">= 0.9.2"])
|
67
|
+
s.add_dependency(%q<activemodel>, [">= 3.0.0.beta4"])
|
68
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: state_machine-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Marcin Ciunelis
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-27 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - "="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 21
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
- 1
|
32
|
+
version: 1.0.1
|
33
|
+
type: :runtime
|
34
|
+
name: bson_ext
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: -1848230041
|
44
|
+
segments:
|
45
|
+
- 2
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
- beta7
|
49
|
+
version: 2.0.0.beta7
|
50
|
+
type: :runtime
|
51
|
+
name: mongoid
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 63
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
- 9
|
64
|
+
- 2
|
65
|
+
version: 0.9.2
|
66
|
+
type: :runtime
|
67
|
+
name: state_machine
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *id003
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: -1848230024
|
77
|
+
segments:
|
78
|
+
- 3
|
79
|
+
- 0
|
80
|
+
- 0
|
81
|
+
- beta4
|
82
|
+
version: 3.0.0.beta4
|
83
|
+
type: :runtime
|
84
|
+
name: activemodel
|
85
|
+
prerelease: false
|
86
|
+
version_requirements: *id004
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 3
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
version: "0"
|
97
|
+
type: :development
|
98
|
+
name: rspec
|
99
|
+
prerelease: false
|
100
|
+
version_requirements: *id005
|
101
|
+
description: a little lack of tests but it works!
|
102
|
+
email: marcin.ciunelis@gmail.com
|
103
|
+
executables: []
|
104
|
+
|
105
|
+
extensions: []
|
106
|
+
|
107
|
+
extra_rdoc_files:
|
108
|
+
- LICENSE
|
109
|
+
- README.rdoc
|
110
|
+
files:
|
111
|
+
- .document
|
112
|
+
- .gitignore
|
113
|
+
- Gemfile
|
114
|
+
- LICENSE
|
115
|
+
- README.rdoc
|
116
|
+
- Rakefile
|
117
|
+
- VERSION
|
118
|
+
- lib/state_machine-mongoid.rb
|
119
|
+
- lib/state_machine/integrations/mongoid.rb
|
120
|
+
- spec/spec.opts
|
121
|
+
- spec/spec_helper.rb
|
122
|
+
- spec/state_machine-mongoid_spec.rb
|
123
|
+
- spec/vehicle.rb
|
124
|
+
- state_machine-mongoid.gemspec
|
125
|
+
has_rdoc: true
|
126
|
+
homepage: http://github.com/martinciu/state_machine-mongoid
|
127
|
+
licenses: []
|
128
|
+
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options:
|
131
|
+
- --charset=UTF-8
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
hash: 3
|
140
|
+
segments:
|
141
|
+
- 0
|
142
|
+
version: "0"
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
hash: 3
|
149
|
+
segments:
|
150
|
+
- 0
|
151
|
+
version: "0"
|
152
|
+
requirements: []
|
153
|
+
|
154
|
+
rubyforge_project:
|
155
|
+
rubygems_version: 1.3.7
|
156
|
+
signing_key:
|
157
|
+
specification_version: 3
|
158
|
+
summary: state_machine mongoid integration
|
159
|
+
test_files:
|
160
|
+
- spec/spec_helper.rb
|
161
|
+
- spec/state_machine-mongoid_spec.rb
|
162
|
+
- spec/vehicle.rb
|