class Stance::Event

Attributes

options[R]
record[R]

Public Class Methods

after_create(*methods, &block) click to toggle source
# File lib/stance/event.rb, line 17
def after_create(*methods, &block)
  set_callback :create, :after, *methods, &block
end
before_create(*methods, &block) click to toggle source
# File lib/stance/event.rb, line 13
def before_create(*methods, &block)
  set_callback :create, :before, *methods, &block
end
new(name, subject, metadata, options) click to toggle source
# File lib/stance/event.rb, line 22
def initialize(name, subject, metadata, options)
  @options = { singleton: false, record: true, class: false }.merge(options)

  attrs = { name: name, metadata: metadata }
  if subject.is_a?(String)
    attrs[:subject_type] = subject
  else
    attrs[:subject] = subject
  end
  @record = Stance::EventRecord.new(attrs)
end

Public Instance Methods

create() click to toggle source
# File lib/stance/event.rb, line 34
def create
  return self if singleton_exists?

  Stance::EventRecord.transaction do
    run_callbacks :create do
      # Call each public method of the Event class if a custom class.
      if self.class.name != 'Stance::Event'
        Rails.logger.info "Event: #{full_name}"

        (public_methods(false) - Stance::Event.instance_methods(false)).each do |method|
          send method
        end
      end

      record.save if @options[:record]
    end
  end

  self
end
full_name() click to toggle source
# File lib/stance/event.rb, line 55
def full_name
  "#{record.subject_type.downcase}.#{name}"
end

Private Instance Methods

singleton_exists?() click to toggle source

Event is a singleton and already exists.

# File lib/stance/event.rb, line 62
def singleton_exists?
  options[:singleton] && subject.events.active.exists?(name: name)
end