From a7ddf718b119e9c5fc8b87e6784e8c3304707a72 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Tue, 24 Sep 2013 09:46:22 +0200 Subject: [PATCH] Use equalizer instead of manually overwriting #== --- lib/twitter/base.rb | 15 --------------- lib/twitter/geo.rb | 9 ++------- lib/twitter/identity.rb | 14 +++----------- lib/twitter/size.rb | 17 ++--------------- lib/twitter/suggestion.rb | 8 ++------ lib/twitter/trend.rb | 9 ++------- spec/twitter/geo/point_spec.rb | 4 ++-- spec/twitter/geo/polygon_spec.rb | 4 ++-- spec/twitter/geo_spec.rb | 8 ++++---- spec/twitter/size_spec.rb | 14 ++------------ spec/twitter/suggestion_spec.rb | 14 ++------------ spec/twitter/trend_spec.rb | 14 ++------------ twitter.gemspec | 1 + 13 files changed, 26 insertions(+), 105 deletions(-) diff --git a/lib/twitter/base.rb b/lib/twitter/base.rb index 03262e5d9..e25bbdcee 100644 --- a/lib/twitter/base.rb +++ b/lib/twitter/base.rb @@ -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 diff --git a/lib/twitter/geo.rb b/lib/twitter/geo.rb index dcb1d4742..b2dd8cd3e 100644 --- a/lib/twitter/geo.rb +++ b/lib/twitter/geo.rb @@ -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 diff --git a/lib/twitter/identity.rb b/lib/twitter/identity.rb index a39e95925..a14e74842 100644 --- a/lib/twitter/identity.rb +++ b/lib/twitter/identity.rb @@ -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 # @@ -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 diff --git a/lib/twitter/size.rb b/lib/twitter/size.rb index 3987f1d0a..368c75741 100644 --- a/lib/twitter/size.rb +++ b/lib/twitter/size.rb @@ -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 diff --git a/lib/twitter/suggestion.rb b/lib/twitter/suggestion.rb index 441f60cde..0dc2033df 100644 --- a/lib/twitter/suggestion.rb +++ b/lib/twitter/suggestion.rb @@ -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] def users memoize(:users) do diff --git a/lib/twitter/trend.rb b/lib/twitter/trend.rb index eb3234a05..5b03a8c83 100644 --- a/lib/twitter/trend.rb +++ b/lib/twitter/trend.rb @@ -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 diff --git a/spec/twitter/geo/point_spec.rb b/spec/twitter/geo/point_spec.rb index 618473abc..1009d7eab 100644 --- a/spec/twitter/geo/point_spec.rb +++ b/spec/twitter/geo/point_spec.rb @@ -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]) diff --git a/spec/twitter/geo/polygon_spec.rb b/spec/twitter/geo/polygon_spec.rb index 68461f8fd..d9741d563 100644 --- a/spec/twitter/geo/polygon_spec.rb +++ b/spec/twitter/geo/polygon_spec.rb @@ -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]]]) diff --git a/spec/twitter/geo_spec.rb b/spec/twitter/geo_spec.rb index ff8ffe321..a10d91b29 100644 --- a/spec/twitter/geo_spec.rb +++ b/spec/twitter/geo_spec.rb @@ -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]]]) @@ -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 diff --git a/spec/twitter/size_spec.rb b/spec/twitter/size_spec.rb index ebbc48a06..80ae05db4 100644 --- a/spec/twitter/size_spec.rb +++ b/spec/twitter/size_spec.rb @@ -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) @@ -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 diff --git a/spec/twitter/suggestion_spec.rb b/spec/twitter/suggestion_spec.rb index 0343c4e3d..fe48ebffc 100644 --- a/spec/twitter/suggestion_spec.rb +++ b/spec/twitter/suggestion_spec.rb @@ -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") @@ -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 diff --git a/spec/twitter/trend_spec.rb b/spec/twitter/trend_spec.rb index 950272319..21b6c92bf 100644 --- a/spec/twitter/trend_spec.rb +++ b/spec/twitter/trend_spec.rb @@ -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") @@ -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 diff --git a/twitter.gemspec b/twitter.gemspec index c7a3ce5da..d2b720031 100644 --- a/twitter.gemspec +++ b/twitter.gemspec @@ -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'