Class: InlineSvg::CachedAssetFile

Inherits:
Object
  • Object
show all
Defined in:
lib/inline_svg/cached_asset_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths: [], filters: []) ⇒ CachedAssetFile

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.


17
18
19
20
21
22
# 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

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



5
6
7
# File 'lib/inline_svg/cached_asset_file.rb', line 5

def assets
  @assets
end

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/inline_svg/cached_asset_file.rb', line 5

def filters
  @filters
end

#pathsObject (readonly)

Returns the value of attribute paths.



5
6
7
# File 'lib/inline_svg/cached_asset_file.rb', line 5

def paths
  @paths
end

Instance Method Details

#named(asset_name) ⇒ Object

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



29
30
31
32
# 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