Class: Downstream::Event

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

Constant Summary collapse

RESERVED_ATTRIBUTES =
i[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.



60
61
62
63
64
65
66
# File 'lib/downstream/event.rb', line 60

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



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

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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



58
59
60
# File 'lib/downstream/event.rb', line 58

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



58
59
60
# File 'lib/downstream/event.rb', line 58

def errors
  @errors
end

#event_idObject (readonly)

Returns the value of attribute event_id.



58
59
60
# File 'lib/downstream/event.rb', line 58

def event_id
  @event_id
end

Class Method Details

.attributes(*fields) ⇒ Object

define store readers



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

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 "      def \#{field}\n        data[:\#{field}]\n      end\n    CODE\n  end\nend\n", __FILE__, __LINE__ + 1

.defined_attributesObject



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

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



49
50
51
# File 'lib/downstream/event.rb', line 49

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

.i18n_scopeObject



45
46
47
# File 'lib/downstream/event.rb', line 45

def i18n_scope
  :activemodel
end

.lookup_ancestorsObject



53
54
55
# File 'lib/downstream/event.rb', line 53

def lookup_ancestors
  [self]
end

Instance Method Details

#inspectObject



80
81
82
# File 'lib/downstream/event.rb', line 80

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

#read_attribute_for_validation(attr) ⇒ Object



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

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

#to_hObject



72
73
74
75
76
77
78
# File 'lib/downstream/event.rb', line 72

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

#typeObject



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

def type
  self.class.identifier
end