Class: Downstream::Event

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
GlobalID::Identification
Defined in:
lib/downstream/event.rb

Direct Known Subclasses

DataEvent

Constant Summary collapse

RESERVED_ATTRIBUTES =
%i[id event_id type].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Event.



77
78
79
80
81
82
83
# File 'lib/downstream/event.rb', line 77

def initialize(event_id: nil, **params)
  @event_id = event_id || SecureRandom.hex(10)
  validate_attributes!(params)

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

Class Attribute Details

.identifierObject



13
14
15
16
17
# File 'lib/downstream/event.rb', line 13

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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



73
74
75
# File 'lib/downstream/event.rb', line 73

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



73
74
75
# File 'lib/downstream/event.rb', line 73

def errors
  @errors
end

#event_idObject (readonly) Also known as: id

Returns the value of attribute event_id.



73
74
75
# File 'lib/downstream/event.rb', line 73

def event_id
  @event_id
end

Class Method Details

.attributes(*fields) ⇒ Object

define store readers



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/downstream/event.rb', line 20

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

.define(*fields) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/downstream/event.rb', line 46

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

  data_class = ::Data.define(*fields)

  Class.new(DataEvent, &).tap do
    _1.data_class = data_class

    _1.delegate(*fields, to: :data)
  end
end

.defined_attributesObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/downstream/event.rb', line 35

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 = {}) ⇒ Object



64
65
66
# File 'lib/downstream/event.rb', line 64

def human_attribute_name(attr, options = {})
  attr
end

.i18n_scopeObject



60
61
62
# File 'lib/downstream/event.rb', line 60

def i18n_scope
  :activemodel
end

.lookup_ancestorsObject



68
69
70
# File 'lib/downstream/event.rb', line 68

def lookup_ancestors
  [self]
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



119
120
121
122
123
124
# File 'lib/downstream/event.rb', line 119

def ==(other)
  super ||
    other.instance_of?(self.class) &&
      !event_id.nil? &&
      other.event_id == event_id
end

#inspectObject



111
112
113
# File 'lib/downstream/event.rb', line 111

def inspect
  "#{self.class.name}<#{type}##{event_id}>, data: #{data}"
end

#read_attribute_for_validation(attr) ⇒ Object



115
116
117
# File 'lib/downstream/event.rb', line 115

def read_attribute_for_validation(attr)
  data.fetch(attr)
end

#to_global_idObject Also known as: to_gid



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/downstream/event.rb', line 97

def to_global_id
  new_data = data.to_h.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

#to_hObject



89
90
91
92
93
94
95
# File 'lib/downstream/event.rb', line 89

def to_h
  {
    type: type,
    event_id: event_id,
    data: data
  }
end

#typeObject



85
86
87
# File 'lib/downstream/event.rb', line 85

def type
  self.class.identifier
end