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

'.'.freeze
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'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHanami::Mailer::Configuration

Initialize a configuration instance

Since:

  • 0.1.0



41
42
43
44
# File 'lib/hanami/mailer/configuration.rb', line 41

def initialize
  @namespace = Object
  reset!
end

Instance Attribute Details

#default_charset(value = nil) ⇒ Object

Since:

  • 0.1.0



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

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



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

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



30
31
32
# File 'lib/hanami/mailer/configuration.rb', line 30

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



34
35
36
# File 'lib/hanami/mailer/configuration.rb', line 34

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



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

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



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

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



158
159
160
# File 'lib/hanami/mailer/configuration.rb', line 158

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



204
205
206
207
208
# File 'lib/hanami/mailer/configuration.rb', line 204

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



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

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



179
180
181
182
# File 'lib/hanami/mailer/configuration.rb', line 179

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



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

def prepare(&blk)
  if block_given? # rubocop:disable Style/GuardClause
    @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



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

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

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