class Downstream::Event

Constants

RESERVED_ATTRIBUTES

Attributes

identifier[W]
data[R]
errors[R]
event_id[R]
id[R]

Public Class Methods

attributes(*fields) click to toggle source

define store readers

# File lib/downstream/event.rb, line 33
      def attributes(*fields)
        fields.each do |field|
          raise ArgumentError, "#{field} is reserved" if RESERVED_ATTRIBUTES.include?(field)

          defined_attributes << field

          # TODO: rewrite with define_method
          class_eval <<~CODE, __FILE__, __LINE__ + 1
            def #{field}
              data[:#{field}]
            end
          CODE
        end
      end
defined_attributes() click to toggle source
# File lib/downstream/event.rb, line 48
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
human_attribute_name(attr, options = {}) click to toggle source
# File lib/downstream/event.rb, line 63
def human_attribute_name(attr, options = {})
  attr
end
i18n_scope() click to toggle source
# File lib/downstream/event.rb, line 59
def i18n_scope
  :activemodel
end
identifier() click to toggle source
# File lib/downstream/event.rb, line 26
def identifier
  return @identifier if instance_variable_defined?(:@identifier)

  @identifier = name.underscore.tr("/", ".")
end
lookup_ancestors() click to toggle source
# File lib/downstream/event.rb, line 67
def lookup_ancestors
  [self]
end
new(event_id: nil, **params) click to toggle source
# File lib/downstream/event.rb, line 76
def initialize(event_id: nil, **params)
  @event_id = event_id || SecureRandom.hex(10)
  validate_attributes!(params)

  @errors = ActiveModel::Errors.new(self)
  @data = params
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/downstream/event.rb, line 118
def ==(other)
  super ||
    other.instance_of?(self.class) &&
      !event_id.nil? &&
      other.event_id == event_id
end
Also aliased as: eql?
eql?(other)
Alias for: ==
inspect() click to toggle source
# File lib/downstream/event.rb, line 110
def inspect
  "#{self.class.name}<#{type}##{event_id}>, data: #{data}"
end
read_attribute_for_validation(attr) click to toggle source
# File lib/downstream/event.rb, line 114
def read_attribute_for_validation(attr)
  data.fetch(attr)
end
to_gid()
Alias for: to_global_id
to_global_id() click to toggle source
Calls superclass method
# File lib/downstream/event.rb, line 96
def to_global_id
  new_data = data.each_with_object({}) do |(key, value), memo|
    memo[key] = if value.respond_to?(:to_global_id)
      value.to_global_id
    else
      value
    end
  end

  super(new_data.merge!(app: :downstream))
end
Also aliased as: to_gid
to_h() click to toggle source
# File lib/downstream/event.rb, line 88
def to_h
  {
    type: type,
    event_id: event_id,
    data: data
  }
end
type() click to toggle source
# File lib/downstream/event.rb, line 84
def type
  self.class.identifier
end

Private Instance Methods

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