Class: AASM::SupportingClasses::Event
- Inherits:
-
Object
- Object
- AASM::SupportingClasses::Event
- Defined in:
- lib/alexrevin-aasm_numerical/event.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
- #==(event) ⇒ Object
- #all_transitions ⇒ Object
- #call_action(action, record) ⇒ Object
- #execute_error_callback(obj, error, error_callback = nil) ⇒ Object
- #execute_success_callback(obj, success = nil) ⇒ Object
- #fire(obj, to_state = nil, *args) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Event
constructor
A new instance of Event.
-
#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.
- #transitions_from_state(state) ⇒ Object
- #transitions_from_state?(state) ⇒ Boolean
- #update(options = {}, &block) ⇒ Object
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, = {}, &block) @name = name @transitions = [] update(, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
2 3 4 |
# File 'lib/alexrevin-aasm_numerical/event.rb', line 2 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/alexrevin-aasm_numerical/event.rb', line 2 def @options end |
#success ⇒ Object (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_transitions ⇒ Object
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.
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
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( = {}, &block) if .key?(:success) then @success = [:success] end if .key?(:error) then @error = [:error] end if block then instance_eval(&block) end @options = self end |