class SGMailer::Base

Public Class Methods

default(values = nil) click to toggle source
# File lib/sg_mailer/base.rb, line 4
def default(values = nil)
  @default ||= {}
  @default.merge!(values) if values
  @default
end
template_id(id) click to toggle source
# File lib/sg_mailer/base.rb, line 10
def template_id(id)
  @current_template_id = id
end
template_ids() click to toggle source
# File lib/sg_mailer/base.rb, line 14
def template_ids
  @template_ids ||= {}
end

Private Class Methods

inherited(klass) click to toggle source
# File lib/sg_mailer/base.rb, line 24
def inherited(klass)
  klass.instance_variable_set(:@default, default.dup)
end
instance() click to toggle source
# File lib/sg_mailer/base.rb, line 20
def instance
  @instance ||= new
end
method_added(method_name) click to toggle source
# File lib/sg_mailer/base.rb, line 37
def method_added(method_name)
  if @current_template_id
    template_ids[method_name.to_s] = @current_template_id
  end

  @current_template_id = nil
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/sg_mailer/base.rb, line 28
def method_missing(name, *args, &block)
  if public_instance_methods.include?(name)
    SGMailer.delivery_processor.new \
      instance.public_send(name, *args, &block)
  else
    super
  end
end

Private Instance Methods

deep_merge(hash, other_hash) click to toggle source

Based on the Active Support Hash#deep_merge! core extension. Inlined here, so we don't need to explicitly depend on Active Support.

# File lib/sg_mailer/base.rb, line 67
def deep_merge(hash, other_hash)
  merged = hash.dup

  other_hash.each_pair do |current_key, other_value|
    this_value = merged[current_key]

    merged[current_key] =
      if this_value.is_a?(Hash) && other_value.is_a?(Hash)
        deep_merge(this_value, other_value)
      else
        other_value
      end
  end

  merged
end
mail(**options) click to toggle source
# File lib/sg_mailer/base.rb, line 48
def mail(**options)
  # Guess the template id here, so we don't have to account for extra
  # calls.
  guessed_template_id =
    self.class.template_ids[caller_locations.first.base_label]

  normalized = normalize_options({
    template_id: guessed_template_id
  }.merge(options))

  MailBuilder.build(normalized)
end
normalize_options(options) click to toggle source
# File lib/sg_mailer/base.rb, line 61
def normalize_options(options)
  deep_merge(self.class.default, options)
end