module CatHerder::Assets

Public Class Methods

[](logical_path) click to toggle source
# File lib/cat_herder/assets.rb, line 48
def [](logical_path)
  source_path = resolve_logical_path(logical_path) or raise AssetNotFound, logical_path
  (@assets ||= {})[source_path] ||= (source_path.end_with?(".erb") ? ErbAsset : VerbatimAsset).new(logical_path, source_path)
end
cache() click to toggle source
# File lib/cat_herder/assets.rb, line 20
def cache
  @cache ||= ActiveSupport::Cache.lookup_store(*cache_store)
end
clean() click to toggle source
# File lib/cat_herder/assets.rb, line 76
def clean
  public_files.each do |file|
    logical_path = public_file_logical_path(file)
    file.delete unless resolve_logical_path(logical_path) && file == self[logical_path].public_file
  end
end
glob(*logical_patterns, &block) click to toggle source
# File lib/cat_herder/assets.rb, line 53
def glob(*logical_patterns, &block)
  logical_patterns.map! { |pattern| "#{pattern}{,.erb}" }
  load_paths.flat_map { |load_path| Dir.glob(*logical_patterns, base: load_path) }.
    each { |path| path.sub!(%r"_?([^/]+?)(?:\.erb)?\z", '\1') }.uniq.
    tap { |logical_paths| logical_paths.each(&block) if block }
end
precompile() click to toggle source
# File lib/cat_herder/assets.rb, line 60
def precompile
  public_path.rmtree if public_path.exist?
  glob("**/[^_]*") { |logical_path| self[logical_path].compile }
  @assets&.each_value { |asset| asset.public_file.dirname.rmtree if asset.partial? && asset.public_file.exist? }
  cache.clear
end
precompiled_asset_path(logical_path) click to toggle source
# File lib/cat_herder/assets.rb, line 72
def precompiled_asset_path(logical_path)
  precompiled_asset_paths[logical_path] or raise AssetNotFound, logical_path
end
precompiled_asset_paths() click to toggle source
# File lib/cat_herder/assets.rb, line 67
def precompiled_asset_paths
  @precompiled_asset_paths ||= public_files.index_by { |file| public_file_logical_path(file) }.
    transform_values! { |file| "/#{file.relative_path_from(Rails.public_path)}" }
end
public_file_logical_path(public_file) click to toggle source
# File lib/cat_herder/assets.rb, line 32
def public_file_logical_path(public_file)
  public_file.dirname.relative_path_from(public_path).to_s
end
public_files() click to toggle source
# File lib/cat_herder/assets.rb, line 28
def public_files
  public_path.glob("**/*").select(&:file?)
end
public_path() click to toggle source
# File lib/cat_herder/assets.rb, line 24
def public_path
  @public_path ||= Rails.public_path.join(public_subpath)
end
resolve_logical_path(logical_path) click to toggle source
# File lib/cat_herder/assets.rb, line 36
def resolve_logical_path(logical_path)
  Current.resolved_paths[logical_path] ||= begin
    logical_dirname, logical_basename = File.split(logical_path)
    basename_pattern = /\A_?#{Regexp.escape logical_basename}(?:\.erb)?\z/
    load_paths.find do |load_path|
      dirname = File.expand_path(logical_dirname, load_path)
      basename = Current.dir_children(dirname).find { |name| basename_pattern.match?(name) }
      break File.join(dirname, basename) if basename
    end
  end
end