1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
class Premailer
# Manages the adapter classes. Currently supports:
#
# * nokogiri
# * nokogiri_fast
# * nokogumbo
module Adapter
autoload :Nokogiri, 'premailer/adapter/nokogiri'
autoload :NokogiriFast, 'premailer/adapter/nokogiri_fast'
autoload :Nokogumbo, 'premailer/adapter/nokogumbo'
# adapter to required file mapping.
REQUIREMENT_MAP = [
["nokogiri", :nokogiri],
["nokogiri", :nokogiri_fast],
["nokogumbo", :nokogumbo],
]
# Returns the adapter to use.
def self.use
return @use if @use
self.use = self.default
@use
end
# 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.
# @raise [RuntimeError] unless suitable adapter found.
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 RuntimeError.new("No suitable adapter for Premailer was found, please install nokogiri or nokogumbo")
end
# Sets the adapter to use.
# @raise [ArgumentError] unless the adapter exists.
def self.use=(new_adapter)
@use = find(new_adapter)
end
# Returns an adapter.
# @raise [ArgumentError] unless the adapter exists.
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
end
end
|