Skip to content

Commit 6994fe4

Browse files
committed
add proper handling of ImageOptim.respond_to?
1 parent 0fb45ce commit 6994fe4

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

CHANGELOG.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## unreleased
44

5+
* Add proper handling of `ImageOptim.respond_to?` [@toy](https://github.com/toy)
6+
57
## v0.23.0 (2016-07-17)
68

79
* Added `cache_dir` and `cache_worker_digests` options to cache results [#83](https://github.com/toy/image_optim/issues/83) [@gpakosz](https://github.com/gpakosz)

lib/image_optim.rb

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,43 @@ def optimize_images_data(datas, &block)
168168
run_method_for(datas, :optimize_image_data, &block)
169169
end
170170

171-
# Optimization methods with default options
172-
def self.method_missing(method, *args, &block)
173-
if method_defined?(method) && method.to_s =~ /^optimize_image/
174-
new.send(method, *args, &block)
175-
else
176-
super
171+
class << self
172+
# Optimization methods with default options
173+
def method_missing(method, *args, &block)
174+
if optimize_image_method?(method)
175+
new.send(method, *args, &block)
176+
else
177+
super
178+
end
177179
end
178-
end
179180

180-
# Version of image_optim gem spec loaded
181-
def self.version
182-
Gem.loaded_specs['image_optim'].version.to_s
183-
rescue
184-
'DEV'
185-
end
181+
def respond_to_missing?(method, include_private = false)
182+
optimize_image_method?(method) || super
183+
end
186184

187-
# Full version of image_optim
188-
def self.full_version
189-
"image_optim v#{version}"
185+
if RUBY_VERSION < '1.9'
186+
def respond_to?(method, include_private = false)
187+
optimize_image_method?(method) || super
188+
end
189+
end
190+
191+
# Version of image_optim gem spec loaded
192+
def version
193+
Gem.loaded_specs['image_optim'].version.to_s
194+
rescue
195+
'DEV'
196+
end
197+
198+
# Full version of image_optim
199+
def full_version
200+
"image_optim v#{version}"
201+
end
202+
203+
private
204+
205+
def optimize_image_method?(method)
206+
method_defined?(method) && method.to_s =~ /^optimize_image/
207+
end
190208
end
191209

192210
# Are there workers for file at path?

spec/image_optim_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,33 @@ def temp_copy(image)
227227
end
228228
end
229229
end
230+
231+
%w[
232+
optimize_image
233+
optimize_image!
234+
optimize_image_data
235+
optimize_images
236+
optimize_images!
237+
optimize_images_data
238+
].each do |method|
239+
describe ".#{method}" do
240+
it 'is checkable by respond_to?' do
241+
expect(ImageOptim).to respond_to(method)
242+
end
243+
244+
it 'calls same method on a new ImageOptim instance' do
245+
image_optim = instance_double(ImageOptim)
246+
method_arg = double
247+
method_block = proc{ :x }
248+
249+
allow(ImageOptim).to receive(:new).and_return(image_optim)
250+
expect(image_optim).to receive(method) do |arg, &block|
251+
expect(arg).to eq(method_arg)
252+
expect(block).to eq(method_block)
253+
end
254+
255+
ImageOptim.send(method, method_arg, &method_block)
256+
end
257+
end
258+
end
230259
end

0 commit comments

Comments
 (0)