Skip to content

Commit

Permalink
Replaced respond_to with respond_to_missing in Mash and ActiveModel.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed May 1, 2014
1 parent 85f3a29 commit 953edec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/hashie/extensions/mash/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module Extensions
module Mash
# Extends Mash to behave in a way compatible with ActiveModel.
module ActiveModel
def respond_to?(name, include_private = false)
return false if name == :permitted?
def respond_to_missing?(method_name, *args)
return false if method_name == :permitted?
super
end

def method_missing(name, *args)
fail ArgumentError if name == :permitted?
def method_missing(method_name, *args)
fail ArgumentError if method_name == :permitted?
super
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hashie/mash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def replace(other_hash)
self
end

def respond_to?(method_name, include_private = false)
def respond_to_missing?(method_name, *args)
return true if key?(method_name)
_, suffix = method_suffix(method_name)
case suffix
Expand Down
21 changes: 15 additions & 6 deletions spec/hashie/mash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,36 +314,45 @@ class SubMash < Hashie::Mash
end

describe '#respond_to?' do
subject do
Hashie::Mash.new(abc: 'def')
end

it 'responds to a normal method' do
expect(Hashie::Mash.new).to be_respond_to(:key?)
expect(subject).to be_respond_to(:key?)
end

it 'responds to a set key' do
expect(Hashie::Mash.new(abc: 'def')).to be_respond_to(:abc)
expect(subject).to be_respond_to(:abc)
expect(subject.method(:abc)).to_not be_nil
end

it 'responds to a set key with a suffix' do
%w(= ? ! _).each do |suffix|
expect(Hashie::Mash.new(abc: 'def')).to be_respond_to(:"abc#{suffix}")
expect(subject).to be_respond_to(:"abc#{suffix}")
expect(subject.method(:"abc#{suffix}")).to_not be_nil
end
end

it 'responds to an unknown key with a suffix' do
%w(= ? ! _).each do |suffix|
expect(Hashie::Mash.new(abc: 'def')).to be_respond_to(:"xyz#{suffix}")
expect(subject).to be_respond_to(:"xyz#{suffix}")
expect(subject.method(:"xyz#{suffix}")).to_not be_nil
end
end

it 'does not respond to an unknown key without a suffix' do
expect(Hashie::Mash.new(abc: 'def')).not_to be_respond_to(:xyz)
expect(subject).not_to be_respond_to(:xyz)
expect { subject.method(:xyz) }.to raise_error(NameError)
end

it 'does not respond to permitted?' do
expect(Hashie::Mash.new).to be_respond_to(:permitted?)
expect(subject).to be_respond_to(:permitted?)
klass = Class.new(Hashie::Mash) do
include Hashie::Extensions::Mash::ActiveModel
end
expect(klass.new).not_to be_respond_to(:permitted?)
expect { klass.new.method(:permitted?) }.to raise_error(NameError)
expect { klass.new.permitted? }.to raise_error(ArgumentError)
end
end
Expand Down

0 comments on commit 953edec

Please sign in to comment.