class Propshaft::LoadPath

Attributes

compilers[R]
paths[R]
version[R]

Public Class Methods

new(paths = [], compilers:, version: nil) click to toggle source
# File lib/propshaft/load_path.rb, line 6
def initialize(paths = [], compilers:, version: nil)
  @paths, @compilers, @version = dedup(paths), compilers, version
end

Public Instance Methods

asset_paths_by_glob(glob) click to toggle source
# File lib/propshaft/load_path.rb, line 27
def asset_paths_by_glob(glob)
  (@cached_asset_paths_by_glob ||= Hash.new)[glob] ||=
    extract_logical_paths_from(assets.select { |a| a.path.fnmatch?(glob) })
end
asset_paths_by_type(content_type) click to toggle source
# File lib/propshaft/load_path.rb, line 22
def asset_paths_by_type(content_type)
  (@cached_asset_paths_by_type ||= Hash.new)[content_type] ||=
    extract_logical_paths_from(assets.select { |a| a.content_type == Mime::EXTENSION_LOOKUP[content_type] })
end
assets() click to toggle source
# File lib/propshaft/load_path.rb, line 18
def assets
  assets_by_path.values
end
cache_sweeper() click to toggle source

Returns a file watcher object configured to clear the cache of the load_path when the directories passed during its initialization have changes. This is used in development and test to ensure the map caches are reset when javascript files are changed.

# File lib/propshaft/load_path.rb, line 43
def cache_sweeper
  @cache_sweeper ||= begin
    exts_to_watch  = Mime::EXTENSION_LOOKUP.map(&:first)
    files_to_watch = Array(paths).collect { |dir| [ dir.to_s, exts_to_watch ] }.to_h
    mutex = Mutex.new

    Rails.application.config.file_watcher.new([], files_to_watch) do
      mutex.synchronize do
        clear_cache
        seed_cache
      end
    end
  end
end
find(asset_name) click to toggle source
# File lib/propshaft/load_path.rb, line 10
def find(asset_name)
  assets_by_path[asset_name]
end
find_referenced_by(asset) click to toggle source
# File lib/propshaft/load_path.rb, line 14
def find_referenced_by(asset)
  compilers.referenced_by(asset).delete(self)
end
manifest() click to toggle source
# File lib/propshaft/load_path.rb, line 32
def manifest
  Hash.new.tap do |manifest|
    assets.each do |asset|
      manifest[asset.logical_path.to_s] = asset.digested_path.to_s
    end
  end
end

Private Instance Methods

all_files_from_tree(path) click to toggle source
# File lib/propshaft/load_path.rb, line 70
def all_files_from_tree(path)
  path.children.flat_map { |child| child.directory? ? all_files_from_tree(child) : child }
end
assets_by_path() click to toggle source
# File lib/propshaft/load_path.rb, line 59
def assets_by_path
  @cached_assets_by_path ||= Hash.new.tap do |mapped|
    paths.each do |path|
      without_dotfiles(all_files_from_tree(path)).each do |file|
        logical_path = file.relative_path_from(path)
        mapped[logical_path.to_s] ||= Propshaft::Asset.new(file, logical_path: logical_path, load_path: self)
      end if path.exist?
    end
  end
end
clear_cache() click to toggle source
# File lib/propshaft/load_path.rb, line 82
def clear_cache
  @cached_assets_by_path = nil
  @cached_asset_paths_by_type = nil
  @cached_asset_paths_by_glob = nil
end
dedup(paths) click to toggle source
# File lib/propshaft/load_path.rb, line 92
def dedup(paths)
  paths   = Array(paths).map { |path| Pathname.new(path) }
  deduped = [].tap do |deduped|
    paths.sort.each { |path| deduped << path if deduped.blank? || !path.to_s.start_with?(deduped.last.to_s) }
  end

  paths & deduped
end
extract_logical_paths_from(assets) click to toggle source
# File lib/propshaft/load_path.rb, line 74
def extract_logical_paths_from(assets)
  assets.collect { |asset| asset.logical_path.to_s }.sort
end
seed_cache() click to toggle source
# File lib/propshaft/load_path.rb, line 88
def seed_cache
  assets_by_path
end
without_dotfiles(files) click to toggle source
# File lib/propshaft/load_path.rb, line 78
def without_dotfiles(files)
  files.reject { |file| file.basename.to_s.starts_with?(".") }
end