Module: Shift

Defined in:
lib/shift.rb,
lib/shift/c/sass.rb,
lib/shift/errors.rb,
lib/shift/mappings.rb,
lib/shift/c/identity.rb,
lib/shift/c/rdiscount.rb,
lib/shift/c/redcarpet.rb,
lib/shift/c/uglify_js.rb,
lib/shift/c/coffee_script.rb,
lib/shift/c/yui_compressor.rb,
lib/shift/c/closure_compiler.rb

Defined Under Namespace

Classes: ClosureCompiler, CoffeeScript, DependencyError, Error, Identity, RDiscount, Redcarpet, Sass, UglifyJS, UnknownFormatError, YUICompressor

Constant Summary collapse

VERSION =
'0.1.0'
MAPPINGS =

Mappings from file names to implementation classes. The classes are listed in order of preference per type.

{
  'echo'    => %w{ Identity },
  'js'      => %w{ UglifyJS ClosureCompiler YUICompressor },
  'coffee'  => %w{ CoffeeScript },
  'sass'    => %w{ Sass },
  'md'      => %w{ RDiscount Redcarpet }
}
Echo =
Identity
RedCarpet =
Redcarpet

Class Method Summary collapse

Class Method Details

.[](file) ⇒ Object

Get the preferred available class mapped to match the given filename or extension.

(see Shift.best_available_mapping_for)

Raises:



48
49
50
51
52
53
54
55
56
57
# File 'lib/shift.rb', line 48

def self.[](file)
  pattern = File.basename(file.to_s.downcase)
  until pattern.empty?
    if MAPPINGS[pattern]
      return best_available_mapping_for(pattern)
    end
    pattern.sub!(/^[^.]*\.?/, '')
  end
  raise UnknownFormatError, "no mapping matches #{file}"
end

.best_available_mapping_for(key) ⇒ Class

Returns The preferred available class associated with the file or extension.

Returns:

  • (Class)

    The preferred available class associated with the file or extension.

Raises:

  • (DependencyError)

    when none of the mapped implementations are available.



21
22
23
24
25
26
27
28
29
# File 'lib/shift/mappings.rb', line 21

def self.best_available_mapping_for(key)
  MAPPINGS[key].each do |kls_name|
    kls = const_get(kls_name)
    return kls if kls.available?
  end
  help = const_get(MAPPINGS[key].first)::INSTRUCTIONS
  raise DependencyError, "no implementation available for " +
    "#{key.inspect}. Possible solution: #{help}"
end

.read(path, opts = {}) ⇒ Object

Read and process a file with the mapped component.

(see Identity#read)

See Also:



37
38
39
# File 'lib/shift.rb', line 37

def self.read(path, opts={})
  self[path].new(opts).read(path)
end