Class: SinatraMore::MailerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra_more/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.



29
30
31
32
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 29

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

Instance Attribute Details

#mail_attributesObject

Returns the value of attribute mail_attributes.



27
28
29
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 27

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)



56
57
58
59
60
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 56

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



23
24
25
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 23

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



69
70
71
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 69

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)


63
64
65
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 63

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



40
41
42
43
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 40

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



46
47
48
# File 'lib/sinatra_more/mailer_plugin/mailer_base.rb', line 46

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