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.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/aasm/core/event.rb', line 7

def initialize(name, state_machine, options = {}, &block)
  @name = name
  @state_machine = state_machine
  @transitions = []
  @guards = Array(options[:guard] || options[:guards] || options[:if])
  @unless = Array(options[:unless]) #TODO: This could use a better name

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/aasm/core/event.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/aasm/core/event.rb', line 5

def options
  @options
end

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



52
53
54
55
56
57
58
# File 'lib/aasm/core/event.rb', line 52

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

#fire(obj, options = {}, to_state = nil, *args) ⇒ Object



26
27
28
# File 'lib/aasm/core/event.rb', line 26

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

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



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

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

#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.

Returns:

  • (Boolean)


22
23
24
# File 'lib/aasm/core/event.rb', line 22

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aasm/core/event.rb', line 61

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



34
35
36
# File 'lib/aasm/core/event.rb', line 34

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

#transitions_from_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/aasm/core/event.rb', line 30

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

#transitions_to_state(state) ⇒ Object



42
43
44
# File 'lib/aasm/core/event.rb', line 42

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

#transitions_to_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/aasm/core/event.rb', line 38

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