Class: Heron::Factories::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/heron/factories/event.rb

Overview

Factory class to create Events (for the moment only PlateCherrypicked)

Constant Summary collapse

EVENT_CLASSES =
{ BroadcastEvent::PlateCherrypicked::EVENT_TYPE.to_s => BroadcastEvent::PlateCherrypicked }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(params, seed) ⇒ Event

Returns a new instance of Event.


12
13
14
15
# File 'app/heron/factories/event.rb', line 12

def initialize(params, seed)
  @params = params
  @seed = seed
end

Instance Method Details

#broadcast_eventObject


17
18
19
20
21
# File 'app/heron/factories/event.rb', line 17

def broadcast_event
  return unless event_class

  @broadcast_event ||= event_class.new(seed: @seed, properties: @params.dig(:event))
end

#check_broadcast_eventObject


27
28
29
30
31
32
33
# File 'app/heron/factories/event.rb', line 27

def check_broadcast_event
  return if errors.count.positive?
  return unless broadcast_event
  return if broadcast_event.valid?

  broadcast_event.errors.each { |key, value| errors.add(key, value) }
end

#event_classObject


23
24
25
# File 'app/heron/factories/event.rb', line 23

def event_class
  EVENT_CLASSES.dig(@params.dig(:event, :event_type))
end

#saveObject


35
36
37
38
39
40
41
# File 'app/heron/factories/event.rb', line 35

def save
  return false unless valid?
  return false unless broadcast_event

  ActiveRecord::Base.transaction { broadcast_event.save }
  true
end