Skip to content

Commit 312d8ee

Browse files
committed
Extract common logic to a shared method
1 parent 8750853 commit 312d8ee

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

lib/propshaft/asset.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
class Propshaft::Asset
55
attr_reader :path, :logical_path, :load_path
66

7+
class << self
8+
def extract_path_and_digest(digested_path)
9+
digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1]
10+
path = digest ? digested_path.sub("-#{digest}", "") : digested_path
11+
12+
[path, digest]
13+
end
14+
end
15+
716
def initialize(path, logical_path:, load_path:)
817
@path, @logical_path, @load_path = path, Pathname.new(logical_path), load_path
918
end

lib/propshaft/output_path.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def files
2626
Hash.new.tap do |files|
2727
all_files_from_tree(path).each do |file|
2828
digested_path = file.relative_path_from(path)
29-
logical_path, digest = extract_path_and_digest(digested_path)
29+
logical_path, digest = Propshaft::Asset.extract_path_and_digest(digested_path.to_s)
3030

3131
files[digested_path.to_s] = {
3232
logical_path: logical_path.to_s,
@@ -42,7 +42,7 @@ def fresh_version_within_limit(mtime, count, expires_at:, limit:)
4242
modified_at = [ 0, Time.now - mtime ].max
4343
modified_at < expires_at || limit < count
4444
end
45-
45+
4646
def remove(path)
4747
FileUtils.rm(@path.join(path))
4848
Propshaft.logger.info "Removed #{path}"
@@ -51,11 +51,4 @@ def remove(path)
5151
def all_files_from_tree(path)
5252
path.children.flat_map { |child| child.directory? ? all_files_from_tree(child) : child }
5353
end
54-
55-
def extract_path_and_digest(digested_path)
56-
digest = digested_path.to_s[/-([0-9a-f]{7,128})\.(?!digested)/, 1]
57-
path = digest ? digested_path.sub("-#{digest}", "") : digested_path
58-
59-
[path, digest]
60-
end
6154
end

lib/propshaft/server.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ def inspect
3535
private
3636
def extract_path_and_digest(env)
3737
full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
38-
digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1]
39-
path = digest ? full_path.sub("-#{digest}", "") : full_path
4038

41-
[ path, digest ]
39+
Propshaft::Asset.extract_path_and_digest(full_path)
4240
end
4341

4442
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3")

test/propshaft/output_path_test.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase
5454
old = output_asset("by_count.txt.map", "old", created_at: Time.now - 300)
5555
current = output_asset("by_count.txt.map", "current", created_at: Time.now - 180)
5656

57+
assert File.exist?(current)
58+
assert File.exist?(old)
59+
5760
@output_path.clean(1, 0)
5861

5962
assert File.exist?(current)
60-
assert_not File.exist?(old)
63+
assert_not File.exist?(old), "#{old} should not exist"
6164
ensure
6265
FileUtils.rm(old) if File.exist?(old)
6366
FileUtils.rm(current) if File.exist?(current)

0 commit comments

Comments
 (0)