Skip to content

Commit

Permalink
Implement #eql? for Addressable::Template
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlinc committed Mar 12, 2014
1 parent ea3c89a commit f01bae2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/addressable/template.rb
Expand Up @@ -250,6 +250,20 @@ def inspect
self.class.to_s, self.object_id, self.pattern)
end

##
# Returns <code>true</code> if the Template objects are equal. This method
# does NOT normalize either Template before doing the comparison.
#
# @param [Object] template The Template to compare.
#
# @return [TrueClass, FalseClass]
# <code>true</code> if the Templates are equivalent, <code>false</code>
# otherwise.
def eql?(template)
return false unless template.kind_of?(Template)
return self.pattern == template.pattern
end

##
# Extracts a mapping from the URI using a URI Template pattern.
#
Expand Down
21 changes: 21 additions & 0 deletions spec/addressable/template_spec.rb
Expand Up @@ -32,6 +32,27 @@
end
end

describe "eql?" do
let(:template) { Addressable::Template.new('https://www.example.com/{foo}') }
it 'is equal when the pattern matches' do
other_template = Addressable::Template.new('https://www.example.com/{foo}')
expect(template).to be_eql(other_template)
expect(other_template).to be_eql(template)
end
it 'is not equal when the pattern differs' do
other_template = Addressable::Template.new('https://www.example.com/{bar}')
expect(template).to_not be_eql(other_template)
expect(other_template).to_not be_eql(template)
end
it 'is not equal to non-templates' do
uri = 'https://www.example.com/foo/bar'
addressable_template = Addressable::Template.new uri
addressable_uri = Addressable::URI.parse uri
expect(addressable_template).to_not be_eql(addressable_uri)
expect(addressable_uri).to_not be_eql(addressable_template)
end
end

describe "Type conversion" do
subject{
{:var => true, :hello => 1234, :nothing => nil, :sym => :symbolic}
Expand Down

0 comments on commit f01bae2

Please sign in to comment.