Class: AlphaSimprini::DirectiveProcessor

Inherits:
Sprockets::DirectiveProcessor
  • Object
show all
Defined in:
lib/alpha_simprini/directive_processor.rb

Instance Method Summary collapse

Instance Method Details

#glob_assets(path, globstring) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/alpha_simprini/directive_processor.rb', line 5

def glob_assets(path, globstring)
  root = pathname.dirname.join(path).expand_path
  root.to_s.match(/(javascripts|stylesheets|images)/)
  asset_dir = $1

  asset_dirs = (::Rails::Engine.subclasses << Rails.application).map do |engine|
    engine.paths["app/assets"].map do |asset_path|
      engine.root.join(asset_path).join(asset_dir).join(path)
    end
  end.flatten

  Dir["{#{asset_dirs.join ','}}#{globstring}"]
end

#process_require_directory_directive(path = ".") ⇒ Object

‘require_directory` requires all the files inside a single directory. It’s similar to ‘path/*` since it does not follow nested directories.

//= require_directory "./javascripts"


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alpha_simprini/directive_processor.rb', line 25

def process_require_directory_directive(path = ".")
  if relative?(path)
    
    root = pathname.dirname.join(path).expand_path
    unless root.directory?
      raise ArgumentError, "require_tree argument must be a directory"
    end
    context.depend_on(root)

    glob_assets(path, "/*").sort.each do |filename| #Dir["#{root}/*"].sort.each do |filename|
      if filename == self.file
        next
      elsif context.asset_requirable?(filename)
        context.require_asset(filename)
      end
    end
  else
    # The path must be relative and start with a `./`.
    raise ArgumentError, "require_directory argument must be a relative path"
  end
end

#process_require_tree_directive(path = ".") ⇒ Object

‘require_tree` requires all the nested files in a directory. Its glob equivalent is `path/*/`.

//= require_tree "./public"


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/alpha_simprini/directive_processor.rb', line 52

def process_require_tree_directive(path = ".")
  if relative?(path)
    root = pathname.dirname.join(path).expand_path
    
    unless root.directory?
      raise ArgumentError, "require_tree argument must be a directory"
    end

    context.depend_on(root)
    
    
  glob_assets(path, "/**/*").sort.each do |filename| #Dir["{#{root},#{relative_root}}/**/*"].sort.each do |filename|
      if filename == self.file
        next
      elsif File.directory?(filename)
        context.depend_on(filename)
      elsif context.asset_requirable?(filename)
        context.require_asset(filename)
      end
    end
  else
    # The path must be relative and start with a `./`.
    raise ArgumentError, "require_tree argument must be a relative path"
  end
end