1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

version bump to 3.0.0 (new DSL)

This commit is contained in:
Thorsten Böttger 2011-11-26 21:11:57 +01:00
parent 19275903a2
commit 8f0ff1b0ab
7 changed files with 144 additions and 127 deletions

View file

@ -1,5 +1,11 @@
# CHANGELOG # CHANGELOG
## 3.0.0
* switched documentation to the new DSL
* whiny transactions: by default, raise an exception if an event transition is not possible
* you may disable whiny transactions
## 2.4.0 ## 2.4.0
* supporting new DSL (which is much shorter) * supporting new DSL (which is much shorter)

View file

@ -59,26 +59,24 @@ gem 'aasm'
Here's a quick example highlighting some of the features. Here's a quick example highlighting some of the features.
```ruby ```ruby
class Conversation class Conversation
include AASM include AASM
aasm_column :current_state # defaults to aasm_state aasm :column => :current_state do # defaults to aasm_state
state :unread, :initial => true
state :read
state :closed
aasm_initial_state :unread event :view do
aasm_state :unread
aasm_state :read
aasm_state :closed
aasm_event :view do
transitions :to => :read, :from => [:unread] transitions :to => :read, :from => [:unread]
end end
aasm_event :close do event :close do
transitions :to => :closed, :from => [:read, :unread] transitions :to => :closed, :from => [:read, :unread]
end end
end end
end
``` ```
## A Slightly More Complex Example ## ## A Slightly More Complex Example ##
@ -89,21 +87,20 @@ This example uses a few of the more complex features available.
class Relationship class Relationship
include AASM include AASM
aasm_column :status aasm :column => :status
state :dating, :enter => :make_happy, :exit => :make_depressed
state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover
aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating } event :get_intimate do
aasm_state :dating, :enter => :make_happy, :exit => :make_depressed
aasm_state :intimate, :enter => :make_very_happy, :exit => :never_speak_again
aasm_state :married, :enter => :give_up_intimacy, :exit => :buy_exotic_car_and_wear_a_combover
aasm_event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk? transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end end
aasm_event :get_married do event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood? transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
end end
end
aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }
def strictly_for_fun?; end def strictly_for_fun?; end
def drunk?; end def drunk?; end
@ -122,10 +119,11 @@ This example uses a few of the more complex features available.
class Relationship class Relationship
include AASM include AASM
aasm_state :dating aasm do
aasm_state :married state :dating
state :married
aasm_event :get_married, event :get_married,
:before => :make_vows, :before => :make_vows,
:after => :eat_wedding_cake do :after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating] transitions :to => :married, :from => [:dating]

View file

@ -2,13 +2,13 @@ module AASM
module ClassMethods module ClassMethods
def human_event_name(*args) def human_event_name(*args)
warn "AASM.human_event_name is deprecated and will be removed in version 3.0.0; please use AASM.aasm_human_event_name instead!" warn "AASM.human_event_name is deprecated and will be removed in version 3.1.0; please use AASM.aasm_human_event_name instead!"
aasm_human_event_name(*args) aasm_human_event_name(*args)
end end
end end
def human_state def human_state
warn "AASM#human_state is deprecated and will be removed in version 3.0.0; please use AASM#aasm_human_state instead!" warn "AASM#human_state is deprecated and will be removed in version 3.1.0; please use AASM#aasm_human_state instead!"
aasm_human_state aasm_human_state
end end

View file

@ -1,3 +1,3 @@
module AASM module AASM
VERSION = "2.4.0" VERSION = "3.0.0"
end end

View file

@ -1,33 +1,33 @@
class Conversation class Conversation
include AASM include AASM
aasm_initial_state :needs_attention aasm do
state :needs_attention, :initial => true
state :read
state :closed
state :awaiting_response
state :junk
aasm_state :needs_attention event :new_message do
aasm_state :read
aasm_state :closed
aasm_state :awaiting_response
aasm_state :junk
aasm_event :new_message do
end end
aasm_event :view do event :view do
transitions :to => :read, :from => [:needs_attention] transitions :to => :read, :from => [:needs_attention]
end end
aasm_event :reply do event :reply do
end end
aasm_event :close do event :close do
transitions :to => :closed, :from => [:read, :awaiting_response] transitions :to => :closed, :from => [:read, :awaiting_response]
end end
aasm_event :junk do event :junk do
transitions :to => :junk, :from => [:read] transitions :to => :junk, :from => [:read]
end end
aasm_event :unjunk do event :unjunk do
end
end end
def initialize(persister) def initialize(persister)

View file

@ -2,17 +2,18 @@ Dir[File.dirname(__FILE__) + "/../models/*.rb"].each { |f| require File.expand_p
class Foo class Foo
include AASM include AASM
aasm_initial_state :open aasm do
aasm_state :open, :exit => :exit state :open, :initial => true, :exit => :exit
aasm_state :closed, :enter => :enter state :closed, :enter => :enter
aasm_event :close, :success => :success_callback do event :close, :success => :success_callback do
transitions :to => :closed, :from => [:open] transitions :to => :closed, :from => [:open]
end end
aasm_event :null do event :null do
transitions :to => :closed, :from => [:open], :guard => :always_false transitions :to => :closed, :from => [:open], :guard => :always_false
end end
end
def always_false def always_false
false false
@ -29,18 +30,22 @@ end
class FooTwo < Foo class FooTwo < Foo
include AASM include AASM
aasm_state :foo aasm do
state :foo
end
end end
class Bar class Bar
include AASM include AASM
aasm_state :read aasm do
aasm_state :ended state :read
state :ended
aasm_event :foo do event :foo do
transitions :to => :ended, :from => [:read] transitions :to => :ended, :from => [:read]
end end
end
end end
class Baz < Bar class Baz < Bar
@ -48,9 +53,11 @@ end
class Banker class Banker
include AASM include AASM
aasm do
state :retired
state :selling_bad_mortgages
end
aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages } aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
aasm_state :retired
aasm_state :selling_bad_mortgages
RICH = 1_000_000 RICH = 1_000_000
attr_accessor :balance attr_accessor :balance
def initialize(balance = 0); self.balance = balance; end def initialize(balance = 0); self.balance = balance; end
@ -59,13 +66,14 @@ end
class Argument class Argument
include AASM include AASM
aasm_initial_state :invalid aasm do
aasm_state :invalid state :invalid, :initial => true
aasm_state :valid state :valid
aasm_event :valid do event :valid do
transitions :to => :valid, :from => [:invalid] transitions :to => :valid, :from => [:invalid]
end end
end
end end
class AuthMachine class AuthMachine
@ -73,40 +81,40 @@ class AuthMachine
attr_accessor :activation_code, :activated_at, :deleted_at attr_accessor :activation_code, :activated_at, :deleted_at
aasm_initial_state :pending aasm do
state :passive
state :pending, :initial => true, :enter => :make_activation_code
state :active, :enter => :do_activate
state :suspended
state :deleted, :enter => :do_delete, :exit => :do_undelete
aasm_state :passive event :register do
aasm_state :pending, :enter => :make_activation_code
aasm_state :active, :enter => :do_activate
aasm_state :suspended
aasm_state :deleted, :enter => :do_delete, :exit => :do_undelete
aasm_event :register do
transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| u.can_register? } transitions :from => :passive, :to => :pending, :guard => Proc.new {|u| u.can_register? }
end end
aasm_event :activate do event :activate do
transitions :from => :pending, :to => :active transitions :from => :pending, :to => :active
end end
aasm_event :suspend do event :suspend do
transitions :from => [:passive, :pending, :active], :to => :suspended transitions :from => [:passive, :pending, :active], :to => :suspended
end end
aasm_event :delete do event :delete do
transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted transitions :from => [:passive, :pending, :active, :suspended], :to => :deleted
end end
# a dummy event that can never happen # a dummy event that can never happen
aasm_event :unpassify do event :unpassify do
transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false } transitions :from => :passive, :to => :active, :guard => Proc.new {|u| false }
end end
aasm_event :unsuspend do event :unsuspend do
transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? } transitions :from => :suspended, :to => :active, :guard => Proc.new {|u| u.has_activated? }
transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? } transitions :from => :suspended, :to => :pending, :guard => Proc.new {|u| u.has_activation_code? }
transitions :from => :suspended, :to => :passive transitions :from => :suspended, :to => :passive
end end
end
def initialize def initialize
# the AR backend uses a before_validate_on_create :aasm_ensure_initial_state # the AR backend uses a before_validate_on_create :aasm_ensure_initial_state
@ -147,31 +155,34 @@ end
class ThisNameBetterNotBeInUse class ThisNameBetterNotBeInUse
include AASM include AASM
aasm_state :initial aasm do
aasm_state :symbol state :initial
aasm_state :string state :symbol
aasm_state :array state :string
aasm_state :proc state :array
state :proc
end
end end
class ChetanPatil class ChetanPatil
include AASM include AASM
aasm_initial_state :sleeping aasm do
aasm_state :sleeping state :sleeping, :initial => true
aasm_state :showering state :showering
aasm_state :working state :working
aasm_state :dating state :dating
aasm_state :prettying_up state :prettying_up
aasm_event :wakeup do event :wakeup do
transitions :from => :sleeping, :to => [:showering, :working] transitions :from => :sleeping, :to => [:showering, :working]
end end
aasm_event :dress do event :dress do
transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes transitions :from => :sleeping, :to => :working, :on_transition => :wear_clothes
transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) } transitions :from => :showering, :to => [:working, :dating], :on_transition => Proc.new { |obj, *args| obj.wear_clothes(*args) }
transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair] transitions :from => :showering, :to => :prettying_up, :on_transition => [:condition_hair, :fix_hair]
end end
end
def wear_clothes(shirt_color, trouser_type) def wear_clothes(shirt_color, trouser_type)
end end

View file

@ -12,12 +12,14 @@ class Gate < ActiveRecord::Base
# Fake this column for testing purposes # Fake this column for testing purposes
attr_accessor :aasm_state attr_accessor :aasm_state
aasm_state :opened aasm do
aasm_state :closed state :opened
state :closed
aasm_event :view do event :view do
transitions :to => :read, :from => [:needs_attention] transitions :to => :read, :from => [:needs_attention]
end end
end
end end
class Reader < ActiveRecord::Base class Reader < ActiveRecord::Base