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, options = {}, &block) ⇒ Machine

Returns a new instance of Machine.



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

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

Instance Attribute Details

#auto_scopesObject (readonly)

Returns the value of attribute auto_scopes.



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

def auto_scopes
  @auto_scopes
end

#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

#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



79
80
81
82
# File 'lib/transitions/machine.rb', line 79

def current_state_variable
  # TODO Refactor me away.
  :@current_state
end

#events_for(state) ⇒ Object



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

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

TODO Refactor me please?



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/transitions/machine.rb', line 47

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

      if record.respond_to?(:event_fired)
        record.send(:event_fired, record.current_state, new_state, event)
      end

      record.update_current_state(new_state, persist)
      @events[event].success.call(record) if @events[event].success
      return true
    else
      record.send(:event_failed, event) if record.respond_to?(:event_failed)
      return false
    end
  rescue => e
    if record.respond_to?(:event_failed)
      record.send(:event_failed, event)
      return false
    else
      raise e
    end
  end
end

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



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

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