Class: Noticed::Deliverable::DeliverBy

Inherits:
Object
  • Object
show all
Defined in:
app/models/noticed/deliverable/deliver_by.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, bulk: false) ⇒ DeliverBy

Returns a new instance of DeliverBy.



6
7
8
# File 'app/models/noticed/deliverable/deliver_by.rb', line 6

def initialize(name, config, bulk: false)
  @name, @config, @bulk, = name, config, bulk
end

Instance Attribute Details

#bulkObject (readonly)

Returns the value of attribute bulk.



4
5
6
# File 'app/models/noticed/deliverable/deliver_by.rb', line 4

def bulk
  @bulk
end

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'app/models/noticed/deliverable/deliver_by.rb', line 4

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'app/models/noticed/deliverable/deliver_by.rb', line 4

def name
  @name
end

Instance Method Details

#constantObject



10
11
12
13
# File 'app/models/noticed/deliverable/deliver_by.rb', line 10

def constant
  namespace = bulk ? "Noticed::BulkDeliveryMethods" : "Noticed::DeliveryMethods"
  config.fetch(:class, [namespace, name.to_s.camelize].join("::")).constantize
end

#ephemeral_perform_later(notifier, recipient, params, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/models/noticed/deliverable/deliver_by.rb', line 30

def ephemeral_perform_later(notifier, recipient, params, options = {})
  options[:wait] ||= evaluate_option(:wait, recipient) if config.has_key?(:wait)
  options[:wait_until] ||= evaluate_option(:wait_until, recipient) if config.has_key?(:wait_until)
  options[:queue] ||= evaluate_option(:queue, recipient) if config.has_key?(:queue)
  options[:priority] ||= evaluate_option(:priority, recipient) if config.has_key?(:priority)

  constant.set(options).perform_later(name, "#{notifier}::Notification", recipient: recipient, params: params)
end

#evaluate_option(name, context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/noticed/deliverable/deliver_by.rb', line 39

def evaluate_option(name, context)
  option = config[name]

  if option&.respond_to?(:call)
    context.instance_exec(&option)
  elsif option.is_a?(Symbol) && context.respond_to?(option)
    context.send(option)
  else
    option
  end
end

#perform_later(event_or_notification, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/noticed/deliverable/deliver_by.rb', line 21

def perform_later(event_or_notification, options = {})
  options[:wait] ||= evaluate_option(:wait, event_or_notification) if config.has_key?(:wait)
  options[:wait_until] ||= evaluate_option(:wait_until, event_or_notification) if config.has_key?(:wait_until)
  options[:queue] ||= evaluate_option(:queue, event_or_notification) if config.has_key?(:queue)
  options[:priority] ||= evaluate_option(:priority, event_or_notification) if config.has_key?(:priority)

  constant.set(options).perform_later(name, event_or_notification)
end

#validate!Object



15
16
17
18
19
# File 'app/models/noticed/deliverable/deliver_by.rb', line 15

def validate!
  constant.required_option_names.each do |option|
    raise ValidationError, "option `#{option}` must be set for `deliver_by :#{name}`" unless config[option].present?
  end
end