Class | StateMachine::MachineCollection |
In: |
lib/state_machine/machine_collection.rb
|
Parent: | Hash |
Represents a collection of state machines for a class
Runs one or more events in parallel on the given object. See StateMachine::InstanceMethods#fire_events for more information.
# File lib/state_machine/machine_collection.rb, line 45 45: def fire_events(object, *events) 46: run_action = [true, false].include?(events.last) ? events.pop : true 47: 48: # Generate the transitions to run for each event 49: transitions = events.collect do |event_name| 50: # Find the actual event being run 51: event = nil 52: detect {|name, machine| event = machine.events[event_name, :qualified_name]} 53: 54: raise(InvalidEvent.new(object, event_name)) unless event 55: 56: # Get the transition that will be performed for the event 57: unless transition = event.transition_for(object) 58: machine = event.machine 59: event.on_failure(object) 60: end 61: 62: transition 63: end.compact 64: 65: # Run the events in parallel only if valid transitions were found for 66: # all of them 67: if events.length == transitions.length 68: TransitionCollection.new(transitions, :actions => run_action).perform 69: else 70: false 71: end 72: end
Initializes the state of each machine in the given object. This can allow states to be initialized in two groups: static and dynamic. For example:
machines.initialize_states(object) do # After static state initialization, before dynamic state initialization end
If no block is provided, then all states will still be initialized.
Valid configuration options:
# File lib/state_machine/machine_collection.rb, line 26 26: def initialize_states(object, options = {}) 27: assert_valid_keys(options, :static, :dynamic, :to) 28: options = {:static => :force, :dynamic => true}.merge(options) 29: 30: each_value do |machine| 31: machine.initialize_state(object, :force => options[:static] == :force, :to => options[:to]) unless machine.dynamic_initial_state? 32: end if options[:static] 33: 34: result = yield if block_given? 35: 36: each_value do |machine| 37: machine.initialize_state(object, :force => options[:dynamic] == :force, :to => options[:to]) if machine.dynamic_initial_state? 38: end if options[:dynamic] 39: 40: result 41: end
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.
# File lib/state_machine/machine_collection.rb, line 79 79: def transitions(object, action, options = {}) 80: transitions = map do |name, machine| 81: machine.events.attribute_transition_for(object, true) if machine.action == action 82: end 83: 84: AttributeTransitionCollection.new(transitions.compact, options) 85: end