Class: AetherObservatory::EventBase

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Attributes
Defined in:
lib/aether_observatory/event_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ EventBase

Returns a new instance of EventBase.



61
62
63
64
# File 'lib/aether_observatory/event_base.rb', line 61

def initialize(attributes = {})
  super()
  assign_attributes(attributes) if attributes
end

Class Method Details

.create(**attributes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/aether_observatory/event_base.rb', line 16

def create(**attributes)
  event = new(**attributes)
  event_names_with_prefix.each do |event_name_parts|
    event_name = event_name_parts.filter_map do |part|
      event.instance_exec(&part) unless part.nil?
    end.join(".")
    logger.debug("[#{name}] Create event for topic: [#{event_name}]")
    ActiveSupport::Notifications.instrument(event_name, event)
  end

  nil
end

.event_name(value = nil, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/aether_observatory/event_base.rb', line 36

def event_name(value = nil, &block)
  event_names << -> { value } if value.present?
  event_names << block if block.present?

  nil
end

.event_namesObject



47
48
49
# File 'lib/aether_observatory/event_base.rb', line 47

def event_names
  @event_names ||= []
end

.event_names_with_prefixObject



43
44
45
# File 'lib/aether_observatory/event_base.rb', line 43

def event_names_with_prefix
  event_names.map { |event_name| [event_prefix, event_name] }
end

.event_prefix(value = nil, &block) ⇒ Object



29
30
31
32
33
34
# File 'lib/aether_observatory/event_base.rb', line 29

def event_prefix(value = nil, &block)
  @event_prefix = -> { value } if value.present?
  @event_prefix = block if block.present?

  @event_prefix
end

.inherited(subclass) ⇒ Object



11
12
13
14
# File 'lib/aether_observatory/event_base.rb', line 11

def inherited(subclass)
  super
  subclass.event_prefix(&event_prefix)
end

.logger(value = nil) ⇒ Object



51
52
53
54
55
# File 'lib/aether_observatory/event_base.rb', line 51

def logger(value = nil)
  @logger = value if value.present?

  @logger || AetherObservatory.config.logger
end