Skip to content

Commit

Permalink
Adding respond_to override to the ObjectifiedHash class so it properl…
Browse files Browse the repository at this point in the history
…y responds to respond_to? calls
  • Loading branch information
koglinjg committed Dec 15, 2014
1 parent b41fb5e commit 3cba3b2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/gitlab/objectified_hash.rb
Expand Up @@ -20,5 +20,9 @@ def to_hash
def method_missing(key)
@data.key?(key.to_s) ? @data[key.to_s] : nil
end

def respond_to?(method_name, include_private = false)
@hash.keys.map(&:to_sym).include?(method_name.to_sym) || super
end
end
end
20 changes: 19 additions & 1 deletion spec/gitlab/objectified_hash_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe Gitlab::ObjectifiedHash do
before do
@hash = {a: 1, b: 2}
@hash = {a: 1, b: 2, 'string' => 'string', symbol: :symbol}
@oh = Gitlab::ObjectifiedHash.new @hash
end

Expand All @@ -20,4 +20,22 @@
expect(@oh.respond_to?(:to_h)).to be_truthy
end
end

describe "#respond_to" do
it "should return true for methods this object responds to through method_missing as sym" do
expect(@oh.respond_to?(:a)).to be_truthy
end

it "should return true for methods this object responds to through method_missing as string" do
expect(@oh.respond_to?('string')).to be_truthy
end

it "should not care if you use a string or symbol to reference a method" do
expect(@oh.respond_to?(:string)).to be_truthy
end

it "should not care if you use a string or symbol to reference a method" do
expect(@oh.respond_to?('symbol')).to be_truthy
end
end
end

0 comments on commit 3cba3b2

Please sign in to comment.