Skip to content

Commit

Permalink
Version bump to 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martinciu committed Jun 27, 2010
1 parent ba8fdac commit 3492b79
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.0.0
0.1.0
1 change: 1 addition & 0 deletions spec/spec.opts
@@ -0,0 +1 @@
--color
25 changes: 25 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,25 @@
require 'rubygems'
require 'bundler'

begin
Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run `bundle install` to install missing gems"
exit e.status_code
end

require 'active_model'
require 'state_machine'
require 'mongoid'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'state_machine-mongoid'
require 'spec'
require 'spec/autorun'
require 'vehicle'

Spec::Runner.configure do |config|

end
36 changes: 36 additions & 0 deletions spec/state_machine-mongoid_spec.rb
@@ -0,0 +1,36 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "StateMachineMongoid integration" do
before(:all) do
Mongoid.configure do |config|
name = "state_machine-mongoid"
host = "localhost"
config.allow_dynamic_fields = false
config.master = Mongo::Connection.new.db(name)
end
end

context "new vehicle" do
before(:each) do
@vehicle = Vehicle.new
end

it "should be parked" do
@vehicle.parked?.should be_true
@vehicle.state.should == "parked"
end

context "after igniting" do
before(:each) do
@vehicle.ignite
end

it "should be ignited" do
@vehicle.idling?.should be_true
end
end


end

end
100 changes: 100 additions & 0 deletions spec/vehicle.rb
@@ -0,0 +1,100 @@
class Vehicle
include Mongoid::Document

field :state, :type => String, :default => "parked"
field :alarm_state, :type => String, :default => "active"

state_machine :state, :initial => :parked do
before_transition :parked => any - :parked, :do => :put_on_seatbelt

after_transition :on => :crash, :do => :tow
after_transition :on => :repair, :do => :fix
after_transition any => :parked do |vehicle, transition|
vehicle.seatbelt_on = false
end

event :park do
transition [:idling, :first_gear] => :parked
end

event :ignite do
transition :stalled => same, :parked => :idling
end

event :idle do
transition :first_gear => :idling
end

event :shift_up do
transition :idling => :first_gear, :first_gear => :second_gear, :second_gear => :third_gear
end

event :shift_down do
transition :third_gear => :second_gear, :second_gear => :first_gear
end

event :crash do
transition all - [:parked, :stalled] => :stalled, :unless => :auto_shop_busy?
end

event :repair do
# The first transition that matches the state and passes its conditions
# will be used
transition :stalled => :parked, :if => :auto_shop_busy?
transition :stalled => same
end

state :parked do
def speed
0
end
end

state :idling, :first_gear do
def speed
10
end
end

state :second_gear do
def speed
20
end
end
end

state_machine :alarm_state, :initial => :active, :namespace => 'alarm' do
event :enable do
transition all => :active
end

event :disable do
transition all => :off
end

state :active, :value => 1
state :off, :value => 0
end

def initialize
@seatbelt_on = false
@time_used = 0
super() # NOTE: This *must* be called, otherwise states won't get initialized
end

def put_on_seatbelt
@seatbelt_on = true
end

def auto_shop_busy?
false
end

def tow
# tow the vehicle
end

def fix
# get the vehicle fixed by a mechanic
end
end

0 comments on commit 3492b79

Please sign in to comment.