Class: InlineSvg::CachedAssetFile
- Inherits:
-
Object
- Object
- InlineSvg::CachedAssetFile
- Defined in:
- lib/inline_svg/cached_asset_file.rb
Instance Attribute Summary collapse
-
#assets ⇒ Object
readonly
Returns the value of attribute assets.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
-
#initialize(paths: [], filters: []) ⇒ CachedAssetFile
constructor
For each of the given paths, recursively reads each asset and stores its contents alongside the full path to the asset.
-
#named(asset_name) ⇒ Object
Public: Finds the named asset and returns the contents as a string.
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
#assets ⇒ Object (readonly)
Returns the value of attribute assets.
5 6 7 |
# File 'lib/inline_svg/cached_asset_file.rb', line 5 def assets @assets end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
5 6 7 |
# File 'lib/inline_svg/cached_asset_file.rb', line 5 def filters @filters end |
#paths ⇒ Object (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 |