Class: AASM::SupportingClasses::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Event

Returns a new instance of Event.



8
9
10
11
12
13
14
# File 'lib/aasm/event.rb', line 8

def initialize(name, options = {}, &block)
  @name = name
  @success = options[:success]
  @transitions = []
  @options = options
  instance_eval(&block) if block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#successObject (readonly)

Returns the value of attribute success.



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

def success
  @success
end

Instance Method Details

#all_transitionsObject



64
65
66
# File 'lib/aasm/event.rb', line 64

def all_transitions
  @transitions
end

#call_action(action, record) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/aasm/event.rb', line 52

def call_action(action, record)
  action = @options[action]
  case action
  when Symbol, String
    record.send(action)
  when Proc
    action.call(record)
  when Array
    action.each { |a| record.send(a) }
  end
end

#execute_success_callback(obj, success = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/aasm/event.rb', line 40

def execute_success_callback(obj, success = nil)
  callback = success || @success
  case(callback)
  when String, Symbol
    obj.send(callback)
  when Proc
    callback.call(obj)
  when Array
    callback.each{|meth|self.execute_success_callback(obj, meth)}
  end
end

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/aasm/event.rb', line 16

def fire(obj, to_state=nil, *args)
  transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
  raise AASM::InvalidTransition, "Event '#{name}' cannot transition from '#{obj.aasm_current_state}'" if transitions.size == 0

  next_state = nil
  transitions.each do |transition|
    next if to_state and !Array(transition.to).include?(to_state)
    if transition.perform(obj)
      next_state = to_state || Array(transition.to).first
      transition.execute(obj, *args)
      break
    end
  end
  next_state
end

#transitions_from_state(state) ⇒ Object



36
37
38
# File 'lib/aasm/event.rb', line 36

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

#transitions_from_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/aasm/event.rb', line 32

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