Skip to content

Commit

Permalink
Fixes respond_to? when raise_missing_keys is on
Browse files Browse the repository at this point in the history
  • Loading branch information
brianstanwyck committed Oct 29, 2012
1 parent b5796ce commit c4b4e35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/hashr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ def set(path, value, stack = [])
tokens.size == 1 ? self[path] = value : self[tokens.shift, Hashr.new].set(tokens.join('.'), value, stack)
end

def respond_to?(*args)
true
def respond_to?(method)
if self.class.raise_missing_keys
key?(method)
else
true
end
end

def method_missing(name, *args, &block)
Expand Down
19 changes: 19 additions & 0 deletions test/hashr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ def teardown
assert_equal 'foo', hashr.foo
end

test 'respond_to? returns true if raise_missing_keys is off' do
Hashr.raise_missing_keys = false
hashr = Hashr.new
assert hashr.respond_to?(:foo)
end

test 'respond_to? returns false for missing keys if raise_missing_keys is on' do
Hashr.raise_missing_keys = true
hashr = Hashr.new
assert_equal false, hashr.respond_to?(:foo)
end

test 'respond_to? returns true for extant keys if raise_missing_keys is on' do
Hashr.raise_missing_keys = true
hashr = Hashr.new
hashr[:foo] = 'bar'
assert hashr.respond_to?(:foo)
end

test 'defining defaults' do
klass = Class.new(Hashr) do
define :foo => 'foo', :bar => { :baz => 'baz' }
Expand Down

0 comments on commit c4b4e35

Please sign in to comment.