Class: StateMachine::MachineCollection
- Inherits:
-
Hash
- Object
- Hash
- StateMachine::MachineCollection
- Defined in:
- lib/state_machine/machine_collection.rb
Overview
Represents a collection of state machines for a class
Instance Method Summary collapse
-
#fire_events(object, *events) ⇒ Object
Runs one or more events in parallel on the given object.
-
#initialize_states(object, options = {}) ⇒ Object
Initializes the state of each machine in the given object.
-
#transitions(object, action, options = {}) ⇒ Object
Builds the collection of transitions for all event attributes defined on the given object.
Instance Method Details
#fire_events(object, *events) ⇒ Object
Runs one or more events in parallel on the given object. See StateMachine::InstanceMethods#fire_events for more information.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/state_machine/machine_collection.rb', line 22 def fire_events(object, *events) run_action = [true, false].include?(events.last) ? events.pop : true # Generate the transitions to run for each event transitions = events.collect do |event_name| # Find the actual event being run event = nil detect {|name, machine| event = machine.events[event_name, :qualified_name]} raise InvalidEvent, "#{event_name.inspect} is an unknown state machine event" unless event # Get the transition that will be performed for the event unless transition = event.transition_for(object) machine = event.machine machine.invalidate(object, :state, :invalid_transition, [[:event, event.human_name]]) end transition end.compact # Run the events in parallel only if valid transitions were found for # all of them if events.length == transitions.length TransitionCollection.new(transitions, :actions => run_action).perform else false end end |
#initialize_states(object, options = {}) ⇒ Object
Initializes the state of each machine in the given object. Initial values are only set if the machine’s attribute doesn’t already exist (which must mean the defaults are being skipped)
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/state_machine/machine_collection.rb', line 7 def initialize_states(object, = {}) if ignore = [:ignore] ignore = ignore.map {|attribute| attribute.to_sym} end each_value do |machine| if (!ignore || !ignore.include?(machine.attribute)) && (!.include?(:dynamic) || machine.dynamic_initial_state? == [:dynamic]) value = machine.read(object, :state) machine.initialize_state(object) if ignore || value.nil? || value.respond_to?(:empty?) && value.empty? end end end |
#transitions(object, action, options = {}) ⇒ Object
Builds the collection of transitions for all event attributes defined on the given object. This will only include events whose machine actions match the one specified.
These should only be fired as a result of the action being run.
56 57 58 59 60 61 62 |
# File 'lib/state_machine/machine_collection.rb', line 56 def transitions(object, action, = {}) transitions = map do |name, machine| machine.events.attribute_transition_for(object, true) if machine.action == action end AttributeTransitionCollection.new(transitions.compact, ) end |