Class: Transitions::Machine
- Inherits:
-
Object
- Object
- Transitions::Machine
- Defined in:
- lib/transitions/machine.rb
Instance Attribute Summary collapse
-
#auto_scopes ⇒ Object
readonly
Returns the value of attribute auto_scopes.
-
#events ⇒ Object
Returns the value of attribute events.
- #initial_state ⇒ Object
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#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, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#initialize(klass, options = {}, &block) ⇒ Machine
constructor
A new instance of Machine.
- #update(options = {}, &block) ⇒ Object
Constructor Details
#initialize(klass, options = {}, &block) ⇒ Machine
Returns a new instance of Machine.
7 8 9 10 11 12 13 |
# File 'lib/transitions/machine.rb', line 7 def initialize(klass, = {}, &block) @klass = klass @states = [] @state_index = {} @events = {} update(, &block) end |
Instance Attribute Details
#auto_scopes ⇒ Object (readonly)
Returns the value of attribute auto_scopes.
5 6 7 |
# File 'lib/transitions/machine.rb', line 5 def auto_scopes @auto_scopes end |
#events ⇒ Object
Returns the value of attribute events.
4 5 6 |
# File 'lib/transitions/machine.rb', line 4 def events @events end |
#initial_state ⇒ Object
15 16 17 |
# File 'lib/transitions/machine.rb', line 15 def initial_state @initial_state ||= (states.first ? states.first.name : nil) end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/transitions/machine.rb', line 5 def klass @klass end |
#state_index ⇒ Object
Returns the value of attribute state_index.
4 5 6 |
# File 'lib/transitions/machine.rb', line 4 def state_index @state_index end |
#states ⇒ Object
Returns the value of attribute states.
4 5 6 |
# File 'lib/transitions/machine.rb', line 4 def states @states end |
Instance Method Details
#current_state_variable ⇒ Object
53 54 55 56 |
# File 'lib/transitions/machine.rb', line 53 def current_state_variable # TODO: Refactor me away. :@current_state end |
#events_for(state) ⇒ Object
48 49 50 51 |
# File 'lib/transitions/machine.rb', line 48 def events_for(state) events = @events.values.select { |event| event.transitions_from_state?(state) } events.map!(&:name) end |
#fire_event(event, record, persist, *args, **kwargs) ⇒ Object
rubocop:disable Metrics/MethodLength
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/transitions/machine.rb', line 30 def fire_event(event, record, persist, *args, **kwargs) handle_state_exit_callback record if new_state = transition_to_new_state(record, event, *args, **kwargs) handle_state_enter_callback record, new_state handle_event_fired_callback record, new_state, event record.update_current_state(new_state, persist) handle_event_success_callback record, event return true else handle_event_failed_callback record, event return false end rescue => e raise e unless record.respond_to?(:event_failed) record.send(:event_failed, event) return false end |
#update(options = {}, &block) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/transitions/machine.rb', line 19 def update( = {}, &block) @initial_state = [:initial] if .key?(:initial) @auto_scopes = [:auto_scopes] instance_eval(&block) if block include_scopes if @auto_scopes && ::Transitions.active_model_descendant?(klass) self end |