Class: AASM::Core::Event

Inherits:
Object
  • Object
show all
Includes:
DslHelper
Defined in:
lib/aasm/core/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DslHelper

#add_options_from_dsl

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
31
# File 'lib/aasm/core/event.rb', line 9

def initialize(name, state_machine, options = {}, &block)
  @name = name
  @state_machine = state_machine
  @transitions = []
  @valid_transitions = {}
  @guards = Array(options[:guard] || options[:guards] || options[:if])
  @unless = Array(options[:unless]) #TODO: This could use a better name
  @default_display_name = name.to_s.gsub(/_/, ' ').capitalize

  # from aasm4
  @options = options # QUESTION: .dup ?
  add_options_from_dsl(@options, [
    :after,
    :after_commit,
    :after_transaction,
    :before,
    :before_transaction,
    :ensure,
    :error,
    :before_success,
    :success,
  ], &block) if block
end

Instance Attribute Details

#default_display_nameObject (readonly)

Returns the value of attribute default_display_name


7
8
9
# File 'lib/aasm/core/event.rb', line 7

def default_display_name
  @default_display_name
end

#nameObject (readonly)

Returns the value of attribute name


7
8
9
# File 'lib/aasm/core/event.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


7
8
9
# File 'lib/aasm/core/event.rb', line 7

def options
  @options
end

#state_machineObject (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


86
87
88
89
90
91
92
# File 'lib/aasm/core/event.rb', line 86

def ==(event)
  if event.is_a? Symbol
    name == event
  else
    name == event.name
  end
end

#failed_callbacksObject


109
110
111
# File 'lib/aasm/core/event.rb', line 109

def failed_callbacks
  transitions.flat_map(&:failures)
end

#fire(obj, options = {}, to_state = ::AASM::NO_VALUE, *args) ⇒ Object


50
51
52
# File 'lib/aasm/core/event.rb', line 50

def fire(obj, options={}, to_state=::AASM::NO_VALUE, *args)
  _fire(obj, options, to_state, *args) # false indicates this is not a test (fire!)
end

#fire_callbacks(callback_name, record, *args) ⇒ Object


74
75
76
77
78
# File 'lib/aasm/core/event.rb', line 74

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


70
71
72
# File 'lib/aasm/core/event.rb', line 70

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


80
81
82
83
84
# File 'lib/aasm/core/event.rb', line 80

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()


34
35
36
37
38
39
40
41
# File 'lib/aasm/core/event.rb', line 34

def initialize_copy(orig)
  super
  @transitions = @transitions.collect { |transition| transition.clone }
  @guards      = @guards.dup
  @unless      = @unless.dup
  @options     = {}
  orig.options.each_pair { |name, setting| @options[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.

Returns:

  • (Boolean)

46
47
48
# File 'lib/aasm/core/event.rb', line 46

def may_fire?(obj, to_state=::AASM::NO_VALUE, *args)
  _fire(obj, {:test_only => true}, to_state, *args) # true indicates test firing
end

#to_sObject


113
114
115
# File 'lib/aasm/core/event.rb', line 113

def to_s
  name.to_s
end

#transitions(definitions = nil, &block) ⇒ Object

DSL interface


95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aasm/core/event.rb', line 95

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


58
59
60
# File 'lib/aasm/core/event.rb', line 58

def transitions_from_state(state)
  @transitions.select { |t| t.from.nil? or t.from == state }
end

#transitions_from_state?(state) ⇒ Boolean

Returns:

  • (Boolean)

54
55
56
# File 'lib/aasm/core/event.rb', line 54

def transitions_from_state?(state)
  transitions_from_state(state).any?
end

#transitions_to_state(state) ⇒ Object


66
67
68
# File 'lib/aasm/core/event.rb', line 66

def transitions_to_state(state)
  @transitions.select { |t| t.to == state }
end

#transitions_to_state?(state) ⇒ Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/aasm/core/event.rb', line 62

def transitions_to_state?(state)
  transitions_to_state(state).any?
end