Sha256: f84f978a11b39eef0fc03d5cef87e7acfa7e2da53fbb412a4f04e39067890bd7

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

require 'mongoid'
require 'mongoid/edge-state-machine'

class MongoTrafficLight
  include Mongoid::Document
  include Mongoid::EdgeStateMachine
  field :state

  state_machine do
    create_scopes true
    persisted_to :state
    state :off

    state :red
    state :green
    state :yellow

    event :red_on do
      transition :to => :red, :from => [:yellow]
    end

    event :green_on do
      transition :to => :green, :from => [:red]
    end

    event :yellow_on do
      transition :to => :yellow, :from => [:green]
    end

    event :reset do
      transition :to => :red, :from => [:off]
    end
  end
end

class MongoProtectedTrafficLight < MongoTrafficLight
  attr_protected :state
end

class MongoValidatingTrafficLight < MongoTrafficLight
  validate {|t| errors.add(:base, 'This TrafficLight will never validate after creation') unless t.new_record? }
end

class MongoConditionalValidatingTrafficLight < MongoTrafficLight
  validates :name, :presence => true, :length  => { :within => 20..40 }, :confirmation => true, :if => :red?
end

class MongoTrafficLightNoScope
  include Mongoid::Document
  include Mongoid::EdgeStateMachine
  field :state

  state_machine do
    persisted_to :state
    state :off

    state :red
    state :green
    state :yellow

    event :red_on do
      transition :to => :red, :from => [:yellow]
    end

    event :green_on do
      transition :to => :green, :from => [:red]
    end

    event :yellow_on do
      transition :to => :yellow, :from => [:green]
    end

    event :reset do
      transition :to => :red, :from => [:off]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
edge-state-machine-1.0.1 spec/mongoid/samples/traffic_light.rb
edge-state-machine-1.0.0 spec/mongoid/samples/traffic_light.rb
edge-state-machine-0.9.1 spec/mongoid/samples/traffic_light.rb
edge-state-machine-0.9.0 spec/mongoid/samples/traffic_light.rb