Class: Transitions::Event
- Inherits:
-
Object
- Object
- Transitions::Event
- Defined in:
- lib/transitions/event.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #==(event) ⇒ Object
- #can_execute_transition_from_state?(state, obj, *args) ⇒ Boolean
- #fire(obj, to_state = nil, *args) ⇒ Object
-
#initialize(machine, name, options = {}, &block) ⇒ Event
constructor
A new instance of Event.
-
#timestamp_defined? ⇒ Boolean
Has the timestamp option been specified for this event?.
- #transitions_from_state?(state) ⇒ Boolean
- #update(options = {}, &block) ⇒ Object
-
#update_event_timestamp(obj, next_state) ⇒ Object
update the timestamp attribute on obj.
Constructor Details
#initialize(machine, name, options = {}, &block) ⇒ Event
Returns a new instance of Event.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/transitions/event.rb', line 5 def initialize(machine, name, = {}, &block) @machine, @name, @transitions = machine, name, [] if machine machine.klass.send(:define_method, "#{name}!") do |*args| machine.fire_event(name, self, true, *args) end machine.klass.send(:define_method, name.to_s) do |*args| machine.fire_event(name, self, false, *args) end machine.klass.send(:define_method, "can_#{name}?") do |*_args| machine.events_for(current_state).include?(name.to_sym) end machine.klass.send(:define_method, "can_execute_#{name}?") do |*args| event = name.to_sym send("can_#{name}?", *args) && machine.events[event].can_execute_transition_from_state?(current_state, self, *args) end end update(, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/transitions/event.rb', line 3 def name @name end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
3 4 5 |
# File 'lib/transitions/event.rb', line 3 def success @success end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
3 4 5 |
# File 'lib/transitions/event.rb', line 3 def @timestamp end |
Instance Method Details
#==(event) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/transitions/event.rb', line 56 def ==(event) if event.is_a? Symbol name == event else name == event.name end end |
#can_execute_transition_from_state?(state, obj, *args) ⇒ Boolean
52 53 54 |
# File 'lib/transitions/event.rb', line 52 def can_execute_transition_from_state?(state, obj, *args) @transitions.select { |t| t.from? state }.any? { |t| t.executable?(obj, *args) } end |
#fire(obj, to_state = nil, *args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/transitions/event.rb', line 30 def fire(obj, to_state = nil, *args) transitions = @transitions.select { |t| t.from == obj.current_state } fail InvalidTransition, (obj) if transitions.size == 0 next_state = nil transitions.each do |transition| next if to_state && !Array(transition.to).include?(to_state) if transition.executable?(obj, *args) next_state = to_state || Array(transition.to).first transition.execute(obj, *args) (obj, next_state) if break end end # Update timestamps on obj if a timestamp has been defined next_state end |
#timestamp_defined? ⇒ Boolean
Has the timestamp option been specified for this event?
65 66 67 |
# File 'lib/transitions/event.rb', line 65 def !@timestamp.nil? end |
#transitions_from_state?(state) ⇒ Boolean
48 49 50 |
# File 'lib/transitions/event.rb', line 48 def transitions_from_state?(state) @transitions.any? { |t| t.from? state } end |
#update(options = {}, &block) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/transitions/event.rb', line 69 def update( = {}, &block) @success = build_success_callback([:success]) if .key?(:success) self. = [:timestamp] if [:timestamp] instance_eval(&block) if block self end |
#update_event_timestamp(obj, next_state) ⇒ Object
update the timestamp attribute on obj
77 78 79 |
# File 'lib/transitions/event.rb', line 77 def (obj, next_state) obj.send "#{(obj, next_state)}=", Time.now end |