Module: MultiCss

Defined in:
lib/multi_css.rb,
lib/multi_css/version.rb,
lib/multi_css/adapters/css_press.rb,
lib/multi_css/adapters/cssminify.rb,
lib/multi_css/adapters/rainpress.rb,
lib/multi_css/adapters/yuicssmin.rb,
lib/multi_css/adapters/yui_compressor.rb

Defined Under Namespace

Modules: Adapters Classes: ParseError

Constant Summary collapse

REQUIREMENT_MAP =
[
  ["cssminify", :cssminify],
  ["yuicssmin", :yuicssmin],
  ["yui/compressor", :yui_compressor],
  ["rainpress", :rainpress],
  ["css_press", :css_press]
]
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.adapterObject Also known as: engine

Get the current adapter class.



49
50
51
52
53
# File 'lib/multi_css.rb', line 49

def adapter
  return @adapter if @adapter
  self.use self.default_adapter
  @adapter
end

.current_adapter(options) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/multi_css.rb', line 88

def current_adapter(options)
  if new_adapter = (options || {}).delete(:adapter)
    load_adapter(new_adapter)
  else
    adapter
  end
end

.default_adapterObject Also known as: default_engine

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.



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

def default_adapter
  return :cssminify if defined?(::CSSminify)      
  return :yuicssmin if defined?(::Yuicssmin)
  return :yui_compressor if defined?(::YUI::CssCompressor)
  return :rainpress if defined?(::Rainpress)
  return :css_press if defined?(::CssPress)

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

  :css_press
end

.load_adapter(new_adapter) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/multi_css.rb', line 72

def load_adapter(new_adapter)
  case new_adapter
  when String, Symbol
    require "multi_css/adapters/#{new_adapter}"
    self::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when NilClass, FalseClass
    default_adapter = self.default_adapter
    require "multi_css/adapters/#{default_adapter}"
    self::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
  when Class
    new_adapter
  else
    raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
  end
end

.min(string, options = {}) ⇒ Object

Minify CSS



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/multi_css.rb', line 97

def min(string, options={})
  string = string.read if string.respond_to?(:read)

  adapter = current_adapter(options)
  if defined?(adapter::ParseError)
    begin
      adapter.min(string, options)
    rescue adapter::ParseError => exception
      raise ::MultiCss::ParseError.new(exception.message, exception.backtrace)
    end
  else
    adapter.min(string, options)
  end
end

.min_attr(string, options = {}) ⇒ Object

Minify partial CSS (as in style attribute)



113
114
115
# File 'lib/multi_css.rb', line 113

def min_attr(string, options={})
  min("a{#{string}}", options).gsub(/^a\{|\}$/, '')
end

.use(new_adapter) ⇒ Object Also known as: adapter=, engine=

Set the adapter utilizing a symbol, string, or class. Supported by default are:

  • :css_press

  • :cssminify

  • :yuicssmin

  • :yui_compressor

  • :rainpress



65
66
67
# File 'lib/multi_css.rb', line 65

def use(new_adapter)
  @adapter = load_adapter(new_adapter)
end