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
## 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
* 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.
```ruby
class Conversation
include AASM
class Conversation
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
aasm_state :unread
aasm_state :read
aasm_state :closed
aasm_event :view do
event :view do
transitions :to => :read, :from => [:unread]
end
aasm_event :close do
event :close do
transitions :to => :closed, :from => [:read, :unread]
end
end
end
```
## A Slightly More Complex Example ##
@ -89,22 +87,21 @@ This example uses a few of the more complex features available.
class Relationship
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
event :get_intimate do
transitions :to => :intimate, :from => [:dating], :guard => :drunk?
end
event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
end
end
aasm_initial_state Proc.new { |relationship| relationship.strictly_for_fun? ? :intimate : :dating }
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?
end
aasm_event :get_married do
transitions :to => :married, :from => [:dating, :intimate], :guard => :willing_to_give_up_manhood?
end
def strictly_for_fun?; end
def drunk?; end
def willing_to_give_up_manhood?; end
@ -122,14 +119,15 @@ This example uses a few of the more complex features available.
class Relationship
include AASM
aasm_state :dating
aasm_state :married
aasm do
state :dating
state :married
aasm_event :get_married,
:before => :make_vows,
:after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating]
end
event :get_married,
:before => :make_vows,
:after => :eat_wedding_cake do
transitions :to => :married, :from => [:dating]
end
end
```

View file

@ -2,13 +2,13 @@ module AASM
module ClassMethods
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)
end
end
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
end

View file

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

View file

@ -1,33 +1,33 @@
class Conversation
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
aasm_state :read
aasm_state :closed
aasm_state :awaiting_response
aasm_state :junk
event :new_message do
end
aasm_event :new_message do
end
event :view do
transitions :to => :read, :from => [:needs_attention]
end
aasm_event :view do
transitions :to => :read, :from => [:needs_attention]
end
event :reply do
end
aasm_event :reply do
end
event :close do
transitions :to => :closed, :from => [:read, :awaiting_response]
end
aasm_event :close do
transitions :to => :closed, :from => [:read, :awaiting_response]
end
event :junk do
transitions :to => :junk, :from => [:read]
end
aasm_event :junk do
transitions :to => :junk, :from => [:read]
end
aasm_event :unjunk do
event :unjunk do
end
end
def initialize(persister)

View file

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

View file

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