Class: Sass::Rails::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/rails/importer.rb

Constant Summary collapse

GLOB =
/\*|\[.+\]/
PARTIAL =
/^_/
HAS_EXTENSION =
/\.css(.s[ac]ss)?$/
SASS_EXTENSIONS =
{
  ".css.sass" => :sass,
  ".css.scss" => :scss,
  ".sass" => :sass,
  ".scss" => :scss
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Importer

Returns a new instance of Importer.



17
18
19
20
# File 'lib/sass/rails/importer.rb', line 17

def initialize(context)
  @context = context
  @resolver = Resolver.new(context)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



15
16
17
# File 'lib/sass/rails/importer.rb', line 15

def context
  @context
end

Instance Method Details

#each_globbed_file(glob, base_pathname, options) ⇒ Object



74
75
76
77
78
79
# File 'lib/sass/rails/importer.rb', line 74

def each_globbed_file(glob, base_pathname, options)
  Dir["#{base_pathname}/#{glob}"].sort.each do |filename|
    next if filename == options[:filename]
    yield filename if File.directory?(filename) || context.asset_requirable?(filename)
  end
end

#find(name, options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sass/rails/importer.rb', line 59

def find(name, options)
  if name =~ GLOB
    nil # globs must be relative
  elsif pathname = resolve(name)
    context.depend_on(pathname)
    if sass_file?(pathname)
      Sass::Engine.new(pathname.read, options.merge(:filename => pathname.to_s, :importer => self, :syntax => syntax(pathname)))
    else
      Sass::Engine.new(@resolver.process(pathname), options.merge(:filename => pathname.to_s, :importer => self, :syntax => :scss))
    end
  else
    nil
  end
end

#find_relative(name, base, options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sass/rails/importer.rb', line 43

def find_relative(name, base, options)
  base_pathname = Pathname.new(base)
  if name =~ GLOB
    glob_imports(name, base_pathname, options)
  elsif pathname = resolve(name, base_pathname.dirname)
    context.depend_on(pathname)
    if sass_file?(pathname)
      Sass::Engine.new(pathname.read, options.merge(:filename => pathname.to_s, :importer => self, :syntax => syntax(pathname)))
    else
      Sass::Engine.new(@resolver.process(pathname), options.merge(:filename => pathname.to_s, :importer => self, :syntax => :scss))
    end
  else
    nil
  end
end

#glob_imports(glob, base_pathname, options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sass/rails/importer.rb', line 81

def glob_imports(glob, base_pathname, options)
  contents = ""
  each_globbed_file(glob, base_pathname.dirname, options) do |filename|
    if File.directory?(filename)
      context.depend_on(filename)
    elsif context.asset_requirable?(filename)
      context.depend_on(filename)
      contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n"
    end
  end
  return nil if contents.empty?
  Sass::Engine.new(contents, options.merge(
    :filename => base_pathname.to_s,
    :importer => self,
    :syntax => :scss
  ))
end

#key(name, options) ⇒ Object



112
113
114
# File 'lib/sass/rails/importer.rb', line 112

def key(name, options)
  ["Sprockets:" + File.dirname(File.expand_path(name)), File.basename(name)]
end

#mtime(name, options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sass/rails/importer.rb', line 99

def mtime(name, options)
  if name =~ GLOB && options[:filename]
    mtime = nil
    each_globbed_file(name, Pathname.new(options[:filename]).dirname, options) do |p|
      mtime ||= File.mtime(p)
      mtime = [mtime, File.mtime(p)].max
    end
    mtime
  elsif pathname = resolve(name)
    pathname.mtime
  end
end

#resolve(name, base_pathname = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/sass/rails/importer.rb', line 33

def resolve(name, base_pathname = nil)
  name = Pathname.new(name)
  if base_pathname && base_pathname.to_s.size > 0
    root = Pathname.new(context.root_path)
    name = base_pathname.relative_path_from(root).join(name)
  end
  partial_name = name.dirname.join("_#{name.basename}")
  @resolver.resolve(name) || @resolver.resolve(partial_name)
end

#sass_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/sass/rails/importer.rb', line 22

def sass_file?(filename)
  filename = filename.to_s
  SASS_EXTENSIONS.keys.any?{|ext| filename[ext]}
end

#syntax(filename) ⇒ Object



27
28
29
30
31
# File 'lib/sass/rails/importer.rb', line 27

def syntax(filename)
  filename = filename.to_s
  SASS_EXTENSIONS.each {|ext, syntax| return syntax if filename[(ext.size+2)..-1][ext]}
  nil
end

#to_sObject



116
117
118
# File 'lib/sass/rails/importer.rb', line 116

def to_s
  "Sass::Rails::Importer(#{context.pathname})"
end