Sha256: 70574526c9ca714436f8b4a46f40d1310e95b131c3c0608fe261ff6f68c7e0a9

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'active_record'
require 'active_record/edge-state-machine'

ActiveRecord::Base.establish_connection(:adapter  => "sqlite3", :database => ":memory:")

class TrafficLight < ActiveRecord::Base
  include ActiveRecord::EdgeStateMachine

  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 ProtectedTrafficLight < TrafficLight
  attr_protected :state
end

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

class ConditionalValidatingTrafficLight < TrafficLight
  validate :name, :presence => true, :length  => { :within => 20..40 }, :confirmation => true, :if => :red?
end

class TrafficLightNoScope < ActiveRecord::Base
  include ActiveRecord::EdgeStateMachine

  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/active_record/samples/traffic_light.rb
edge-state-machine-1.0.0 spec/active_record/samples/traffic_light.rb
edge-state-machine-0.9.1 spec/active_record/samples/traffic_light.rb
edge-state-machine-0.9.0 spec/active_record/samples/traffic_light.rb