lib/sprockets/sass/importer.rb in sprockets-sass-0.1.0 vs lib/sprockets/sass/importer.rb in sprockets-sass-0.2.0
- old
+ new
@@ -2,21 +2,27 @@
require "pathname"
module Sprockets
module Sass
class Importer < ::Sass::Importers::Base
+ GLOB = /\*|\[.+\]/
+
# Reference to the Sprockets context
attr_reader :context
#
def initialize(context)
@context = context
end
# @see Sass::Importers::Base#find_relative
- def find_relative(path, base, options)
- engine_from_path(path, options)
+ def find_relative(path, base_path, options)
+ if path =~ GLOB
+ engine_from_glob(path, base_path, options)
+ else
+ engine_from_path(path, options)
+ end
end
# @see Sass::Importers::Base#find
def find(path, options)
engine_from_path(path, options)
@@ -43,32 +49,57 @@
end
protected
# Create a Sass::Engine from the given path.
- # This is where all the magic happens!
def engine_from_path(path, options)
pathname = resolve(path) or return nil
context.depend_on pathname
::Sass::Engine.new evaluate(pathname), options.merge(
:filename => pathname.to_s,
:syntax => syntax(pathname),
:importer => self
)
end
+ # Create a Sass::Engine that will handle importing
+ # a glob of files.
+ def engine_from_glob(glob, base_path, options)
+ imports = resolve_glob(glob, base_path).inject("") do |imports, path|
+ context.depend_on path
+ relative_path = path.relative_path_from Pathname.new(context.root_path)
+ imports << %(@import "#{relative_path}";\n)
+ end
+ return nil if imports.empty?
+ ::Sass::Engine.new imports, options.merge(
+ :filename => base_path.to_s,
+ :syntax => :scss,
+ :importer => self
+ )
+ end
+
# Finds an asset from the given path. This is where
# we make Sprockets behave like Sass, and import partial
# style paths.
def resolve(path)
path = Pathname.new(path) unless path.is_a?(Pathname)
partial = path.dirname.join("_#{path.basename}")
- resolve_asset(path) || resolve_asset(partial)
+ resolve_path(path) || resolve_path(partial)
end
+ # Finds all of the assets using the given glob.
+ def resolve_glob(glob, base_path)
+ base_path = Pathname.new(base_path)
+ path_with_glob = base_path.dirname.join(glob).to_s
+
+ Pathname.glob(path_with_glob).sort.select do |path|
+ path != context.pathname && context.asset_requirable?(path)
+ end
+ end
+
# Finds the asset using the context from Sprockets.
- def resolve_asset(path)
+ def resolve_path(path)
context.resolve(path, :content_type => :self)
rescue ::Sprockets::FileNotFound, ::Sprockets::ContentTypeMismatch
nil
end