Module: Sequel::Plugins::StateMachine::ClassMethods

Defined in:
lib/sequel/plugins/state_machine.rb

Instance Method Summary collapse

Instance Method Details

#timestamp_accessor(event, accessor) ⇒ Object

Register the timestamp access for an event. A timestamp accessor reads when a certain transition happened by looking at the timestamp of the successful transition into that state.

The event can be just the event name, or a hash of <event method symbol>, from: <state name>, used when a single event can cause multiple transitions.



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/sequel/plugins/state_machine.rb', line 249

def timestamp_accessor(event, accessor)
  define_method(accessor) do
    event = {event: event} if event.is_a?(String)
    audit = self.audit_logs.select(&:succeeded?).find do |a|
      ev_match = event[:event].nil? || event[:event] == a.event
      from_match = event[:from].nil? || event[:from] == a.from_state
      to_match = event[:to].nil? || event[:to] == a.to_state
      ev_match && from_match && to_match
    end
    return audit&.at
  end
end

#timestamp_accessors(events_and_accessors) ⇒ Object



237
238
239
240
241
# File 'lib/sequel/plugins/state_machine.rb', line 237

def timestamp_accessors(events_and_accessors)
  events_and_accessors.each do |(ev, acc)|
    self.timestamp_accessor(ev, acc)
  end
end