Class: Codeclimate::Collectors::Messages::Message
- Inherits:
-
Object
- Object
- Codeclimate::Collectors::Messages::Message
- Includes:
- ActiveModel::Model, ActiveModel::Validations::HelperMethods, Validations
- Defined in:
- lib/codeclimate/collectors/messages/message.rb
Direct Known Subclasses
Class Method Summary collapse
-
.attribute(name, type, optional: false) ⇒ Object
Declare an attribute on a message type.
- .attribute_metadata ⇒ Object
-
.json_type ⇒ Object
Calculate the type string to use in the JSON representation to identify this message type.
-
.ordering_keys ⇒ Object
Messages can have ordering dependencies, e.g.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#as_json(*_opts) ⇒ Object
A
Hashfor serializing the message as JSON. -
#attributes ⇒ Object
Return all the attributes of a message as a
Hash.
Class Method Details
.attribute(name, type, optional: false) ⇒ Object
Declare an attribute on a message type. This will declare an attr_accessor, a validation that the attribute is of the specified type, and include it in #attributes. If optional is false a validation for presence is also added.
17 18 19 20 21 22 23 24 25 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 17 def self.attribute(name, type, optional: false) name = name.to_sym [name] = { type: type, optional: optional } attr_accessor name validates name, presence: !optional, type: { type: type } end |
.attribute_metadata ⇒ Object
9 10 11 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 9 def self. @attribute_metadata ||= {} end |
.json_type ⇒ Object
Calculate the type string to use in the JSON representation to identify this message type.
29 30 31 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 29 def self.json_type to_s.gsub(/^.+::(\w+)$/, "\\1").underscore end |
.ordering_keys ⇒ Object
Messages can have ordering dependencies, e.g. a collector could emit an Incident message, followed by several IncidentEvent messages. IncidentEvent messages identify their associated incident via their incident_external_id attribute.
Our backend distributes messages amongst parallel workers for ingestion. To ensure messages with an ordering dependency are processed in order by the same worker, we can return an appropriate ordering_keys array for the classes: e.g. Incident returns [:external_id] and IncidentEvent returns [incident_external_id].
Collectors are also responsible for emitting messages in the appropriate order.
46 47 48 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 46 def self.ordering_keys [] end |
Instance Method Details
#==(other) ⇒ Object
50 51 52 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 50 def ==(other) other.class == self.class && other.attributes == attributes end |
#as_json(*_opts) ⇒ Object
A Hash for serializing the message as JSON. Includes a type key and an attributes key.
67 68 69 70 71 72 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 67 def as_json(*_opts) { type: self.class.json_type, attributes: json_attributes, } end |
#attributes ⇒ Object
Return all the attributes of a message as a Hash
57 58 59 60 61 62 63 |
# File 'lib/codeclimate/collectors/messages/message.rb', line 57 def attributes Hash[ self.class..keys.each.map do |name| [name, public_send(name)] end ] end |