Module: StateMachines::Machine::EventMethods
- Included in:
- StateMachines::Machine
- Defined in:
- lib/state_machines/machine/event_methods.rb
Instance Method Summary collapse
-
#event(*names) ⇒ Object
(also: #on)
Defines one or more events for the machine and the transitions that can be performed when those events are run.
-
#paths_for(object, requirements = {}) ⇒ Object
Gets the list of all possible transition paths from the current state to the given target state.
-
#transition(options) ⇒ Object
Creates a new transition that determines what to change the current state to when an event fires.
Instance Method Details
#event(*names) ⇒ Object Also known as: on
Defines one or more events for the machine and the transitions that can be performed when those events are run.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/state_machines/machine/event_methods.rb', line 8 def event(*names, &) = names.last.is_a?(Hash) ? names.pop : {} StateMachines::OptionsValidator.assert_valid_keys!(, :human_name) # Store the context so that it can be used for / matched against any event # that gets added @events.context(names, &) if block_given? if names.first.is_a?(Matcher) # Add any events referenced in the matcher. When matchers are used, # events are not allowed to be configured. raise ArgumentError, "Cannot configure events when using matchers (using #{.inspect})" if .any? events = add_events(names.first.values) else events = add_events(names) # Update the configuration for the event(s) events.each do |event| event.human_name = [:human_name] if .include?(:human_name) # Add any states that may have been referenced within the event add_states(event.known_states) end end events.length == 1 ? events.first : events end |
#paths_for(object, requirements = {}) ⇒ Object
Gets the list of all possible transition paths from the current state to the given target state. If multiple target states are provided, then this will return all possible paths to those states.
54 55 56 |
# File 'lib/state_machines/machine/event_methods.rb', line 54 def paths_for(object, requirements = {}) PathCollection.new(object, self, requirements) end |
#transition(options) ⇒ Object
Creates a new transition that determines what to change the current state to when an event fires.
41 42 43 44 45 46 47 48 49 |
# File 'lib/state_machines/machine/event_methods.rb', line 41 def transition() raise ArgumentError, 'Must specify :on event' unless [:on] branches = [] = .dup event(*Array(.delete(:on))) { branches << transition() } branches.length == 1 ? branches.first : branches end |