Class: StatefulModelRails::StateMachine::StateMachineInternal
- Inherits:
-
Object
- Object
- StatefulModelRails::StateMachine::StateMachineInternal
- Defined in:
- lib/stateful_model_rails/state_machine.rb
Instance Attribute Summary collapse
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#seen_states ⇒ Object
readonly
Returns the value of attribute seen_states.
-
#transition_callback ⇒ Object
readonly
Returns the value of attribute transition_callback.
-
#transition_map ⇒ Object
readonly
Returns the value of attribute transition_map.
Instance Method Summary collapse
-
#initialize(field_name) ⇒ StateMachineInternal
constructor
A new instance of StateMachineInternal.
- #install_event_helpers!(base) ⇒ Object
- #record_transition_with(&block) ⇒ Object
- #transition(event, from:, to:) ⇒ Object
Constructor Details
#initialize(field_name) ⇒ StateMachineInternal
Returns a new instance of StateMachineInternal.
67 68 69 70 71 72 |
# File 'lib/stateful_model_rails/state_machine.rb', line 67 def initialize(field_name) @field_name = field_name @seen_states = [] @transition_map = {} @transition_callback = nil end |
Instance Attribute Details
#field_name ⇒ Object (readonly)
Returns the value of attribute field_name.
65 66 67 |
# File 'lib/stateful_model_rails/state_machine.rb', line 65 def field_name @field_name end |
#seen_states ⇒ Object (readonly)
Returns the value of attribute seen_states.
65 66 67 |
# File 'lib/stateful_model_rails/state_machine.rb', line 65 def seen_states @seen_states end |
#transition_callback ⇒ Object (readonly)
Returns the value of attribute transition_callback.
65 66 67 |
# File 'lib/stateful_model_rails/state_machine.rb', line 65 def transition_callback @transition_callback end |
#transition_map ⇒ Object (readonly)
Returns the value of attribute transition_map.
65 66 67 |
# File 'lib/stateful_model_rails/state_machine.rb', line 65 def transition_map @transition_map end |
Instance Method Details
#install_event_helpers!(base) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/stateful_model_rails/state_machine.rb', line 90 def install_event_helpers!(base) events = @transition_map.keys events.each do |event| fromtos = @transition_map[event] base.instance_eval do define_method(event) do |**kwargs| with_lock do matching_froms = fromtos.select { |fr| fr.from == state.class } raise TooManyDestinationStates if matching_froms.length > 1 raise StatefulModelRails::NoMatchingTransition.new(state.name, event.to_s) if matching_froms.empty? matching_from = matching_froms[0] from_state = matching_from.from.new(self) to_state = matching_from.to.new(self) from_state.run_before_leave if from_state.respond_to?(:run_before_leave) to_state.run_before_enter if to_state.respond_to?(:run_before_enter) update!(self.class.state_machine_instance.field_name.to_sym => matching_from.to.name) self.class.state_machine_instance.transition_callback&.call(self, matching_from.from.name, matching_from.to.name, **kwargs) from_state.run_after_leave if from_state.respond_to?(:run_after_leave) to_state.run_after_enter if to_state.respond_to?(:run_after_enter) end end end end end |
#record_transition_with(&block) ⇒ Object
86 87 88 |
# File 'lib/stateful_model_rails/state_machine.rb', line 86 def record_transition_with(&block) @transition_callback = block end |
#transition(event, from:, to:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/stateful_model_rails/state_machine.rb', line 74 def transition(event, from:, to:) @transition_map[event] ||= [] Array(from).each do |from| @transition_map[event] << StatefulModelRails::Transition.new(from, to) @seen_states << from end @seen_states << to @seen_states.uniq! end |