Module: Stance::Eventable

Extended by:
ActiveSupport::Concern
Defined in:
lib/stance/eventable.rb

Instance Method Summary collapse

Instance Method Details

#ensure_event!(name) ⇒ Object

Raise EventNotFound if the event has not been defined.



60
61
62
63
64
# File 'lib/stance/eventable.rb', line 60

def ensure_event!(name)
  return if events_class.events.one? { |event, options| name == event && !options[:class] }

  raise Stance::EventNotFound, "Event `#{name}` not found in #{events_class}"
end

#event_class(name) ⇒ Object

The class which defines the event. There will be only one.



75
76
77
78
79
# File 'lib/stance/eventable.rb', line 75

def event_class(name)
  "#{events_class_name}::#{name.tr('.', '/').classify}".constantize
rescue NameError
  Stance::Event
end

#events_classObject



66
67
68
# File 'lib/stance/eventable.rb', line 66

def events_class
  @events_class ||= events_class_name.constantize
end

#events_class_nameObject



70
71
72
# File 'lib/stance/eventable.rb', line 70

def events_class_name
  @events_class_name ||= "#{self.class.name}Events"
end

#publish_event(name, metadata = {}) ⇒ Object

Publish an event.

Creates an EventRecord with the given ‘name`, `metadata` and self as the ’subject’.

Returns the results of ‘call`ing the event class.



55
56
57
# File 'lib/stance/eventable.rb', line 55

def publish_event(name,  = {})
  self.class.publish_event name, , self
end