Module: Premailer::Adapter

Defined in:
lib/premailer/adapter.rb,
lib/premailer/adapter/hpricot.rb,
lib/premailer/adapter/nokogiri.rb

Overview

Manages the adapter classes. Currently supports:

  • nokogiri
  • hpricot

Defined Under Namespace

Modules: Hpricot, Nokogiri

Constant Summary collapse

REQUIREMENT_MAP =

adapter to required file mapping.

[
  ["hpricot",  :hpricot],
  ["nokogiri", :nokogiri],
]

Class Method Summary collapse

Class Method Details

.defaultObject

The default adapter based on what you currently have loaded and installed. First checks to see if any adapters are already loaded, then checks to see which are installed if none are loaded.

Raises:

  • (RuntimeError)

    unless suitable adapter found.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/premailer/adapter.rb', line 30

def self.default
  return :hpricot  if defined?(::Hpricot)
  return :nokogiri if defined?(::Nokogiri)

  REQUIREMENT_MAP.each do |(library, adapter)|
    begin
      require library
      return adapter
    rescue LoadError
      next
    end
  end

  raise RuntimeError.new("No suitable adapter for Premailer was found, please install hpricot or nokogiri")
end

.find(adapter) ⇒ Object

Returns an adapter.

Raises:

  • (ArgumentError)

    unless the adapter exists.



54
55
56
57
58
59
60
# File 'lib/premailer/adapter.rb', line 54

def self.find(adapter)
  return adapter if adapter.is_a?(Module)

  Premailer::Adapter.const_get("#{adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
rescue NameError
  raise ArgumentError, "Invalid adapter: #{adapter}"
end

.useObject

Returns the adapter to use.



20
21
22
23
24
# File 'lib/premailer/adapter.rb', line 20

def self.use
  return @use if @use
  self.use = self.default
  @use
end

.use=(new_adapter) ⇒ Object

Sets the adapter to use.

Raises:

  • (ArgumentError)

    unless the adapter exists.



48
49
50
# File 'lib/premailer/adapter.rb', line 48

def self.use=(new_adapter)
  @use = find(new_adapter)
end