Class: Rack::Less::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/less/source.rb

Overview

The engine for compiling LESS CSS Given the name of the less source file you want to compile and a path to the source files, will returns corresponding compiled LESS CSS

Constant Summary collapse

PREFERRED_EXTENSIONS =

prefer source files with the .less extension but also accept files with the .css extension

[:less, :css]
YUI_OPTS =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(css_resource, options = {}) ⇒ Source

Returns a new instance of Source.



26
27
28
29
30
31
# File 'lib/rack/less/source.rb', line 26

def initialize(css_resource, options={})
  @css_resource = css_resource.gsub(/^\/+/, '')
  @compress = options[:compress]
  @cache    = options[:cache]
  @folder   = get_required_path(options, :folder)
end

Instance Attribute Details

#css_resourceObject (readonly)

Returns the value of attribute css_resource.



24
25
26
# File 'lib/rack/less/source.rb', line 24

def css_resource
  @css_resource
end

Instance Method Details

#cacheObject



39
40
41
# File 'lib/rack/less/source.rb', line 39

def cache
  @cache
end

#cache?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/less/source.rb', line 36

def cache?
  !@cache.nil?
end

#compiledObject Also known as: to_css, css



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rack/less/source.rb', line 48

def compiled
  @compiled ||= begin
    compiled_css = files.collect do |file_path|
      opts = {
        :paths => [ File.dirname(file_path) ],
        :filename => File.basename(file_path)
      }
      less = File.send(File.respond_to?(:binread) ? :binread : :read, file_path.to_s)
      Less::Parser.new(opts).parse(less).to_css(:compress => compress?)
    end.join("\n")

    compiled_css = case @compress
    when :whitespace, true
      compiled_css.delete("\n")
    when :yui
      if defined?(YUI::CssCompressor)
        YUI::CssCompressor.new(YUI_OPTS).compress(compiled_css)
      else
        raise LoadError, "YUI::CssCompressor is not available. Install it with: gem install yui-compressor"
      end
    else
      compiled_css
    end

    if cache? && (cf = File.join(@cache, "#{@css_resource}.css"))
      FileUtils.mkdir_p(File.dirname(cf))
      File.open(cf, "w") { |file| file.write(compiled_css) }
    end

    compiled_css
  end
end

#compress?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rack/less/source.rb', line 33

def compress?
  !!@compress
end

#filesObject

Use named css sources before using combination sources



44
45
46
# File 'lib/rack/less/source.rb', line 44

def files
  @files ||= (css_sources.empty? ? combination_sources : css_sources)
end