Class: BroadcastEvent

Inherits:
ApplicationRecord show all
Extended by:
MetadataHelpers::MetadatableClassMethods, RenderHelpers::RenderableClassMethods, SubjectHelpers::SubjectableClassMethods
Includes:
Uuid::Uuidable
Defined in:
app/models/broadcast_event.rb

Overview

Abstract class used to generate events; use subclass to specify how your particular event is generated.

See Also:

Defined Under Namespace

Modules: MetadataHelpers, RenderHelpers, SubjectHelpers Classes: AssetAudit, LabEvent, LabwareReceived, LibraryComplete, LibraryStart, OrderMade, PlateLibraryComplete, QcAssay, SampleManifestCreated, SampleManifestUpdated, SequencingComplete, SequencingStart

Constant Summary collapse

EVENT_JSON_ROOT =
'event'
UNKNOWN_USER_IDENTIFIER =
'UNKNOWN'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RenderHelpers::RenderableClassMethods

render_class

Methods included from MetadataHelpers::MetadatableClassMethods

has_metadata, metadata_finders

Methods included from SubjectHelpers::SubjectableClassMethods

has_subject, has_subjects, seed_class, seed_subject, subject_associations

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Attribute Details

.event_typeObject (readonly)

Returns the value of attribute event_type


73
74
75
# File 'app/models/broadcast_event.rb', line 73

def event_type
  @event_type
end

Class Method Details

.set_event_type(event_type) ⇒ String

Use in subclasses to specify a fixed event type

Parameters:

  • event_type (String)

    The event type to use for this subclass

Returns:

  • (String)

    The event type


67
68
69
# File 'app/models/broadcast_event.rb', line 67

def self.set_event_type(event_type)
  @event_type = event_type
end

Instance Method Details

#event_typeString

Override in subclasses if you want dynamic event types

Returns:

  • (String)

    The value of the event_type key in the generated message


58
59
60
# File 'app/models/broadcast_event.rb', line 58

def event_type
  self.class.event_type
end

#json_rootString

Returns the root of the generated json object. 'event'

Returns:

  • (String)

    the root of the generated json object. 'event'


52
53
54
# File 'app/models/broadcast_event.rb', line 52

def json_root
  EVENT_JSON_ROOT
end

#metadataObject

Returns a hash of all metadata


41
42
43
# File 'app/models/broadcast_event.rb', line 41

def 
  Hash[self.class..map { |mf| mf.for(seed, self) }]
end

#routing_keyString

Routing key generated for the broadcasted event.

Returns:

  • (String)

    Rouring key. eg. production.event.library_created.123


47
48
49
# File 'app/models/broadcast_event.rb', line 47

def routing_key
  "#{Rails.env}.event.#{event_type}.#{id}"
end

#subjectsObject

Returns an array of all subjects


34
35
36
37
38
# File 'app/models/broadcast_event.rb', line 34

def subjects
  self.class.subject_associations.flat_map do |sa|
    sa.for(seed, self)
  end.select(&:broadcastable?)
end

#user_identifierObject

Prefer email, fall back to login if missing


27
28
29
30
31
# File 'app/models/broadcast_event.rb', line 27

def user_identifier
  return UNKNOWN_USER_IDENTIFIER if user.nil? # User has probably been deleted

  user.email.presence || user.
end