Class: Sinatra::MailerPlugin::MailerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/mailer_plugin/mailer_base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail_name = nil) ⇒ MailerBase

Returns a new instance of MailerBase.



30
31
32
33
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 30

def initialize(mail_name=nil)
  @mail_name = mail_name
  @mail_attributes = {}
end

Instance Attribute Details

#mail_attributesObject

Returns the value of attribute mail_attributes.



28
29
30
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 28

def mail_attributes
  @mail_attributes
end

Class Method Details

.deliver(mail_name, *args) ⇒ Object

Delivers the specified message for mail_name to the intended recipients mail_name corresponds to the name of a defined method within the mailer class SampleMailer.deliver(:birthday_message)



57
58
59
60
61
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 57

def self.deliver(mail_name, *args)
  mail_object = self.new(mail_name)
  mail_object.method(mail_name).call(*args)
  MailObject.new(mail_object.mail_attributes, self.smtp_settings).deliver
end

.mail_fieldsObject

Returns the available mail fields when composing a message



24
25
26
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 24

def self.mail_fields
  [:to, :from, :subject, :type, :charset, :via, :attachments]
end

.method_missing(method_sym, *arguments, &block) ⇒ Object

Handles method missing for a mailer class. Delivers a message based on the method being called i.e #deliver_birthday_message(22) invokes #birthday_message(22) to setup mail object



70
71
72
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 70

def self.method_missing(method_sym, *arguments, &block)
  method_sym.to_s =~ /deliver_(.*)/ ? self.deliver($1, *arguments) : super
end

.respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns true if a mail exists with the name being delivered

Returns:

  • (Boolean)


64
65
66
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 64

def self.respond_to?(method_sym, include_private = false)
  method_sym.to_s =~ /deliver_(.*)/ ? self.method_defined?($1) : super
end

Instance Method Details

#body(body_value) ⇒ Object

Assigns the body key to the mail attributes either with the rendered body from a template or the given string value



41
42
43
44
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 41

def body(body_value)
  @mail_attributes[:body] = Tilt.new(template_path).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
  @mail_attributes[:body] = body_value if body_value.is_a?(String)
end

#template_pathObject

Returns the path to the email template searched for using glob pattern



47
48
49
# File 'lib/sinatra/mailer_plugin/mailer_base.rb', line 47

def template_path
  Dir[File.join(self.views_path, self.class.name.underscore.split("/").last, "#{@mail_name}.*")].first
end