Class: Sass::Switcheroo::Importer
- Inherits:
-
Importers::Filesystem
- Object
- Importers::Filesystem
- Sass::Switcheroo::Importer
- Defined in:
- lib/sass/switcheroo/importer.rb
Instance Method Summary collapse
- #_find(dir, name, options) ⇒ Object
- #after(glob, import, options = {}) ⇒ Object
- #before(glob, import, options = {}) ⇒ Object
- #co_import(type, glob, import, options = {}) ⇒ Object
- #find_real_file(dir, name, options) ⇒ Object
-
#initialize(root, options = {}) ⇒ Importer
constructor
A new instance of Importer.
- #replace(glob, conversions) ⇒ Object
- #resolve_match(possible_glob, filename, import, glob_options, if_exists, options) ⇒ Object
Constructor Details
#initialize(root, options = {}) ⇒ Importer
Returns a new instance of Importer.
3 4 5 6 7 8 |
# File 'lib/sass/switcheroo/importer.rb', line 3 def initialize(root, = {}) super(root) @default_glob_options = .fetch(:glob_options, File::FNM_EXTGLOB | File::FNM_PATHNAME) @fallback_to_original = .fetch(:fallback, false) @globs = {} end |
Instance Method Details
#_find(dir, name, options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sass/switcheroo/importer.rb', line 58 def _find(dir, name, ) engine = super return unless engine filename = remove_root(engine.[:filename]) syntax = engine.[:syntax] befores = [] afters = [] @globs.keys.each do |possible_glob| before_import, , before_if_exists = @globs[possible_glob][:before] after_import, , after_if_exists = @globs[possible_glob][:after] if before_import resolved_import = resolve_match(possible_glob, filename, before_import, , before_if_exists, ) befores << resolved_import end if after_import resolved_import = resolve_match(possible_glob, filename, after_import, , after_if_exists, ) 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 = {}) ⇒ Object
30 31 32 |
# File 'lib/sass/switcheroo/importer.rb', line 30 def after(glob, import, = {}) co_import(:after, glob, import, ) end |
#before(glob, import, options = {}) ⇒ Object
26 27 28 |
# File 'lib/sass/switcheroo/importer.rb', line 26 def before(glob, import, = {}) co_import(:before, glob, import, ) end |
#co_import(type, glob, import, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sass/switcheroo/importer.rb', line 16 def co_import(type, glob, import, = {}) = import.delete(:glob_options) if import.is_a?(Hash) ||= .delete(:glob_options) ||= @default_glob_options if_exists = import.delete(:if_exists) if import.is_a?(Hash) if_exists ||= .delete(:if_exists) @globs[glob] ||= {} @globs[glob][type] = [import, , if_exists] end |
#find_real_file(dir, name, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sass/switcheroo/importer.rb', line 34 def find_real_file(dir, name, ) 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, = @globs[possible_glob][:replace] next unless conversions next if matched_globs[possible_glob] File.fnmatch(possible_glob, absolute_name, ) end break unless glob matched_globs[glob] = true conversions, = @globs[glob][:replace] absolute_name = conversions.inject(absolute_name) {|n, (from, to)| n.sub(from, to) } end if result = super(dir, absolute_name, ) result elsif @fallback_to_original super(dir, name, ) end end |
#replace(glob, conversions) ⇒ Object
10 11 12 13 14 |
# File 'lib/sass/switcheroo/importer.rb', line 10 def replace(glob, conversions) = conversions.delete(:glob_options) || @default_glob_options @globs[glob] ||= {} @globs[glob][:replace] = [conversions, ] end |
#resolve_match(possible_glob, filename, import, glob_options, if_exists, options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sass/switcheroo/importer.rb', line 89 def resolve_match(possible_glob, filename, import, , if_exists, ) if File.fnmatch(possible_glob, filename, ) 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, ) || !if_exists return nil end end when String import else raise ArgumentError, "Unknown argument: #{possible_glob.inspect}" end end end |