Class: AASM::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/aasm/state_machine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ StateMachine

Returns a new instance of StateMachine.



15
16
17
18
19
20
21
22
# File 'lib/aasm/state_machine.rb', line 15

def initialize(name)
  @initial_state = nil
  @states = []
  @events = {}
  @global_callbacks = {}
  @config = AASM::Configuration.new
  @name = name
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def config
  @config
end

#eventsObject

Returns the value of attribute events.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def events
  @events
end

#global_callbacksObject

Returns the value of attribute global_callbacks.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def global_callbacks
  @global_callbacks
end

#initial_stateObject

Returns the value of attribute initial_state.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def initial_state
  @initial_state
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def name
  @name
end

#statesObject

Returns the value of attribute states.



13
14
15
# File 'lib/aasm/state_machine.rb', line 13

def states
  @states
end

Class Method Details

.[](klass) ⇒ Object

the following two methods provide the storage of all state machines



5
6
7
# File 'lib/aasm/state_machine.rb', line 5

def self.[](klass)
  (@machines ||= {})[klass.to_s]
end

.[]=(klass, machine) ⇒ Object



9
10
11
# File 'lib/aasm/state_machine.rb', line 9

def self.[]=(klass, machine)
  (@machines ||= {})[klass.to_s] = machine
end

Instance Method Details

#add_event(name, options, &block) ⇒ Object



40
41
42
# File 'lib/aasm/state_machine.rb', line 40

def add_event(name, options, &block)
  @events[name] = AASM::Core::Event.new(name, self, options, &block)
end

#add_global_callbacks(name, *callbacks, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/aasm/state_machine.rb', line 44

def add_global_callbacks(name, *callbacks, &block)
  @global_callbacks[name] ||= []
  callbacks.each do |callback|
    @global_callbacks[name] << callback
  end
  @global_callbacks[name] << block if block
end

#add_state(state_name, klass, options) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/aasm/state_machine.rb', line 31

def add_state(state_name, klass, options)
  set_initial_state(state_name, options)

  # allow reloading, extending or redefining a state
  @states.delete(state_name) if @states.include?(state_name)

  @states << AASM::Core::State.new(state_name, klass, self, options)
end

#initialize_copy(orig) ⇒ Object

called internally by Ruby 1.9 after clone()



25
26
27
28
29
# File 'lib/aasm/state_machine.rb', line 25

def initialize_copy(orig)
  super
  @states = @states.dup
  @events = @events.dup
end