Class: Downstream::Event
- Inherits:
-
Object
- Object
- Downstream::Event
- Extended by:
- ActiveModel::Naming
- Includes:
- GlobalID::Identification
- Defined in:
- lib/downstream/event.rb
Constant Summary collapse
- RESERVED_ATTRIBUTES =
i[id event_id type].freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#event_id ⇒ Object
(also: #id)
readonly
Returns the value of attribute event_id.
Class Method Summary collapse
-
.attributes(*fields) ⇒ Object
define store readers.
- .defined_attributes ⇒ Object
- .human_attribute_name(attr, options = {}) ⇒ Object
- .i18n_scope ⇒ Object
- .lookup_ancestors ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(event_id: nil, **params) ⇒ Event
constructor
A new instance of Event.
- #inspect ⇒ Object
- #read_attribute_for_validation(attr) ⇒ Object
- #to_global_id ⇒ Object (also: #to_gid)
- #to_h ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(event_id: nil, **params) ⇒ Event
Returns a new instance of Event.
76 77 78 79 80 81 82 |
# 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 |
Class Attribute Details
.identifier ⇒ Object
26 27 28 29 30 |
# File 'lib/downstream/event.rb', line 26 def identifier return @identifier if instance_variable_defined?(:@identifier) @identifier = name.underscore.tr("/", ".") end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
72 73 74 |
# File 'lib/downstream/event.rb', line 72 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
72 73 74 |
# File 'lib/downstream/event.rb', line 72 def errors @errors end |
#event_id ⇒ Object (readonly) Also known as: id
Returns the value of attribute event_id.
72 73 74 |
# File 'lib/downstream/event.rb', line 72 def event_id @event_id end |
Class Method Details
.attributes(*fields) ⇒ Object
define store readers
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 " def \#{field}\n data[:\#{field}]\n end\n CODE\n end\nend\n", __FILE__, __LINE__ + 1 |
.defined_attributes ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# 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 = {}) ⇒ Object
63 64 65 |
# File 'lib/downstream/event.rb', line 63 def human_attribute_name(attr, = {}) attr end |
.i18n_scope ⇒ Object
59 60 61 |
# File 'lib/downstream/event.rb', line 59 def i18n_scope :activemodel end |
.lookup_ancestors ⇒ Object
67 68 69 |
# File 'lib/downstream/event.rb', line 67 def lookup_ancestors [self] end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
118 119 120 121 122 123 |
# File 'lib/downstream/event.rb', line 118 def ==(other) super || other.instance_of?(self.class) && !event_id.nil? && other.event_id == event_id end |
#inspect ⇒ Object
110 111 112 |
# File 'lib/downstream/event.rb', line 110 def inspect "#{self.class.name}<#{type}##{event_id}>, data: #{data}" end |
#read_attribute_for_validation(attr) ⇒ Object
114 115 116 |
# File 'lib/downstream/event.rb', line 114 def read_attribute_for_validation(attr) data.fetch(attr) end |
#to_global_id ⇒ Object Also known as: to_gid
96 97 98 99 100 101 102 103 104 105 106 |
# 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 |
#to_h ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/downstream/event.rb', line 88 def to_h { type: type, event_id: event_id, data: data } end |
#type ⇒ Object
84 85 86 |
# File 'lib/downstream/event.rb', line 84 def type self.class.identifier end |