Class: Hanami::Mailer::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/mailer/configuration.rb

Overview

Framework configuration

Since:

  • 0.1.0

Constant Summary collapse

DEFAULT_ROOT =

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.

Default root

Since:

  • 0.1.0

"."
DEFAULT_DELIVERY_METHOD =

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.

Default delivery method

Since:

  • 0.1.0

:smtp
DEFAULT_CHARSET =

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.

Default charset

Since:

  • 0.1.0

"UTF-8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHanami::Mailer::Configuration

Initialize a configuration instance

Since:

  • 0.1.0



43
44
45
46
# File 'lib/hanami/mailer/configuration.rb', line 43

def initialize
  @namespace = Object
  reset!
end

Instance Attribute Details

#default_charset(value = nil) ⇒ Object

Since:

  • 0.1.0



279
280
281
282
283
284
285
# File 'lib/hanami/mailer/configuration.rb', line 279

def default_charset(value = nil)
  if value.nil?
    @default_charset
  else
    @default_charset = value
  end
end

#delivery_method(method = nil, options = {}) ⇒ Array

Specify a global delivery method for the mail gateway.

It supports the following delivery methods:

* Exim (<tt>:exim</tt>)
* Sendmail (<tt>:sendmail</tt>)
* SMTP (<tt>:smtp</tt>, for local installations)
* SMTP Connection (<tt>:smtp_connection</tt>,
  via <tt>Net::SMTP</tt> - for remote installations)
* Test (<tt>:test</tt>, for testing purposes)

The default delivery method is SMTP (:smtp).

Custom delivery methods can be specified by passing the class policy and a set of optional configurations. This class MUST respond to:

* <tt>initialize(options = {})</tt>
* <tt>deliver!(mail)<tt>

Examples:

Setup delivery method with supported symbol

require 'hanami/mailer'

Hanami::Mailer.configure do
  delivery_method :sendmail
end

Setup delivery method with supported symbol and options

require 'hanami/mailer'

Hanami::Mailer.configure do
  delivery_method :smtp, address: "localhost", port: 1025
end

Setup custom delivery method with options

require 'hanami/mailer'

class MandrillDeliveryMethod
  def initialize(options)
    @options = options
  end

  def deliver!(mail)
    # ...
  end
end

Hanami::Mailer.configure do
  delivery_method MandrillDeliveryMethod,
    username: ENV['MANDRILL_USERNAME'],
    password: ENV['MANDRILL_API_KEY']
end

Parameters:

  • method (Symbol, #initialize, deliver!) (defaults to: nil)

    delivery method

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

    optional settings

Returns:

  • (Array)

    an array containing the delivery method and the optional settings as an Hash

Since:

  • 0.1.0



270
271
272
273
274
275
276
# File 'lib/hanami/mailer/configuration.rb', line 270

def delivery_method(method = nil, options = {})
  if method.nil?
    @delivery_method
  else
    @delivery_method = [method, options]
  end
end

#mailersObject (readonly)

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



32
33
34
# File 'lib/hanami/mailer/configuration.rb', line 32

def mailers
  @mailers
end

#modulesObject

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



36
37
38
# File 'lib/hanami/mailer/configuration.rb', line 36

def modules
  @modules
end

#namespace(value) ⇒ Object #namespaceClass, ...

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.

Set the Ruby namespace where to lookup for mailers.

When multiple instances of the framework are used, we want to make sure that if a ‘MyApp` wants a `Mailers::Signup` mailer, we are loading the right one.

If not set, this value defaults to ‘Object`.

This is part of a DSL, for this reason when this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/mailer'

Hanami::Mailer.configuration.namespace # => Object

Setting the value

require 'hanami/mailer'

Hanami::Mailer.configure do
  namespace 'MyApp::Mailers'
end

Overloads:

  • #namespace(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (Class, Module, String)

      a valid Ruby namespace identifier

  • #namespaceClass, ...

    Gets the value

    Returns:

    • (Class, Module, String)

Since:

  • 0.1.0



82
83
84
85
86
87
88
# File 'lib/hanami/mailer/configuration.rb', line 82

def namespace(value = nil)
  if value
    @namespace = value
  else
    @namespace
  end
end

#root(value) ⇒ Object #rootPathname

Set the root path where to search for templates

If not set, this value defaults to the current directory.

When this method is called with an argument, it will set the corresponding instance variable. When called without, it will return the already set value, or the default.

Examples:

Getting the value

require 'hanami/mailer'

Hanami::Mailer.configuration.root # => #<Pathname:.>

Setting the value

require 'hanami/mailer'

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

Hanami::Mailer.configuration.root # => #<Pathname:/path/to/templates>

Overloads:

  • #root(value) ⇒ Object

    Sets the given value

    Parameters:

    • value (String, Pathname, #to_pathname)

      an object that can be coerced to Pathname

  • #rootPathname

    Gets the value

    Returns:

    • (Pathname)

See Also:

Since:

  • 0.1.0



124
125
126
127
128
129
130
# File 'lib/hanami/mailer/configuration.rb', line 124

def root(value = nil)
  if value
    @root = Utils::Kernel.Pathname(value).realpath
  else
    @root
  end
end

Instance Method Details

#add_mailer(mailer) ⇒ 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.

Add a mailer to the registry

Since:

  • 0.1.0



160
161
162
# File 'lib/hanami/mailer/configuration.rb', line 160

def add_mailer(mailer)
  @mailers.add(mailer)
end

#copy!(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.

Copy the configuration for the given mailer

Parameters:

  • base (Class)

    the target mailer

Returns:

  • void

Since:

  • 0.1.0



206
207
208
209
210
# File 'lib/hanami/mailer/configuration.rb', line 206

def copy!(base)
  modules.each do |mod|
    base.class_eval(&mod)
  end
end

#duplicateHanami::Mailer::Configuration

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.

Duplicate by copying the settings in a new instance.

Returns:

Since:

  • 0.1.0



170
171
172
173
174
175
176
177
178
# File 'lib/hanami/mailer/configuration.rb', line 170

def duplicate
  Configuration.new.tap do |c|
    c.namespace  = namespace
    c.root       = root.dup
    c.modules    = modules.dup
    c.delivery_method = delivery_method
    c.default_charset = default_charset
  end
end

#load!Object

Load the configuration

Since:

  • 0.1.0



181
182
183
184
# File 'lib/hanami/mailer/configuration.rb', line 181

def load!
  mailers.each { |m| m.__send__(:load!) }
  freeze
end

#prepare(&blk) ⇒ void

This method returns an undefined value.

Prepare the mailers.

The given block will be yielded when ‘Hanami::Mailer` will be included by a mailer.

This method can be called multiple times.

Parameters:

  • blk (Proc)

    the code block

Raises:

  • (ArgumentError)

    if called without passing a block

See Also:

Since:

  • 0.1.0



148
149
150
151
152
153
154
# File 'lib/hanami/mailer/configuration.rb', line 148

def prepare(&blk)
  if block_given?
    @modules.push(blk)
  else
    raise ArgumentError.new("Please provide a block")
  end
end

#reset!Object Also known as: unload!

Reset the configuration

Since:

  • 0.1.0



187
188
189
190
191
192
193
194
# File 'lib/hanami/mailer/configuration.rb', line 187

def reset!
  root(DEFAULT_ROOT)
  delivery_method(DEFAULT_DELIVERY_METHOD)
  default_charset(DEFAULT_CHARSET)

  @mailers = Set.new
  @modules = []
end