Class: Transitions::Machine
- Inherits:
-
Object
- Object
- Transitions::Machine
- Defined in:
- lib/transitions/machine.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
- #initial_state ⇒ Object
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#state_index ⇒ Object
Returns the value of attribute state_index.
-
#states ⇒ Object
Returns the value of attribute states.
Instance Method Summary collapse
- #current_state_variable ⇒ Object
- #events_for(state) ⇒ Object
- #fire_event(event, record, persist, *args) ⇒ Object
-
#initialize(klass, name, options = {}, &block) ⇒ Machine
constructor
A new instance of Machine.
- #states_for_select ⇒ Object
- #update(options = {}, &block) ⇒ Object
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, = {}, &block) @klass, @name, @states, @state_index, @events = klass, name, [], {}, {} update(, &block) end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
26 27 28 |
# File 'lib/transitions/machine.rb', line 26 def events @events end |
#initial_state ⇒ Object
34 35 36 |
# File 'lib/transitions/machine.rb', line 34 def initial_state @initial_state ||= (states.first ? states.first.name : nil) end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
27 28 29 |
# File 'lib/transitions/machine.rb', line 27 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/transitions/machine.rb', line 27 def name @name end |
#state_index ⇒ Object
Returns the value of attribute state_index.
26 27 28 |
# File 'lib/transitions/machine.rb', line 26 def state_index @state_index end |
#states ⇒ Object
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_variable ⇒ Object
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_select ⇒ Object
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( = {}, &block) @initial_state = [:initial] if .key?(:initial) instance_eval(&block) if block self end |