-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module Noticed | ||
module Deliverable | ||
class DeliverBy | ||
attr_reader :name, :config, :bulk | ||
|
||
def initialize(name, config, bulk: false) | ||
@name, @config, @bulk, = name, config, bulk | ||
end | ||
|
||
def constant | ||
namespace = bulk ? "Noticed::BulkDeliveryMethods" : "Noticed::DeliveryMethods" | ||
config.fetch(:class, [namespace, name.to_s.camelize].join("::")).constantize | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
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 | ||
end | ||
end | ||
end |