Class: AASM::SupportingClasses::Event

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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/alexrevin-aasm_numerical/event.rb', line 2

def options
  @options
end

#successObject (readonly)

Returns the value of attribute success.



2
3
4
# File 'lib/alexrevin-aasm_numerical/event.rb', line 2

def success
  @success
end

Instance Method Details

#==(event) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/alexrevin-aasm_numerical/event.rb', line 63

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

#all_transitionsObject



52
53
54
# File 'lib/alexrevin-aasm_numerical/event.rb', line 52

def all_transitions
  @transitions
end

#call_action(action, record) ⇒ Object



56
57
58
59
60
61
# File 'lib/alexrevin-aasm_numerical/event.rb', line 56

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



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/alexrevin-aasm_numerical/event.rb', line 97

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



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/alexrevin-aasm_numerical/event.rb', line 85

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alexrevin-aasm_numerical/event.rb', line 28

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, *args)
      next_state = to_state || Array(transition.to).first
      transition.execute(obj, *args)
      break
    end
  end
  next_state
end

#may_fire?(obj, to_state = nil) ⇒ Boolean

a neutered version of fire - it doesn’t actually fir the event, it just executes the transition guards to determine if a transition is even an option given current conditions.

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alexrevin-aasm_numerical/event.rb', line 13

def may_fire?(obj, to_state=nil)
  transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
  return false if transitions.size == 0
  
  result = false
  transitions.each do |transition|
    next if to_state and !Array(transition.to).include?(to_state)
    if transition.perform(obj)
      result = true
      break
    end
  end
  result
end

#transitions_from_state(state) ⇒ Object



48
49
50
# File 'lib/alexrevin-aasm_numerical/event.rb', line 48

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

#transitions_from_state?(state) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/alexrevin-aasm_numerical/event.rb', line 44

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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/alexrevin-aasm_numerical/event.rb', line 71

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