Skip to content

Commit

Permalink
Add respond_to_missing? to complement method_missing
Browse files Browse the repository at this point in the history
The `#respond_to_missing?` hook is the complement of `#method_missing`,
used by `#respond_to?`, `#method`, etc... to do the right thing. In this
case, since `#method_missing` is implemented in terms of an instance of
`MiniMagick::Tool::Mogrify`, we use an instance of that class to
determine if objects of this class can respond to those missing
messages.
  • Loading branch information
stevenharman committed Nov 7, 2014
1 parent f3de225 commit 72dfdf7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/mini_magick/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ def method_missing(name, *args)
end
end

def respond_to_missing?(method_name, include_private = false)
MiniMagick::Tool::Mogrify.new.respond_to?(method_name, include_private)
end

##
# Writes the temporary file out to either a file location (by passing in a
# String) or by passing in a Stream that you can #write(chunk) to
Expand Down
32 changes: 22 additions & 10 deletions spec/lib/mini_magick/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,31 @@ def create(path = image_path)
end
end

describe "#method_missing" do
it "executes the command correctly" do
expect { subject.resize '20x30!' }
.to change { subject.dimensions }.to [20, 30]
end
describe "missing methods" do
context "for a known method" do
it "is executed by #method_missing" do
expect { subject.resize '20x30!' }
.to change { subject.dimensions }.to [20, 30]
end

it "fails with a correct NoMethodError" do
expect { subject.foo }
.to raise_error(NoMethodError, /MiniMagick::Image/)
it "returns self" do
expect(subject.resize('20x30!')).to eq subject
end

it "can be responed to" do
expect(subject.respond_to?(:resize)).to eq true
end
end

it "returns self" do
expect(subject.resize('20x30!')).to eq subject
context "for an unknown method" do
it "fails with a NoMethodError" do
expect { subject.foo }
.to raise_error(NoMethodError, /MiniMagick::Image/)
end

it "cannot be responded to" do
expect(subject.respond_to?(:foo)).to eq false
end
end
end

Expand Down

0 comments on commit 72dfdf7

Please sign in to comment.