Module: CatHerder::Assets

Extended by:
ActiveSupport::Autoload
Defined in:
lib/cat_herder/assets.rb,
lib/cat_herder/assets/asset.rb,
lib/cat_herder/assets/erb_asset.rb,
lib/cat_herder/assets/verbatim_asset.rb

Defined Under Namespace

Classes: Asset, ErbAsset, VerbatimAsset

Class Method Summary collapse

Class Method Details

.[](logical_path) ⇒ Object



48
49
50
51
# 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

.cacheObject



20
21
22
# File 'lib/cat_herder/assets.rb', line 20

def cache
  @cache ||= ActiveSupport::Cache.lookup_store(*cache_store)
end

.cleanObject



76
77
78
79
80
81
# 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) ⇒ Object



53
54
55
56
57
58
# 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

.precompileObject



60
61
62
63
64
65
# 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) ⇒ Object



72
73
74
# 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_pathsObject



67
68
69
70
# 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) ⇒ Object



32
33
34
# 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_filesObject



28
29
30
# File 'lib/cat_herder/assets.rb', line 28

def public_files
  public_path.glob("**/*").select(&:file?)
end

.public_pathObject



24
25
26
# 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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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