Class: Stance::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/stance/event.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, subject, metadata, options) ⇒ Event

Returns a new instance of Event.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stance/event.rb', line 31

def initialize(name, subject, , options)
  @subject = subject
  @name = name
  @metadata = 
  @options = { singleton: false, record: true, class: false }.merge(options)

  attrs = { name: name, metadata:  }
  if subject.is_a?(ActiveRecord::Base)
    attrs[:subject] = subject
  elsif subject.is_a?(Class) && subject < ActiveRecord::Base
    attrs[:subject_type] = subject.name
  else
    @options[:record] = false
  end

  return unless @options[:record]

  @record = Stance::EventRecord.new(attrs)
end

Class Attribute Details

.callback_methodsObject

Returns the value of attribute callback_methods.



11
12
13
# File 'lib/stance/event.rb', line 11

def callback_methods
  @callback_methods
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/stance/event.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/stance/event.rb', line 8

def options
  @options
end

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'lib/stance/event.rb', line 8

def record
  @record
end

Class Method Details

.after_create(*methods, &block) ⇒ Object



17
18
19
# File 'lib/stance/event.rb', line 17

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

.before_create(*methods, &block) ⇒ Object



13
14
15
# File 'lib/stance/event.rb', line 13

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

.method_added(method_name) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/stance/event.rb', line 21

def method_added(method_name)
  super

  return if self == Stance::Event || !instance_methods(false).include?(method_name)

  self.callback_methods ||= []
  self.callback_methods << method_name
end

Instance Method Details

#createObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stance/event.rb', line 51

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.debug "Event: #{full_name}"

        self.class.callback_methods&.each { |method| send method }
      end

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

  self
end

#full_nameObject



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

def full_name
  "#{subject.class.name.downcase}.#{name}"
end

#subjectObject



74
75
76
# File 'lib/stance/event.rb', line 74

def subject
  try(:record)&.subject || @subject
end