class InlineSvg::CachedAssetFile

Attributes

assets[R]
filters[R]
paths[R]

Public Class Methods

new(paths: [], filters: []) click to toggle source

For each of the given paths, recursively reads each asset and stores its contents alongside the full path to the asset.

paths - One or more String representing directories on disk to search

for asset files. Note: paths are searched recursively.

filters - One or more Strings/Regexps to match assets against. Only

assets matching all filters will be cached and available to load.
Note: Specifying no filters will cache every file found in
paths.
# File lib/inline_svg/cached_asset_file.rb, line 17
def initialize(paths: [], filters: [])
  @paths = Array(paths).compact.map { |p| Pathname.new(p) }
  @filters = Array(filters).map { |f| Regexp.new(f) }
  @assets = @paths.reduce({}) { |assets, p| assets.merge(read_assets(assets, p)) }
  @sorted_asset_keys = assets.keys.sort { |a, b| a.size <=> b.size }
end

Public Instance Methods

named(asset_name) click to toggle source

Public: Finds the named asset and returns the contents as a string.

asset_name - A string representing the name of the asset to load

Returns: A String or raises InlineSvg::AssetFile::FileNotFound error

# File lib/inline_svg/cached_asset_file.rb, line 29
def named(asset_name)
  assets[key_for_asset(asset_name)] or
    raise InlineSvg::AssetFile::FileNotFound.new("Asset not found: #{asset_name}")
end

Private Instance Methods

key_for_asset(asset_name) click to toggle source

Internal: Finds the key for a given asset name (using a Regex). In the event of an ambiguous asset_name matching multiple assets, this method ranks the matches by their full file path, choosing the shortest (most exact) match over all others.

Returns a String representing the key for the named asset or nil if there is no match.

# File lib/inline_svg/cached_asset_file.rb, line 42
def key_for_asset(asset_name)
  @sorted_asset_keys.find { |k| k.include?(asset_name) }
end
matches_all_filters?(path) click to toggle source
# File lib/inline_svg/cached_asset_file.rb, line 67
def matches_all_filters?(path)
  filters.all? { |f| f.match(path.to_s) }
end
read_assets(acc, paths) click to toggle source

Internal: Recursively descends through current_paths reading each file it finds and adding them to the accumulator if the fullpath of the file matches all configured filters.

acc - Hash representing the accumulated assets keyed by full path paths - Pathname representing the current node in the directory

structure to consider

Returns a Hash containing the contents of each asset, keyed by fullpath to the asset.

# File lib/inline_svg/cached_asset_file.rb, line 56
def read_assets(acc, paths)
  paths.each_child do |child|
    if child.directory?
      read_assets(acc, child)
    elsif child.readable_real?
      acc[child.to_s] = File.read(child) if matches_all_filters?(child)
    end
  end
  acc
end