Class: AASM::SupportingClasses::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/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
# File 'lib/event.rb', line 8

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#successObject (readonly)

Returns the value of attribute success.



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

def success
  @success
end

Instance Method Details

#execute_success_callback(obj) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/event.rb', line 37

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

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



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

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
    else
      raise AASM::TransitionGuarded, "Event '#{name}' was guarded against transition from '#{obj.aasm_current_state}'"
    end
  end
  next_state
end

#transitions_from_state?(state, context = :all) ⇒ Boolean

Returns:

  • (Boolean)


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

def transitions_from_state?(state, context=:all)
  @transitions.any? { |t| t.from == state && t.has_context?(context) }
end