Module: MultiCss

Defined in:
lib/multi_css.rb,
lib/multi_css/version.rb,
lib/multi_css/adapters/vendored.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 =
[
  ["css_press", :css_press],
  ["cssminify", :cssminify],
  ["yuicssmin", :yuicssmin],
  ["yui/compressor", :yui_compressor],
  ["rainpress", :rainpress]
]
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.adapterObject Also known as: engine

Get the current adapter class.



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

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

.current_adapter(options) ⇒ Object



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

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
45
# File 'lib/multi_css.rb', line 27

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

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

  Kernel.warn "[WARNING] MultiCss is using the default adapter."
  :vendored
end

.load_adapter(new_adapter) ⇒ Object



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

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



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

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)



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

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



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

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