Sha256: 2f622d545dbc56dc78e3564a18056832d4650a2f26b5736078bd6dd536841e59

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Stance
  class Event
    include ActiveSupport::Callbacks

    define_callbacks :create
    attr_reader :record, :options

    delegate :subject, :name, to: :record

    class << self
      def before_create(*methods, &block)
        set_callback :create, :before, *methods, &block
      end

      def after_create(*methods, &block)
        set_callback :create, :after, *methods, &block
      end
    end

    def initialize(name, subject, metadata, options)
      @options = { singleton: false }.merge(options)
      @record = Stance::EventRecord.new(name: name, subject: subject, metadata: metadata)
    end

    def create
      return self if singleton_exists?

      Stance::EventRecord.transaction do
        run_callbacks :create do
          record.save
        end
      end

      self
    end

    def full_name
      "#{subject.model_name.singular}.#{name}"
    end

    private

    # Event is a singleton and already exists.
    def singleton_exists?
      options[:singleton] && subject.events.active.exists?(name: name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stance-0.3.0 lib/stance/event.rb
stance-0.2.0 lib/stance/event.rb