Skip to content

Commit

Permalink
Merge pull request #118 from stevendaniels/patch-1
Browse files Browse the repository at this point in the history
Improve CachedAssetFile
  • Loading branch information
jamesmartin committed Mar 23, 2020
2 parents 6500adc + 9dc8079 commit 07809a7
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions lib/inline_svg/cached_asset_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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: Finds the named asset and returns the contents as a string.
Expand All @@ -39,17 +40,7 @@ def named(asset_name)
# Returns a String representing the key for the named asset or nil if there
# is no match.
def key_for_asset(asset_name)
match = all_keys_matching(asset_name).sort do |a, b|
a.string.size <=> b.string.size
end.first
match && match.string
end

# Internal: Find all potential asset keys matching the given asset name.
#
# Returns an array of MatchData objects for keys matching the asset name.
def all_keys_matching(asset_name)
assets.keys.map { |k| /(#{asset_name})/.match(k.to_s) }.compact
@sorted_asset_keys.find { |k| k.include?(asset_name) }
end

# Internal: Recursively descends through current_paths reading each file it
Expand Down

0 comments on commit 07809a7

Please sign in to comment.