class Sass::Switcheroo::Importer
Public Class Methods
new(root, options = {})
click to toggle source
Calls superclass method
# File lib/sass/switcheroo/importer.rb, line 3 def initialize(root, options = {}) super(root) @default_glob_options = options.fetch(:glob_options, File::FNM_EXTGLOB | File::FNM_PATHNAME) @fallback_to_original = options.fetch(:fallback, false) @globs = {} end
Public Instance Methods
_find(dir, name, options)
click to toggle source
Calls superclass method
# File lib/sass/switcheroo/importer.rb, line 58 def _find(dir, name, options) engine = super return unless engine filename = remove_root(engine.options[:filename]) syntax = engine.options[:syntax] befores = [] afters = [] @globs.keys.each do |possible_glob| before_import, before_glob_options, before_if_exists = @globs[possible_glob][:before] after_import, after_glob_options, after_if_exists = @globs[possible_glob][:after] if before_import resolved_import = resolve_match(possible_glob, filename, before_import, before_glob_options, before_if_exists, options) befores << resolved_import end if after_import resolved_import = resolve_match(possible_glob, filename, after_import, after_glob_options, after_if_exists, options) afters << resolved_import end end template = engine.instance_variable_get("@template") if befores.any? imports = befores.inject("") {|s, f| s + %Q{@import "#{f}"#{syntax == :scss ? ";" : "\n"}} } template.replace(imports + template) end if afters.any? imports = afters.inject("") {|s, f| s + %Q{#{syntax == :scss ? ";" : "\n"}@import "#{f}"} } template.replace(template + imports) end engine end
after(glob, import, options = {})
click to toggle source
# File lib/sass/switcheroo/importer.rb, line 30 def after(glob, import, options = {}) co_import(:after, glob, import, options) end
before(glob, import, options = {})
click to toggle source
# File lib/sass/switcheroo/importer.rb, line 26 def before(glob, import, options = {}) co_import(:before, glob, import, options) end
co_import(type, glob, import, options = {})
click to toggle source
# File lib/sass/switcheroo/importer.rb, line 16 def co_import(type, glob, import, options = {}) glob_options = import.delete(:glob_options) if import.is_a?(Hash) glob_options ||= options.delete(:glob_options) glob_options ||= @default_glob_options if_exists = import.delete(:if_exists) if import.is_a?(Hash) if_exists ||= options.delete(:if_exists) @globs[glob] ||= {} @globs[glob][type] = [import, glob_options, if_exists] end
find_real_file(dir, name, options)
click to toggle source
Calls superclass method
# File lib/sass/switcheroo/importer.rb, line 34 def find_real_file(dir, name, options) dir = dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) unless File::ALT_SEPARATOR.nil? name = name.gsub(File::ALT_SEPARATOR, File::SEPARATOR) unless File::ALT_SEPARATOR.nil? absolute_name = remove_root(File.join(dir, remove_root(name))) matched_globs = {} loop do glob = @globs.keys.find do |possible_glob| conversions, glob_options = @globs[possible_glob][:replace] next unless conversions next if matched_globs[possible_glob] File.fnmatch(possible_glob, absolute_name, glob_options) end break unless glob matched_globs[glob] = true conversions, _glob_options = @globs[glob][:replace] absolute_name = conversions.inject(absolute_name) {|n, (from, to)| n.sub(from, to) } end if result = super(dir, absolute_name, options) result elsif @fallback_to_original super(dir, name, options) end end
replace(glob, conversions)
click to toggle source
# File lib/sass/switcheroo/importer.rb, line 10 def replace(glob, conversions) glob_options = conversions.delete(:glob_options) || @default_glob_options @globs[glob] ||= {} @globs[glob][:replace] = [conversions, glob_options] end
resolve_match(possible_glob, filename, import, glob_options, if_exists, options)
click to toggle source
# File lib/sass/switcheroo/importer.rb, line 89 def resolve_match(possible_glob, filename, import, glob_options, if_exists, options) if File.fnmatch(possible_glob, filename, glob_options) case import when Hash import.each do |regex, pattern| if match_data = filename.match(regex) match_data.captures.each_with_index do |capture, i| pattern = pattern.gsub("\\#{i + 1}", capture) end return pattern if if_exists && find_real_file(root, pattern, options) || !if_exists return nil end end when String import else raise ArgumentError, "Unknown argument: #{possible_glob.inspect}" end end end