Class: Hanami::Mailer::Configuration

Inherits:
Object
  • Object
show all
Defined in:
gems/gems/hanami-mailer-1.1.0/lib/hanami/mailer/configuration.rb,
gems/gems/hanami-mailer-1.3.1/lib/hanami/mailer/configuration.rb

Overview

Framework configuration

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHanami::Mailer::Configuration

Initialize a configuration instance

Since:

  • 0.1.0

def initialize
  @namespace = Object
  reset!
end

Instance Attribute Details

#default_charset(value = nil) ⇒ Object

Since:

  • 0.1.0

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 (:exim)

  • Sendmail (:sendmail)

  • SMTP (:smtp, for local installations)

  • SMTP Connection (:smtp_connection, via Net::SMTP - for remote installations)

  • Test (:test, 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:

  • initialize(options = {})

  • <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

def delivery_method(method = nil, options = {})
  if method.nil?
    @delivery_method
  else
    @delivery_method = [method, options]
  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

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

Instance Method Details

#load!Object

Load the configuration

Since:

  • 0.1.0

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

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

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

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