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 = ::AASM::NO_VALUE, *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.
-
#initialize_copy(orig) ⇒ Object
called internally by Ruby 1.9 after clone().
-
#may_fire?(obj, to_state = ::AASM::NO_VALUE, *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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/aasm/core/event.rb', line 9 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 = # QUESTION: .dup ? (, [ :after, :after_commit, :after_transaction, :before, :before_transaction, :ensure, :error, :before_success, :success, ], &block) if block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/aasm/core/event.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/aasm/core/event.rb', line 7 def end |
#state_machine ⇒ Object (readonly)
Returns the value of attribute state_machine.
7 8 9 |
# File 'lib/aasm/core/event.rb', line 7 def state_machine @state_machine end |
Instance Method Details
#==(event) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/aasm/core/event.rb', line 85 def ==(event) if event.is_a? Symbol name == event else name == event.name end end |
#failed_callbacks ⇒ Object
108 109 110 |
# File 'lib/aasm/core/event.rb', line 108 def failed_callbacks transitions.flat_map(&:failures) end |
#fire(obj, options = {}, to_state = ::AASM::NO_VALUE, *args) ⇒ Object
49 50 51 |
# File 'lib/aasm/core/event.rb', line 49 def fire(obj, ={}, to_state=::AASM::NO_VALUE, *args) _fire(obj, , to_state, *args) # false indicates this is not a test (fire!) end |
#fire_callbacks(callback_name, record, *args) ⇒ Object
73 74 75 76 77 |
# File 'lib/aasm/core/event.rb', line 73 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([callback_name], record, args) end |
#fire_global_callbacks(callback_name, record, *args) ⇒ Object
69 70 71 |
# File 'lib/aasm/core/event.rb', line 69 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
79 80 81 82 83 |
# File 'lib/aasm/core/event.rb', line 79 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 |
#initialize_copy(orig) ⇒ Object
called internally by Ruby 1.9 after clone()
33 34 35 36 37 38 39 40 |
# File 'lib/aasm/core/event.rb', line 33 def initialize_copy(orig) super @transitions = @transitions.collect { |transition| transition.clone } @guards = @guards.dup @unless = @unless.dup = {} orig..each_pair { |name, setting| [name] = setting.is_a?(Hash) || setting.is_a?(Array) ? setting.dup : setting } end |
#may_fire?(obj, to_state = ::AASM::NO_VALUE, *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.
45 46 47 |
# File 'lib/aasm/core/event.rb', line 45 def may_fire?(obj, to_state=::AASM::NO_VALUE, *args) _fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing end |
#transitions(definitions = nil, &block) ⇒ Object
DSL interface
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aasm/core/event.rb', line 94 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 !definitions[:from] && definitions[:to] @transitions << AASM::Core::Transition.new(self, attach_event_guards(definitions), &block) end end @transitions end |
#transitions_from_state(state) ⇒ Object
57 58 59 |
# File 'lib/aasm/core/event.rb', line 57 def transitions_from_state(state) @transitions.select { |t| t.from.nil? or t.from == state } end |
#transitions_from_state?(state) ⇒ Boolean
53 54 55 |
# File 'lib/aasm/core/event.rb', line 53 def transitions_from_state?(state) transitions_from_state(state).any? end |
#transitions_to_state(state) ⇒ Object
65 66 67 |
# File 'lib/aasm/core/event.rb', line 65 def transitions_to_state(state) @transitions.select { |t| t.to == state } end |
#transitions_to_state?(state) ⇒ Boolean
61 62 63 |
# File 'lib/aasm/core/event.rb', line 61 def transitions_to_state?(state) transitions_to_state(state).any? end |