Class: ActiveEventStore::Event

Inherits:
RubyEventStore::Event
  • Object
show all
Defined in:
lib/active_event_store/event.rb

Overview

RES event wrapper

Constant Summary collapse

RESERVED_ATTRIBUTES =
%i[event_id type metadata].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata: {}, event_id: nil, **params) ⇒ Event

Returns a new instance of Event.



65
66
67
68
69
# File 'lib/active_event_store/event.rb', line 65

def initialize(metadata: {}, event_id: nil, **params)
  validate_attributes!(params)
  extract_sync_attributes!(params)
  super(**{event_id: event_id, metadata: , data: params}.compact)
end

Class Attribute Details

.identifierObject



11
12
13
14
15
# File 'lib/active_event_store/event.rb', line 11

def identifier
  return @identifier if instance_variable_defined?(:@identifier)

  @identifier = name.underscore.tr("/", ".")
end

Class Method Details

.attributes(*fields) ⇒ Object

define store readers



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_event_store/event.rb', line 18

def attributes(*fields)
  fields.each do |field|
    raise ArgumentError, "#{field} is reserved" if RESERVED_ATTRIBUTES.include?(field)

    defined_attributes << field

    class_eval <<~CODE, __FILE__, __LINE__ + 1
      def #{field}
        data[:#{field}]
      end
    CODE
  end
end

.defined_attributesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/active_event_store/event.rb', line 42

def defined_attributes
  return @defined_attributes if instance_variable_defined?(:@defined_attributes)

  @defined_attributes =
    if superclass.respond_to?(:defined_attributes)
      superclass.defined_attributes.dup
    else
      []
    end
end

.defined_sync_attributesObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/active_event_store/event.rb', line 53

def defined_sync_attributes
  return @defined_sync_attributes if instance_variable_defined?(:@defined_sync_attributes)

  @defined_sync_attributes =
    if superclass.respond_to?(:defined_sync_attributes)
      superclass.defined_sync_attributes.dup
    else
      []
    end
end

.sync_attributes(*fields) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/active_event_store/event.rb', line 32

def sync_attributes(*fields)
  fields.each do |field|
    raise ArgumentError, "#{field} is reserved" if RESERVED_ATTRIBUTES.include?(field)

    defined_sync_attributes << field

    attr_reader field
  end
end

Instance Method Details

#inspectObject



77
78
79
# File 'lib/active_event_store/event.rb', line 77

def inspect
  "#{self.class.name}<#{event_type}##{message_id}>, data: #{data}, metadata: #{}"
end

#to_hObject



82
83
84
85
86
87
88
89
# File 'lib/active_event_store/event.rb', line 82

def to_h
  {
    event_id: event_id,
    metadata: .to_h,
    data: data,
    type: event_type
  }
end

#typeObject Also known as: event_type



71
72
73
# File 'lib/active_event_store/event.rb', line 71

def type
  self.class.identifier
end