Skip to content

Commit

Permalink
added cache_dir and cache_workers options to cache results (11)
Browse files Browse the repository at this point in the history
- cosmetics
  • Loading branch information
gpakosz committed Jun 21, 2016
1 parent 0c7a3db commit b9ad8f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
3 changes: 1 addition & 2 deletions lib/image_optim/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def fetch(original)

digest = digest(original, original.image_format)
cached = @cache_dir / digest
return if cached.file? && cached.zero?
return CachePath.convert(cached) if cached.file?
return cached.size? ? CachePath.convert(cached) : nil if cached.file?

optimized = yield

Expand Down
26 changes: 13 additions & 13 deletions lib/image_optim/runner/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ def wrap_regex(width)
options[:pack] = pack
end

op.separator nil
op.separator ' Caching:'

op.on('--cache-dir DIR', 'Cache optimized images '\
'into the specified directory') do |cache_dir|
options[:cache_dir] = cache_dir
end

op.on('--cache-worker-digests', 'Cache worker digests '\
'(updating workers invalidates cache)') do |cache_worker_digests|
options[:cache_worker_digests] = cache_worker_digests
end

op.separator nil
op.separator ' Disabling workers:'

Expand Down Expand Up @@ -203,19 +216,6 @@ def wrap_regex(width)
end
end

op.separator nil
op.separator ' Caching:'

op.on('--cache-dir DIR', 'Cache optimized images '\
'into the specified directory') do |cache_dir|
options[:cache_dir] = cache_dir
end

op.on('--cache-worker-digests', 'Cache worker digests '\
'(updating workers invalidates cache)') do |cache_worker_digests|
options[:cache_worker_digests] = cache_worker_digests
end

op.separator nil
op.separator ' Common options:'

Expand Down
8 changes: 4 additions & 4 deletions spec/image_optim/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
it 'executes block and write to cache when cached file does not exist' do
cached_s = cached.to_s
allow(FileTest).to receive(:file?).with(cached_s).and_return(false)
allow(FileTest).to receive(:zero?).with(cached_s).and_return(false)
allow(FileTest).to receive(:size?).with(cached_s).and_return(1)
allow(FileUtils).to receive(:mv)
allow(File).to receive(:rename)

Expand All @@ -93,7 +93,7 @@
it 'returns cached file when it exists and options match' do
cached_s = cached.to_s
allow(FileTest).to receive(:file?).with(cached_s).and_return(true)
allow(FileTest).to receive(:zero?).with(cached_s).and_return(false)
allow(FileTest).to receive(:size?).with(cached_s).and_return(1)
expect(FileUtils).not_to receive(:mv)
expect(File).not_to receive(:rename)

Expand Down Expand Up @@ -133,7 +133,7 @@
it 'executes block and write to cache when cached file does not exist' do
cached_s = cached.to_s
allow(FileTest).to receive(:file?).with(cached_s).and_return(false)
allow(FileTest).to receive(:zero?).with(cached_s).and_return(false)
allow(FileTest).to receive(:size?).with(cached_s).and_return(1)
allow(FileUtils).to receive(:mv)
allow(File).to receive(:rename)

Expand All @@ -143,7 +143,7 @@
it 'returns cached file when it exists and both options+workers match' do
cached_s = cached.to_s
allow(FileTest).to receive(:file?).with(cached_s).and_return(true)
allow(FileTest).to receive(:zero?).with(cached_s).and_return(false)
allow(FileTest).to receive(:size?).with(cached_s).and_return(1)
expect(FileUtils).not_to receive(:mv)
expect(File).not_to receive(:rename)

Expand Down

0 comments on commit b9ad8f7

Please sign in to comment.