Class: AASM::Core::Event
- Inherits:
-
Object
- Object
- AASM::Core::Event
- Includes:
- DslHelper
- Defined in:
- lib/aasm/core/event.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#state_machine ⇒ Object
readonly
Returns the value of attribute state_machine.
Instance Method Summary collapse
- #==(event) ⇒ Object
- #failed_callbacks ⇒ Object
- #fire(obj, options = {}, to_state = nil, *args) ⇒ Object
- #fire_callbacks(callback_name, record, *args) ⇒ Object
- #fire_global_callbacks(callback_name, record, *args) ⇒ Object
- #fire_transition_callbacks(obj, *args) ⇒ Object
-
#initialize(name, state_machine, options = {}, &block) ⇒ Event
constructor
A new instance of Event.
-
#may_fire?(obj, to_state = nil, *args) ⇒ Boolean
a neutered version of fire - it doesn’t actually fire the event, it just executes the transition guards to determine if a transition is even an option given current conditions.
-
#transitions(definitions = nil, &block) ⇒ Object
DSL interface.
- #transitions_from_state(state) ⇒ Object
- #transitions_from_state?(state) ⇒ Boolean
- #transitions_to_state(state) ⇒ Object
- #transitions_to_state?(state) ⇒ Boolean
Methods included from DslHelper
Constructor Details
#initialize(name, state_machine, options = {}, &block) ⇒ Event
Returns a new instance of Event.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aasm/core/event.rb', line 7 def initialize(name, state_machine, = {}, &block) @name = name @state_machine = state_machine @transitions = [] @valid_transitions = {} @guards = Array([:guard] || [:guards] || [:if]) @unless = Array([:unless]) #TODO: This could use a better name # from aasm4 @options = # QUESTION: .dup ? (@options, [ :after, :after_commit, :after_transaction, :before, :before_transaction, :ensure, :error, :success, ], &block) if block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/aasm/core/event.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/aasm/core/event.rb', line 5 def @options end |
#state_machine ⇒ Object (readonly)
Returns the value of attribute state_machine.
5 6 7 |
# File 'lib/aasm/core/event.rb', line 5 def state_machine @state_machine end |
Instance Method Details
#==(event) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/aasm/core/event.rb', line 72 def ==(event) if event.is_a? Symbol name == event else name == event.name end end |
#failed_callbacks ⇒ Object
95 96 97 |
# File 'lib/aasm/core/event.rb', line 95 def failed_callbacks transitions.flat_map(&:failures) end |
#fire(obj, options = {}, to_state = nil, *args) ⇒ Object
36 37 38 |
# File 'lib/aasm/core/event.rb', line 36 def fire(obj, ={}, to_state=nil, *args) _fire(obj, , to_state, *args) # false indicates this is not a test (fire!) end |
#fire_callbacks(callback_name, record, *args) ⇒ Object
60 61 62 63 64 |
# File 'lib/aasm/core/event.rb', line 60 def fire_callbacks(callback_name, record, *args) # strip out the first element in args if it's a valid to_state # #given where we're coming from, this condition implies args not empty invoke_callbacks(@options[callback_name], record, args) end |
#fire_global_callbacks(callback_name, record, *args) ⇒ Object
56 57 58 |
# File 'lib/aasm/core/event.rb', line 56 def fire_global_callbacks(callback_name, record, *args) invoke_callbacks(state_machine.global_callbacks[callback_name], record, args) end |
#fire_transition_callbacks(obj, *args) ⇒ Object
66 67 68 69 70 |
# File 'lib/aasm/core/event.rb', line 66 def fire_transition_callbacks(obj, *args) from_state = obj.aasm(state_machine.name).current_state transition = @valid_transitions[from_state] @valid_transitions[from_state].invoke_success_callbacks(obj, *args) if transition end |
#may_fire?(obj, to_state = nil, *args) ⇒ Boolean
a neutered version of fire - it doesn’t actually fire the event, it just executes the transition guards to determine if a transition is even an option given current conditions.
32 33 34 |
# File 'lib/aasm/core/event.rb', line 32 def may_fire?(obj, to_state=nil, *args) _fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing end |
#transitions(definitions = nil, &block) ⇒ Object
DSL interface
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/aasm/core/event.rb', line 81 def transitions(definitions=nil, &block) if definitions # define new transitions # Create a separate transition for each from-state to the given state Array(definitions[:from]).each do |s| @transitions << AASM::Core::Transition.new(self, attach_event_guards(definitions.merge(:from => s.to_sym)), &block) end # Create a transition if :to is specified without :from (transitions from ANY state) if @transitions.empty? && definitions[:to] @transitions << AASM::Core::Transition.new(self, attach_event_guards(definitions), &block) end end @transitions end |
#transitions_from_state(state) ⇒ Object
44 45 46 |
# File 'lib/aasm/core/event.rb', line 44 def transitions_from_state(state) @transitions.select { |t| t.from.nil? or t.from == state } end |
#transitions_from_state?(state) ⇒ Boolean
40 41 42 |
# File 'lib/aasm/core/event.rb', line 40 def transitions_from_state?(state) transitions_from_state(state).any? end |
#transitions_to_state(state) ⇒ Object
52 53 54 |
# File 'lib/aasm/core/event.rb', line 52 def transitions_to_state(state) @transitions.select { |t| t.to == state } end |
#transitions_to_state?(state) ⇒ Boolean
48 49 50 |
# File 'lib/aasm/core/event.rb', line 48 def transitions_to_state?(state) transitions_to_state(state).any? end |