Class: Transitions::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/transitions/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options = {}, &block) ⇒ Machine

Returns a new instance of Machine.



29
30
31
32
# File 'lib/transitions/machine.rb', line 29

def initialize(klass, name, options = {}, &block)
  @klass, @name, @states, @state_index, @events = klass, name, [], {}, {}
  update(options, &block)
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



26
27
28
# File 'lib/transitions/machine.rb', line 26

def events
  @events
end

#initial_stateObject



34
35
36
# File 'lib/transitions/machine.rb', line 34

def initial_state
  @initial_state ||= (states.first ? states.first.name : nil)
end

#klassObject (readonly)

Returns the value of attribute klass.



27
28
29
# File 'lib/transitions/machine.rb', line 27

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/transitions/machine.rb', line 27

def name
  @name
end

#state_indexObject

Returns the value of attribute state_index.



26
27
28
# File 'lib/transitions/machine.rb', line 26

def state_index
  @state_index
end

#statesObject

Returns the value of attribute states.



26
27
28
# File 'lib/transitions/machine.rb', line 26

def states
  @states
end

Instance Method Details

#current_state_variableObject



74
75
76
# File 'lib/transitions/machine.rb', line 74

def current_state_variable
  "@#{@name}_current_state"
end

#events_for(state) ⇒ Object



69
70
71
72
# File 'lib/transitions/machine.rb', line 69

def events_for(state)
  events = @events.values.select { |event| event.transitions_from_state?(state) }
  events.map! { |event| event.name }
end

#fire_event(event, record, persist, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/transitions/machine.rb', line 44

def fire_event(event, record, persist, *args)
  state_index[record.current_state(@name)].call_action(:exit, record)
  if new_state = @events[event].fire(record, nil, *args)
    state_index[new_state].call_action(:enter, record)

    if record.respond_to?(event_fired_callback)
      record.send(event_fired_callback, record.current_state, new_state)
    end

    record.current_state(@name, new_state, persist)
    record.send(@events[event].success) if @events[event].success
    true
  else
    if record.respond_to?(event_failed_callback)
      record.send(event_failed_callback, event)
    end

    false
  end
end

#states_for_selectObject



65
66
67
# File 'lib/transitions/machine.rb', line 65

def states_for_select
  states.map { |st| [st.display_name, st.name.to_s] }
end

#update(options = {}, &block) ⇒ Object



38
39
40
41
42
# File 'lib/transitions/machine.rb', line 38

def update(options = {}, &block)
  @initial_state = options[:initial] if options.key?(:initial)
  instance_eval(&block) if block
  self
end