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



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

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

#evaluate_option(name, context) ⇒ Object



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

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?(notification) ⇒ Boolean

Returns:

  • (Boolean)


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

def perform?(notification)
  return true unless config.key?(:before_enqueue)

  perform = false
  catch(:abort) {
    evaluate_option(:before_enqueue, notification)
    perform = true
  }
  perform
end

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



21
22
23
# File 'app/models/noticed/deliverable/deliver_by.rb', line 21

def perform_later(event_or_notification, options = {})
  constant.set(computed_options(options, event_or_notification)).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