Class: Sass::Switcheroo::Importer

Inherits:
Importers::Filesystem
  • Object
show all
Defined in:
lib/sass/switcheroo/importer.rb

Instance Method Summary collapse

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, 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

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, 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 = {}) ⇒ Object



30
31
32
# File 'lib/sass/switcheroo/importer.rb', line 30

def after(glob, import, options = {})
  co_import(:after, glob, import, options)
end

#before(glob, import, options = {}) ⇒ Object



26
27
28
# 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 = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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) ⇒ 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, 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) ⇒ Object



10
11
12
13
14
# 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) ⇒ 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, 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