Module: MultiJs

Defined in:
lib/multi_js.rb,
lib/multi_js/version.rb,
lib/multi_js/adapters/closure.rb,
lib/multi_js/adapters/uglifier.rb,
lib/multi_js/adapters/yui_compressor.rb

Defined Under Namespace

Modules: Adapters Classes: ParseError

Constant Summary collapse

REQUIREMENT_MAP =
[
  ["closure-compiler", :closure],
  ["yui/compressor", :yui_compressor],
  ["uglifier", :uglifier],
]
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.adapterObject Also known as: engine

Get the current adapter class.



45
46
47
48
49
# File 'lib/multi_js.rb', line 45

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

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

Minify CSS



91
92
93
94
95
96
97
98
99
100
# File 'lib/multi_js.rb', line 91

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

  adapter = current_adapter(options)
  begin
    adapter.compile(string, options)
  rescue adapter::ParseError => exception
    raise ::MultiJs::ParseError.new(exception.message, exception.backtrace)
  end
end

.current_adapter(options) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/multi_js.rb', line 82

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/multi_js.rb', line 25

def default_adapter
  return :closure if defined?(::Closure)
  return :yui_compressor if defined?(::YUI::JavaScriptCompressor)
  return :uglifier if defined?(::Uglifier)

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

  :uglifier
end

.load_adapter(new_adapter) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/multi_js.rb', line 66

def load_adapter(new_adapter)
  case new_adapter
  when String, Symbol
    require "multi_js/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_js/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

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

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

  • :closure

  • :yui_compressor

  • :uglifier



59
60
61
# File 'lib/multi_js.rb', line 59

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