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.3"

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


279
280
281
# File 'lib/hanami/mailer.rb', line 279

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


65
66
67
68
# File 'lib/hanami/mailer.rb', line 65

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


118
119
120
# File 'lib/hanami/mailer.rb', line 118

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


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

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


126
127
128
129
# File 'lib/hanami/mailer.rb', line 126

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


233
234
235
236
237
238
239
# File 'lib/hanami/mailer.rb', line 233

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


209
210
211
212
213
214
215
# File 'lib/hanami/mailer.rb', line 209

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


225
226
227
# File 'lib/hanami/mailer.rb', line 225

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