Module: Crush
- Defined in:
- lib/crush.rb,
lib/crush/yui.rb,
lib/crush/jsmin.rb,
lib/crush/packr.rb,
lib/crush/cssmin.rb,
lib/crush/engine.rb,
lib/crush/closure.rb,
lib/crush/version.rb,
lib/crush/uglifier.rb,
lib/crush/rainpress.rb
Defined Under Namespace
Modules: Closure, YUI Classes: CSSMin, Engine, EngineNotFound, JSMin, Packr, Rainpress, Uglifier
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.[](path_or_name) ⇒ Object
Looks for a compression engine.
-
.find_by_name(engine_name) ⇒ Object
Look for a compression engine with the given engine name.
-
.find_by_path(path) ⇒ Object
Look for a compression engine for the given filename or file extension.
-
.mappings ⇒ Object
Hash of registered compression engines.
-
.new(path_or_name, options = {}, &block) ⇒ Object
Create a new compression engine.
-
.normalize(ext) ⇒ Object
Ensures the extension doesn’t include the “.”.
-
.prefer(engine_or_name, *extensions) ⇒ Object
Make the given compression engine preferred.
-
.register(engine, *extensions) ⇒ Object
Registers a compression engine for the given file extension(s).
-
.registered?(ext) ⇒ Boolean
Returns true when an engine exists for the given file extension.
Class Method Details
.[](path_or_name) ⇒ Object
Looks for a compression engine. When given a Symbol, it will look for an engine by name. When given a String, it will look for a compression engine by filename or file extension.
61 62 63 |
# File 'lib/crush.rb', line 61 def self.[](path_or_name) path_or_name.is_a?(Symbol) ? find_by_name(path_or_name) : find_by_path(path_or_name) end |
.find_by_name(engine_name) ⇒ Object
Look for a compression engine with the given engine name. Return nil when no engine is found.
80 81 82 |
# File 'lib/crush.rb', line 80 def self.find_by_name(engine_name) mappings.values.flatten.detect { |engine| engine.engine_name == engine_name.to_s } end |
.find_by_path(path) ⇒ Object
Look for a compression engine for the given filename or file extension. Return nil when no engine is found.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/crush.rb', line 86 def self.find_by_path(path) pattern = File.basename(path.to_s).downcase until pattern.empty? || registered?(pattern) pattern.sub! /^[^.]*\.?/, "" end pattern = normalize(pattern) preferred_engine = @preferred_engines[pattern] return preferred_engine unless preferred_engine.nil? mappings[pattern].detect(&:engine_initialized?) || mappings[pattern].first end |
.mappings ⇒ Object
Hash of registered compression engines.
17 18 19 |
# File 'lib/crush.rb', line 17 def self.mappings @engine_mappings end |
.new(path_or_name, options = {}, &block) ⇒ Object
Create a new compression engine. The first argument is used to determine which engine to use. This can be either an engine name (as a Symbol) or a path to a file (as a String). If a path is given, the engine is initialized with a reference to that path.
69 70 71 72 73 74 75 76 |
# File 'lib/crush.rb', line 69 def self.new(path_or_name, = {}, &block) if engine = self[path_or_name] path = path_or_name.is_a?(Symbol) ? nil : path_or_name engine.new(path, , &block) else raise EngineNotFound.new("No compression engine registered for '#{path}'") end end |
.normalize(ext) ⇒ Object
Ensures the extension doesn’t include the “.”
22 23 24 |
# File 'lib/crush.rb', line 22 def self.normalize(ext) ext.to_s.downcase.sub /^\./, "" end |
.prefer(engine_or_name, *extensions) ⇒ Object
Make the given compression engine preferred. Which means, it will reordered the beginning of its mapping.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/crush.rb', line 42 def self.prefer(engine_or_name, *extensions) engine = find_by_name(engine_or_name) unless engine_or_name.is_a?(Class) if extensions.empty? mappings.each do |ext, engines| @preferred_engines[ext] = engine if engines.include?(engine) end else register(engine, *extensions) extensions.each do |ext| ext = normalize(ext) @preferred_engines[ext] = engine end end end |
.register(engine, *extensions) ⇒ Object
Registers a compression engine for the given file extension(s).
27 28 29 30 31 32 |
# File 'lib/crush.rb', line 27 def self.register(engine, *extensions) extensions.each do |ext| ext = normalize(ext) mappings[ext].unshift(engine).uniq! end end |
.registered?(ext) ⇒ Boolean
Returns true when an engine exists for the given file extension.
35 36 37 38 |
# File 'lib/crush.rb', line 35 def self.registered?(ext) ext = normalize(ext) mappings.key?(ext) && !mappings[ext].empty? end |