Module: Premailer::Adapter

Defined in:
lib/premailer/adapter.rb,
lib/premailer/adapter/nokogiri.rb,
lib/premailer/adapter/nokogumbo.rb,
lib/premailer/adapter/nokogiri_fast.rb

Overview

Manages the adapter classes. Currently supports:

  • nokogiri

  • nokogiri_fast

  • nokogumbo

Defined Under Namespace

Modules: Nokogiri, NokogiriFast, Nokogumbo

Constant Summary collapse

REQUIREMENT_MAP =

adapter to required file mapping.

[
  ["nokogiri", :nokogiri],
  ["nokogiri", :nokogiri_fast],
  ["nokogumbo", :nokogumbo]
]

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.



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

def self.default
  return :nokogiri if defined?(::Nokogiri)
  return :nokogiri_fast if defined?(::NokogiriFast)
  return :nokogumbo if defined?(::Nokogumbo)

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

  raise "No suitable adapter for Premailer was found, please install nokogiri or nokogumbo"
end

.find(adapter) ⇒ Object

Returns an adapter.

Raises:

  • (ArgumentError)

    unless the adapter exists.



57
58
59
60
61
62
63
# File 'lib/premailer/adapter.rb', line 57

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.



22
23
24
25
26
# File 'lib/premailer/adapter.rb', line 22

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.



51
52
53
# File 'lib/premailer/adapter.rb', line 51

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