mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Add StateMachine object and StateMachineFactory
Supports initial state in SM
This commit is contained in:
parent
28fd460950
commit
7dbbc1480b
2 changed files with 22 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
|||
require File.join(File.dirname(__FILE__), 'event')
|
||||
require File.join(File.dirname(__FILE__), 'state')
|
||||
require File.join(File.dirname(__FILE__), 'state_machine')
|
||||
require File.join(File.dirname(__FILE__), 'persistence')
|
||||
|
||||
module AASM
|
||||
|
@ -9,19 +10,20 @@ module AASM
|
|||
def self.included(base) #:nodoc:
|
||||
base.extend AASM::ClassMethods
|
||||
AASM::Persistence.set_persistence(base)
|
||||
AASM::StateMachineFactory[base] = AASM::StateMachine.new('')
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def aasm_initial_state(set_state=nil)
|
||||
if set_state
|
||||
aasm_initial_state = set_state
|
||||
AASM::StateMachineFactory[self].initial_state = set_state
|
||||
else
|
||||
@aasm_initial_state
|
||||
AASM::StateMachineFactory[self].initial_state
|
||||
end
|
||||
end
|
||||
|
||||
def aasm_initial_state=(state)
|
||||
@aasm_initial_state = state
|
||||
AASM::StateMachineFactory[self].initial_state = state
|
||||
end
|
||||
|
||||
def aasm_state(name, options={})
|
||||
|
|
17
lib/state_machine.rb
Normal file
17
lib/state_machine.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
module AASM
|
||||
unless AASM.const_defined?('StateMachineFactory')
|
||||
StateMachineFactory = {}
|
||||
end
|
||||
|
||||
class StateMachine
|
||||
attr_accessor :states, :events, :initial_state
|
||||
attr_reader :name
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
@initial_state = nil
|
||||
@states = []
|
||||
@events = []
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue