Class: Sass::Rails::Importer
- Inherits:
-
Object
- Object
- Sass::Rails::Importer
- 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
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #each_globbed_file(glob, base_pathname, options) ⇒ Object
- #find(name, options) ⇒ Object
- #find_relative(name, base, options) ⇒ Object
- #glob_imports(glob, base_pathname, options) ⇒ Object
-
#initialize(context) ⇒ Importer
constructor
A new instance of Importer.
- #key(name, options) ⇒ Object
- #mtime(name, options) ⇒ Object
- #resolve(name, base_pathname = nil) ⇒ Object
- #sass_file?(filename) ⇒ Boolean
- #syntax(filename) ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#context ⇒ Object (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, ) Dir["#{base_pathname}/#{glob}"].sort.each do |filename| next if filename == [: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, ) 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, .merge(:filename => pathname.to_s, :importer => self, :syntax => syntax(pathname))) else Sass::Engine.new(@resolver.process(pathname), .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, ) base_pathname = Pathname.new(base) if name =~ GLOB glob_imports(name, base_pathname, ) elsif pathname = resolve(name, base_pathname.dirname) context.depend_on(pathname) if sass_file?(pathname) Sass::Engine.new(pathname.read, .merge(:filename => pathname.to_s, :importer => self, :syntax => syntax(pathname))) else Sass::Engine.new(@resolver.process(pathname), .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, ) contents = "" each_globbed_file(glob, base_pathname.dirname, ) 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, .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, ) ["Sprockets:" + File.dirname(File.(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, ) if name =~ GLOB && [:filename] mtime = nil each_globbed_file(name, Pathname.new([:filename]).dirname, ) 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
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_s ⇒ Object
116 117 118 |
# File 'lib/sass/rails/importer.rb', line 116 def to_s "Sass::Rails::Importer(#{context.pathname})" end |