Module: Hanami::Mailer

Includes:
Utils::ClassAttribute
Defined in:
lib/hanami/mailer.rb,
lib/hanami/mailer/dsl.rb,
lib/hanami/mailer/version.rb,
lib/hanami/mailer/template.rb,
lib/hanami/mailer/configuration.rb,
lib/hanami/mailer/rendering/template_name.rb,
lib/hanami/mailer/rendering/templates_finder.rb

Overview

Hanami::Mailer

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods, Dsl, Rendering Classes: Configuration, Error, MissingDeliveryDataError, Template

Constant Summary collapse

CONTENT_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Content types mapping

Since:

  • 0.1.0

{
  html: 'text/html',
  txt: 'text/plain'
}.freeze
VERSION =

Since:

  • 0.1.0

'1.3.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0



270
271
272
# File 'lib/hanami/mailer.rb', line 270

def method_missing(method_name)
  @locals.fetch(method_name) { super }
end

Class Method Details

.configure(&blk) ⇒ Object

Configure the framework. It yields the given block in the context of the configuration

Examples:

require 'hanami/mailer'

Hanami::Mailer.configure do
  root '/path/to/root'
end

Parameters:

  • blk (Proc)

    the configuration block

See Also:

Since:

  • 0.1.0



63
64
65
66
# File 'lib/hanami/mailer.rb', line 63

def self.configure(&blk)
  configuration.instance_eval(&blk)
  self
end

.deliveriesArray

Test deliveries

This is a collection of delivered messages, used when delivery_method is set on :test

Examples:

require 'hanami/mailer'

Hanami::Mailer.configure do
  delivery_method :test
end.load!

# In testing code
Signup::Welcome.deliver
Hanami::Mailer.deliveries.count # => 1

Returns:

  • (Array)

    a collection of delivered messages

See Also:

  • Hanami::Mailer::Configuration#delivery_mode

Since:

  • 0.1.0



116
117
118
# File 'lib/hanami/mailer.rb', line 116

def self.deliveries
  Mail::TestMailer.deliveries
end

.included(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override Ruby’s hook for modules. It includes basic Hanami::Mailer modules to the given Class. It sets a copy of the framework configuration

Parameters:

  • base (Class)

    the target mailer

See Also:

Since:

  • 0.1.0



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hanami/mailer.rb', line 78

def self.included(base)
  conf = configuration
  conf.add_mailer(base)

  base.class_eval do
    extend Dsl
    extend ClassMethods

    include Utils::ClassAttribute
    class_attribute :configuration

    self.configuration = conf.duplicate
  end

  conf.copy!(base)
end

.load!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Load the framework

Since:

  • 0.1.0



124
125
126
127
# File 'lib/hanami/mailer.rb', line 124

def self.load!
  Mail.eager_autoload!
  configuration.load!
end

Instance Method Details

#deliverObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delivers a multipart email, by looking at all the associated templates and render them.

Since:

  • 0.1.0



224
225
226
227
228
229
230
# File 'lib/hanami/mailer.rb', line 224

def deliver
  mail.deliver
rescue ArgumentError => exception
  raise MissingDeliveryDataError if exception.message =~ /SMTP (From|To) address/

  raise
end

#initialize(locals = {}) ⇒ Object

Initialize a mailer

Parameters:

  • locals (Hash) (defaults to: {})

    a set of objects that acts as context for the rendering

  • :format (Hash)

    a customizable set of options

  • :charset (Hash)

    a customizable set of options

Since:

  • 0.1.0



200
201
202
203
204
205
206
# File 'lib/hanami/mailer.rb', line 200

def initialize(locals = {})
  @locals  = locals
  @format  = locals.fetch(:format, nil)
  @charset = locals.fetch(:charset, self.class.configuration.default_charset)
  @mail    = build
  prepare
end

#render(format) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Render a single template with the specified format.

Parameters:

  • format (Symbol)

    format

Returns:

  • (String)

    the output of the rendering process.

Since:

  • 0.1.0



216
217
218
# File 'lib/hanami/mailer.rb', line 216

def render(format)
  self.class.templates(format).render(self, @locals)
end