Class: Downstream::Event
- Inherits:
-
Object
- Object
- Downstream::Event
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#event_id ⇒ Object
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
-
#initialize(event_id: nil, **params) ⇒ Event
constructor
A new instance of Event.
- #inspect ⇒ Object
- #read_attribute_for_validation(attr) ⇒ Object
- #to_h ⇒ Object
- #type ⇒ Object
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
.identifier ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
58 59 60 |
# File 'lib/downstream/event.rb', line 58 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
58 59 60 |
# File 'lib/downstream/event.rb', line 58 def errors @errors end |
#event_id ⇒ Object (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_attributes ⇒ Object
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, = {}) attr end |
.i18n_scope ⇒ Object
45 46 47 |
# File 'lib/downstream/event.rb', line 45 def i18n_scope :activemodel end |
.lookup_ancestors ⇒ Object
53 54 55 |
# File 'lib/downstream/event.rb', line 53 def lookup_ancestors [self] end |
Instance Method Details
#inspect ⇒ Object
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_h ⇒ Object
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 |
#type ⇒ Object
68 69 70 |
# File 'lib/downstream/event.rb', line 68 def type self.class.identifier end |