Class: Transitions::Event
- Inherits:
-
Object
- Object
- Transitions::Event
- Defined in:
- lib/transitions/event.rb
Overview
rubocop:disable Metrics/ClassLength
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
- #==(other) ⇒ Object
- #can_execute_transition_from_state?(state, obj, *args, **kwargs) ⇒ Boolean
- #fire(obj, to_state = nil, *args, **kwargs) ⇒ Object
-
#initialize(machine, name, options = {}, &block) ⇒ Event
constructor
:reek:TooManyStatements { max_statements: 13 }.
-
#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
:reek:TooManyStatements { max_statements: 13 }
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/transitions/event.rb', line 7 def initialize(machine, name, = {}, &block) @machine = machine @name = name @transitions = [] @timestamps = [] if machine machine.klass.send(:define_method, "#{name}!") do |*args, **kwargs| machine.fire_event(name, self, true, *args, **kwargs) end machine.klass.send(:define_method, name.to_s) do |*args, **kwargs| machine.fire_event(name, self, false, *args, **kwargs) 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, **kwargs| event = name.to_sym send("can_#{name}?", *args, **kwargs) && machine.events[event].can_execute_transition_from_state?(current_state, self, *args, **kwargs) end end update(, &block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/transitions/event.rb', line 4 def name @name end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
4 5 6 |
# File 'lib/transitions/event.rb', line 4 def success @success end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
4 5 6 |
# File 'lib/transitions/event.rb', line 4 def @timestamp end |
Instance Method Details
#==(other) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/transitions/event.rb', line 61 def ==(other) if other.is_a? Symbol name == other else name == other.name end end |
#can_execute_transition_from_state?(state, obj, *args, **kwargs) ⇒ Boolean
57 58 59 |
# File 'lib/transitions/event.rb', line 57 def can_execute_transition_from_state?(state, obj, *args, **kwargs) @transitions.select { |t| t.from? state }.any? { |t| t.executable?(obj, *args, **kwargs) } end |
#fire(obj, to_state = nil, *args, **kwargs) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/transitions/event.rb', line 35 def fire(obj, to_state = nil, *args, **kwargs) 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) next unless transition.executable?(obj, *args, **kwargs) next_state = to_state || Array(transition.to).first transition.execute(obj, *args, **kwargs) (obj, next_state) if break 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?
70 71 72 |
# File 'lib/transitions/event.rb', line 70 def !@timestamps.nil? end |
#transitions_from_state?(state) ⇒ Boolean
53 54 55 |
# File 'lib/transitions/event.rb', line 53 def transitions_from_state?(state) @transitions.any? { |t| t.from? state } end |
#update(options = {}, &block) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/transitions/event.rb', line 74 def update( = {}, &block) @success = build_success_callback([:success]) if .key?(:success) self. = Array([:timestamp]) if [:timestamp] instance_eval(&block) if block self end |
#update_event_timestamp(obj, next_state) ⇒ Object
update the timestamp attribute on obj
82 83 84 85 86 |
# File 'lib/transitions/event.rb', line 82 def (obj, next_state) @timestamps.each do || obj.public_send "#{(obj, next_state, )}=", Time.now end end |