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.



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

def initialize(name, options = {}, &block)
  @name = name
  @transitions = []
  update(options, &block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'lib/aasm/event.rb', line 2

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/aasm/event.rb', line 2

def options
  @options
end

#successObject (readonly)

Returns the value of attribute success.



2
3
4
# File 'lib/aasm/event.rb', line 2

def success
  @success
end

Instance Method Details

#==(event) ⇒ Object



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

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

#all_transitionsObject



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

def all_transitions
  @transitions
end

#call_action(action, record) ⇒ Object



38
39
40
41
42
43
# File 'lib/aasm/event.rb', line 38

def call_action(action, record)
  action = @options[action]
  action.is_a?(Array) ?
          action.each {|a| _call_action(a, record)} :
          _call_action(action, record)
end

#execute_error_callback(obj, error, error_callback = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/aasm/event.rb', line 79

def execute_error_callback(obj, error, error_callback=nil)
  callback = error_callback || @error
  raise error unless callback
  case(callback)
    when String, Symbol
      raise NoMethodError unless obj.respond_to?(callback.to_sym)
      obj.send(callback, error)
    when Proc
      callback.call(obj, error)
    when Array
      callback.each{|meth|self.execute_error_callback(obj, error, meth)}
  end
end

#execute_success_callback(obj, success = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aasm/event.rb', line 67

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/aasm/event.rb', line 10

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



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

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

#transitions_from_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#update(options = {}, &block) ⇒ Object



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

def update(options = {}, &block)
  if options.key?(:success) then
    @success = options[:success]
  end
  if options.key?(:error) then
    @error = options[:error]
  end
  if block then
    instance_eval(&block)
  end
  @options = options
  self
end