Skip to content

Commit

Permalink
Use equalizer instead of manually overwriting #==
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Sep 24, 2013
1 parent 452d243 commit a7ddf71
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 105 deletions.
15 changes: 0 additions & 15 deletions lib/twitter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,5 @@ def memoize(key, &block)
instance_variable_set(ivar, result)
end

private

# @param attr [Symbol]
# @param other [Twitter::Base]
# @return [Boolean]
def attr_equal(attr, other)
self.class == other.class && !other.send(attr.to_sym).nil? && send(attr.to_sym) == other.send(attr.to_sym)
end

# @param other [Twitter::Base]
# @return [Boolean]
def attrs_equal(other)
self.class == other.class && !other.attrs.empty? && attrs == other.attrs
end

end
end
9 changes: 2 additions & 7 deletions lib/twitter/geo.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
require 'equalizer'
require 'twitter/base'

module Twitter
class Geo < Twitter::Base
include Equalizer.new(:coordinates)
attr_reader :coordinates
alias coords coordinates

# @param other [Twitter::Geo]
# @return [Boolean]
def ==(other)
super || attr_equal(:coordinates, other) || attrs_equal(other)
end

end
end
14 changes: 3 additions & 11 deletions lib/twitter/identity.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'equalizer'
require 'twitter/base'

module Twitter
class Identity < Twitter::Base
include Equalizer.new(:id)
attr_reader :id

# Initializes a new object
#
Expand All @@ -13,16 +16,5 @@ def initialize(attrs={})
raise ArgumentError, "argument must have an :id key" unless id
end

# @param other [Twitter::Identity]
# @return [Boolean]
def ==(other)
super || attr_equal(:id, other) || attrs_equal(other)
end

# @return [Integer]
def id
@attrs[:id]
end

end
end
17 changes: 2 additions & 15 deletions lib/twitter/size.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
require 'equalizer'
require 'twitter/base'

module Twitter
class Size < Twitter::Base
include Equalizer.new(:h, :w)
attr_reader :h, :resize, :w
alias height h
alias width w

# @param other [Twitter::Size]
# @return [Boolean]
def ==(other)
super || size_equal(other) || attrs_equal(other)
end

private

# @param other [Twitter::Size]
# @return [Boolean]
def size_equal(other)
self.class == other.class && !other.h.nil? && h == other.h && !other.w.nil? && w == other.w
end

end
end
8 changes: 2 additions & 6 deletions lib/twitter/suggestion.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
require 'equalizer'
require 'twitter/base'

module Twitter
class Suggestion < Twitter::Base
include Equalizer.new(:slug)
attr_reader :name, :size, :slug

# @param other [Twitter::Suggestion]
# @return [Boolean]
def ==(other)
super || attr_equal(:slug, other) || attrs_equal(other)
end

# @return [Array<Twitter::User>]
def users
memoize(:users) do
Expand Down
9 changes: 2 additions & 7 deletions lib/twitter/trend.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
require 'equalizer'
require 'twitter/base'

module Twitter
class Trend < Twitter::Base
include Equalizer.new(:name)
attr_reader :events, :name, :promoted_content, :query
uri_attr_reader :uri

# @param other [Twitter::Trend]
# @return [Boolean]
def ==(other)
super || attr_equal(:name, other) || attrs_equal(other)
end

end
end
4 changes: 2 additions & 2 deletions spec/twitter/geo/point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
end

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
point = Twitter::Geo::Point.new
other = Twitter::Geo::Point.new
expect(point == other).to be_false
expect(point == other).to be_true
end
it "returns true when objects coordinates are the same" do
other = Twitter::Geo::Point.new(:coordinates => [-122.399983, 37.788299])
Expand Down
4 changes: 2 additions & 2 deletions spec/twitter/geo/polygon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
end

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
polygon = Twitter::Geo::Polygon.new
other = Twitter::Geo::Polygon.new
expect(polygon == other).to be_false
expect(polygon == other).to be_true
end
it "returns true when objects coordinates are the same" do
other = Twitter::Geo::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
Expand Down
8 changes: 4 additions & 4 deletions spec/twitter/geo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
end

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
geo = Twitter::Geo.new
other = Twitter::Geo.new
expect(geo == other).to be_false
expect(geo == other).to be_true
end
it "returns true when objects coordinates are the same" do
other = Twitter::Geo.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
Expand All @@ -20,9 +20,9 @@
other = Twitter::Geo.new(:coordinates => [[[37.77752898, -122.40348192], [37.77752898, -122.387436], [37.79448597, -122.387436], [37.79448597, -122.40348192]]])
expect(@geo == other).to be_false
end
it "returns false when classes are different" do
it "returns true when classes are different" do
other = Twitter::Geo::Polygon.new(:coordinates => [[[-122.40348192, 37.77752898], [-122.387436, 37.77752898], [-122.387436, 37.79448597], [-122.40348192, 37.79448597]]])
expect(@geo == other).to be_false
expect(@geo == other).to be_true
end
end

Expand Down
14 changes: 2 additions & 12 deletions spec/twitter/size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
describe Twitter::Size do

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
size = Twitter::Size.new
other = Twitter::Size.new
expect(size == other).to be_false
expect(size == other).to be_true
end
it "returns true when objects height and width are the same" do
size = Twitter::Size.new(:h => 1, :w => 1, :resize => true)
Expand All @@ -23,16 +23,6 @@
other = Twitter::Base.new(:h => 1, :w => 1)
expect(size == other).to be_false
end
it "returns true when objects non-height and width attributes are the same" do
size = Twitter::Size.new(:resize => true)
other = Twitter::Size.new(:resize => true)
expect(size == other).to be_true
end
it "returns false when objects non-height and width attributes are different" do
size = Twitter::Size.new(:resize => true)
other = Twitter::Size.new(:resize => false)
expect(size == other).to be_false
end
end

end
14 changes: 2 additions & 12 deletions spec/twitter/suggestion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
describe Twitter::Suggestion do

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
suggestion = Twitter::Suggestion.new
other = Twitter::Suggestion.new
expect(suggestion == other).to be_false
expect(suggestion == other).to be_true
end
it "returns true when objects slugs are the same" do
suggestion = Twitter::Suggestion.new(:slug => 1, :name => "foo")
Expand All @@ -23,16 +23,6 @@
other = Twitter::Base.new(:slug => 1)
expect(suggestion == other).to be_false
end
it "returns true when objects non-slug attributes are the same" do
suggestion = Twitter::Suggestion.new(:name => "foo")
other = Twitter::Suggestion.new(:name => "foo")
expect(suggestion == other).to be_true
end
it "returns false when objects non-slug attributes are different" do
suggestion = Twitter::Suggestion.new(:name => "foo")
other = Twitter::Suggestion.new(:name => "bar")
expect(suggestion == other).to be_false
end
end

describe "#users" do
Expand Down
14 changes: 2 additions & 12 deletions spec/twitter/trend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
describe Twitter::Trend do

describe "#==" do
it "returns false for empty objects" do
it "returns true for empty objects" do
trend = Twitter::Trend.new
other = Twitter::Trend.new
expect(trend == other).to be_false
expect(trend == other).to be_true
end
it "returns true when objects names are the same" do
trend = Twitter::Trend.new(:name => "#sevenwordsaftersex", :query => "foo")
Expand All @@ -23,16 +23,6 @@
other = Twitter::Base.new(:name => "#sevenwordsaftersex")
expect(trend == other).to be_false
end
it "returns true when objects non-name attributes are the same" do
trend = Twitter::Trend.new(:query => "foo")
other = Twitter::Trend.new(:query => "foo")
expect(trend == other).to be_true
end
it "returns false when objects non-name attributes are different" do
trend = Twitter::Trend.new(:query => "foo")
other = Twitter::Trend.new(:query => "bar")
expect(trend == other).to be_false
end
end

describe "#uri" do
Expand Down
1 change: 1 addition & 0 deletions twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require 'twitter/version'

Gem::Specification.new do |spec|
spec.add_dependency 'buftok', '~> 0.1.0'
spec.add_dependency 'equalizer', '~> 0.0.7'
spec.add_dependency 'faraday', ['>= 0.8', '< 0.10']
spec.add_dependency 'http', '~> 0.5.0'
spec.add_dependency 'http_parser.rb', '~> 0.5.0'
Expand Down

0 comments on commit a7ddf71

Please sign in to comment.