Class: Acts::As::Multiple::StateMachines::SupportingClasses::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ StateMachine



16
17
18
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 16

def initialize(record)
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



14
15
16
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 14

def record
  @record
end

Class Method Details

.current_state(record) ⇒ Object



25
26
27
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 25

def current_state(record)
  record.send(state_column).to_sym
end

.event(event, opts = {}, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 29

def event(event, opts={}, &block)
  e = SupportingClasses::Event.new(self, event, opts, transition_table, &block)
  event_table[event.to_sym] = e
  class_eval "
    def #{event}!
      event_table[:#{event}].fire(record)
    end
  "
end

.state(name, opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 39

def state(name, opts={})
  state = SupportingClasses::State.new(self, name, opts)
  state_map[state.value] = state
  state_name = state.name
  class_eval "
    def #{state.name}?
      current_state.to_s == state_map['#{state.value}'].value
    end
  "
end

.statesObject



21
22
23
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 21

def states
  state_map.keys.collect { |state| state.to_sym }
end

Instance Method Details

#current_stateObject

Returns the current state the object is in, as a Ruby symbol.



88
89
90
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 88

def current_state
  self.class.current_state(record)
end

#next_state_for_event(event) ⇒ Object

Returns what the next state for a given event would be, as a Ruby symbol.



93
94
95
96
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 93

def next_state_for_event(event)
  ns = next_states_for_event(event)
  ns.empty? ? nil : ns.first.to.to_sym
end

#next_states_for_event(event) ⇒ Object



98
99
100
101
102
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 98

def next_states_for_event(event)
  transition_table[event.to_sym].select do |s|
    s.from == current_state.to_s
  end
end

#run_initial_state_actionsObject



81
82
83
84
85
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 81

def run_initial_state_actions
  initial = state_map[initial_state.to_s]
  initial.entering(record)
  initial.entered(record)
end

#set_initial_stateObject



77
78
79
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 77

def set_initial_state
  record.write_attribute state_column, initial_state.to_s
end

#statesObject



104
105
106
# File 'lib/acts/as/multiple/state_machines/supporting_classes/state_machine.rb', line 104

def states
  self.class.states
end