Class: Less::Rails::ImportProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/less/rails/import_processor.rb

Constant Summary collapse

IMPORT_SCANNER =
/@import\s*(?:\([a-z, ]+\))*\s*['"]([^'"]+)['"]\s*;/.freeze
PATHNAME_FINDER =
Proc.new do |scope, path|
  begin
    scope.resolve(path)
  rescue Sprockets::FileNotFound
    nil
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, &block) ⇒ ImportProcessor

Returns a new instance of ImportProcessor.



16
17
18
19
# File 'lib/less/rails/import_processor.rb', line 16

def initialize(filename, &block)
  @filename = filename
  @source   = block.call
end

Class Method Details

.call(input) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/less/rails/import_processor.rb', line 30

def self.call(input)
  filename = input[:filename]
  source   = input[:data]
  scope  = input[:environment].context_class.new(input)

  result = evaluate(filename, source, scope)
  scope..merge(data: result)
end

.depend_on(scope, source, base = File.dirname(scope.logical_path)) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/less/rails/import_processor.rb', line 39

def self.depend_on(scope, source, base=File.dirname(scope.logical_path))
  import_paths = source.scan(IMPORT_SCANNER).flatten.compact.uniq
  import_paths.each do |path|
    pathname = PATHNAME_FINDER.call(scope, path) || PATHNAME_FINDER.call(scope, File.join(base, path))
    scope.depend_on(pathname) if pathname && pathname.to_s.ends_with?('.less')
    depend_on scope, File.read(pathname), File.dirname(path) if pathname
  end
  source
end

.evaluate(filename, source, scope) ⇒ Object



25
26
27
28
# File 'lib/less/rails/import_processor.rb', line 25

def self.evaluate(filename, source, scope)
  depend_on scope, source
  source
end

Instance Method Details

#render(scope, locals) ⇒ Object



21
22
23
# File 'lib/less/rails/import_processor.rb', line 21

def render(scope, locals)
  self.class.evaluate(@filename, @source, scope)
end