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

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.



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

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)|
    require library
    return adapter
  rescue LoadError
    next
  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.



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(&:capitalize).join.to_s)
rescue NameError
  raise ArgumentError, "Invalid adapter: #{adapter}"
end

.useObject

Returns the adapter to use.



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

def self.use
  return @use if @use
  self.use = 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