class ActiveEventStore::Event

RES event wrapper

Constants

RESERVED_ATTRIBUTES

Attributes

identifier[W]
event_id[W]

Public Class Methods

attributes(*fields) click to toggle source

define store readers

# 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_attributes() click to toggle source
# 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_attributes() click to toggle source
# 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
identifier() click to toggle source
# File lib/active_event_store/event.rb, line 11
def identifier
  return @identifier if instance_variable_defined?(:@identifier)

  @identifier = name.underscore.tr("/", ".")
end
new(metadata: {}, event_id: nil, **params) click to toggle source
Calls superclass method
# 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: metadata, data: params}.compact)
end
sync_attributes(*fields) click to toggle source
# 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

Public Instance Methods

event_type() click to toggle source
# File lib/active_event_store/event.rb, line 74
def event_type
  self.class.identifier
end
inspect() click to toggle source
# File lib/active_event_store/event.rb, line 85
def inspect
  "#{self.class.name}<#{event_type}##{message_id}>, data: #{data}, metadata: #{metadata}"
end
to_h() click to toggle source

Has been removed from RES: github.com/RailsEventStore/rails_event_store/pull/726

# File lib/active_event_store/event.rb, line 90
def to_h
  {
    event_id: event_id,
    metadata: metadata.to_h,
    data: data,
    type: event_type
  }
end
type() click to toggle source
# File lib/active_event_store/event.rb, line 78
def type
  self.class.identifier
end
Also aliased as: event_type

Protected Instance Methods

extract_sync_attributes!(params) click to toggle source
# File lib/active_event_store/event.rb, line 110
def extract_sync_attributes!(params)
  params.keys.each do |key|
    next unless self.class.defined_sync_attributes.include?(key.to_sym)

    instance_variable_set(:"@#{key}", params.delete(key))
  end
end
validate_attributes!(params) click to toggle source
# File lib/active_event_store/event.rb, line 103
def validate_attributes!(params)
  unknown_fields = params.keys.map(&:to_sym) - self.class.defined_attributes - self.class.defined_sync_attributes
  unless unknown_fields.empty?
    raise ArgumentError, "Unknown event attributes: #{unknown_fields.join(", ")}"
  end
end