From 724a2684139342eb4613f78bfae723ef00911ff7 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sat, 17 May 2008 20:24:22 -0700 Subject: performance enhancing drugs --- lib/arel/extensions/class.rb | 4 ++-- lib/arel/predicates.rb | 2 +- lib/arel/primitives/attribute.rb | 12 ++++++++---- lib/arel/primitives/expression.rb | 3 ++- lib/arel/primitives/value.rb | 4 ++-- lib/arel/relations/aggregation.rb | 2 +- lib/arel/relations/compound.rb | 2 +- lib/arel/relations/deletion.rb | 2 +- lib/arel/relations/grouping.rb | 2 +- lib/arel/relations/insertion.rb | 2 +- lib/arel/relations/join.rb | 2 +- lib/arel/relations/nil.rb | 2 +- lib/arel/relations/order.rb | 2 +- lib/arel/relations/projection.rb | 2 +- lib/arel/relations/recursion.rb | 4 ++-- lib/arel/relations/relation.rb | 14 +++++++++++++- lib/arel/relations/selection.rb | 2 +- lib/arel/relations/skip.rb | 2 +- lib/arel/relations/table.rb | 4 ++-- lib/arel/relations/take.rb | 2 +- lib/arel/relations/update.rb | 2 +- 21 files changed, 45 insertions(+), 28 deletions(-) (limited to 'lib') diff --git a/lib/arel/extensions/class.rb b/lib/arel/extensions/class.rb index 48d07a45f9..09e6d86ed4 100644 --- a/lib/arel/extensions/class.rb +++ b/lib/arel/extensions/class.rb @@ -1,11 +1,11 @@ class Class - def hash_on(*delegatees) + def hash_on(delegatee) define_method :eql? do |other| self == other end define_method :hash do - @hash ||= delegatees.map(&:hash).sum + @hash ||= delegatee.hash end end end \ No newline at end of file diff --git a/lib/arel/predicates.rb b/lib/arel/predicates.rb index cad4969952..93c7ed0099 100644 --- a/lib/arel/predicates.rb +++ b/lib/arel/predicates.rb @@ -28,7 +28,7 @@ module Arel class Equality < Binary def ==(other) - self.class == other.class and + Equality == other.class and ((operand1 == other.operand1 and operand2 == other.operand2) or (operand1 == other.operand2 and operand2 == other.operand1)) end diff --git a/lib/arel/primitives/attribute.rb b/lib/arel/primitives/attribute.rb index f3291b18da..5e769ac0eb 100644 --- a/lib/arel/primitives/attribute.rb +++ b/lib/arel/primitives/attribute.rb @@ -30,11 +30,11 @@ module Arel end def ==(other) - self.class == other.class and + Attribute == other.class and name == other.name and @alias == other.alias and - relation == other.relation and - ancestor == other.ancestor + ancestor == other.ancestor and + relation == other.relation end def original_relation @@ -76,7 +76,11 @@ module Arel end def match?(other) - !(history & other.history).empty? + history.last == other.history.last + end + + def root + history.last end def descends_from?(other) diff --git a/lib/arel/primitives/expression.rb b/lib/arel/primitives/expression.rb index 3cde9d8724..76b76538c9 100644 --- a/lib/arel/primitives/expression.rb +++ b/lib/arel/primitives/expression.rb @@ -17,7 +17,7 @@ module Arel end def ==(other) - self.class == other.class and + Expression == other.class and attribute == other.attribute and function_sql == other.function_sql and ancestor == other.ancestor and @@ -29,6 +29,7 @@ module Arel Expression.new(attribute, function_sql, aliaz, self) end + # FIXME def bind(new_relation) # new_relation == relation ? self : Expression.new(attribute.bind(new_relation), function_sql, @alias, self) self diff --git a/lib/arel/primitives/value.rb b/lib/arel/primitives/value.rb index 8ed3c4ce30..b4bddd0b0c 100644 --- a/lib/arel/primitives/value.rb +++ b/lib/arel/primitives/value.rb @@ -17,8 +17,8 @@ module Arel end def ==(other) - self.class == other.class and - value == other.value + Value == other.class and + value == other.value end def bind(relation) diff --git a/lib/arel/relations/aggregation.rb b/lib/arel/relations/aggregation.rb index d18ef014fa..d409c34284 100644 --- a/lib/arel/relations/aggregation.rb +++ b/lib/arel/relations/aggregation.rb @@ -19,7 +19,7 @@ module Arel end def ==(other) - self.class == other.class and + Aggregation == other.class and self.relation == other.relation end end diff --git a/lib/arel/relations/compound.rb b/lib/arel/relations/compound.rb index 55b2bc80c7..abc5838805 100644 --- a/lib/arel/relations/compound.rb +++ b/lib/arel/relations/compound.rb @@ -12,7 +12,7 @@ module Arel end def relation_for(attribute) - join? && relation.relation_for(attribute) || self[attribute] && self + join? && relation.relation_for(attribute) || has_attribute?(attribute) && self end end end \ No newline at end of file diff --git a/lib/arel/relations/deletion.rb b/lib/arel/relations/deletion.rb index 6c802ba905..8c9d873a02 100644 --- a/lib/arel/relations/deletion.rb +++ b/lib/arel/relations/deletion.rb @@ -18,7 +18,7 @@ module Arel end def ==(other) - self.class == other.class and + Deletion == other.class and relation == other.relation end end diff --git a/lib/arel/relations/grouping.rb b/lib/arel/relations/grouping.rb index 2c39d23b2d..39b5942bcc 100644 --- a/lib/arel/relations/grouping.rb +++ b/lib/arel/relations/grouping.rb @@ -7,7 +7,7 @@ module Arel end def ==(other) - self.class == other.class and + Grouping == other.class and relation == other.relation and groupings == other.groupings end diff --git a/lib/arel/relations/insertion.rb b/lib/arel/relations/insertion.rb index 37e7be8757..cc7fcb4fb0 100644 --- a/lib/arel/relations/insertion.rb +++ b/lib/arel/relations/insertion.rb @@ -20,7 +20,7 @@ module Arel end def ==(other) - self.class == other.class and + Insertion == other.class and relation == other.relation and record == other.record end diff --git a/lib/arel/relations/join.rb b/lib/arel/relations/join.rb index 88d5f45b67..abd4eae4f6 100644 --- a/lib/arel/relations/join.rb +++ b/lib/arel/relations/join.rb @@ -9,7 +9,7 @@ module Arel end def ==(other) - self.class == other.class and + Join == other.class and predicates == other.predicates and ( (relation1 == other.relation1 and relation2 == other.relation2) or (relation2 == other.relation1 and relation1 == other.relation2) diff --git a/lib/arel/relations/nil.rb b/lib/arel/relations/nil.rb index 714b11cc2a..e8d4cbc481 100644 --- a/lib/arel/relations/nil.rb +++ b/lib/arel/relations/nil.rb @@ -5,7 +5,7 @@ module Arel def name; '' end def ==(other) - self.class == other.class + Nil == other.class end end end \ No newline at end of file diff --git a/lib/arel/relations/order.rb b/lib/arel/relations/order.rb index 2d72127722..31032e871c 100644 --- a/lib/arel/relations/order.rb +++ b/lib/arel/relations/order.rb @@ -11,7 +11,7 @@ module Arel end def ==(other) - self.class == other.class and + Order == other.class and relation == other.relation and orderings == other.orderings end diff --git a/lib/arel/relations/projection.rb b/lib/arel/relations/projection.rb index 7c64726852..7eea63d808 100644 --- a/lib/arel/relations/projection.rb +++ b/lib/arel/relations/projection.rb @@ -15,7 +15,7 @@ module Arel end def ==(other) - self.class == other.class and + Projection == other.class and relation == other.relation and projections == other.projections end diff --git a/lib/arel/relations/recursion.rb b/lib/arel/relations/recursion.rb index fa4d9969c9..c2fcf1bd21 100644 --- a/lib/arel/relations/recursion.rb +++ b/lib/arel/relations/recursion.rb @@ -10,8 +10,8 @@ module Arel end def relation_for(attribute) - self[attribute] && self - end + has_attribute?(attribute) && self + end end end end \ No newline at end of file diff --git a/lib/arel/relations/relation.rb b/lib/arel/relations/relation.rb index 920bcd2d8d..3704eb9318 100644 --- a/lib/arel/relations/relation.rb +++ b/lib/arel/relations/relation.rb @@ -136,10 +136,22 @@ module Arel end def find_attribute_matching_attribute(attribute) - attributes.select { |a| a.match?(attribute) }.max do |a1, a2| + matching_attributes(attribute).max do |a1, a2| (a1.original_attribute / attribute) <=> (a2.original_attribute / attribute) end end + + private + def matching_attributes(attribute) + (@matching_attributes ||= attributes.inject({}) do |hash, a| + (hash[a.root] ||= []) << a + hash + end)[attribute.root] || [] + end + + def has_attribute?(attribute) + !matching_attributes(attribute).empty? + end end include AttributeAccessable diff --git a/lib/arel/relations/selection.rb b/lib/arel/relations/selection.rb index c50db1c88a..0c5956d2fc 100644 --- a/lib/arel/relations/selection.rb +++ b/lib/arel/relations/selection.rb @@ -13,7 +13,7 @@ module Arel end def ==(other) - self.class == other.class and + Selection == other.class and relation == other.relation and predicate == other.predicate end diff --git a/lib/arel/relations/skip.rb b/lib/arel/relations/skip.rb index f17e439ebf..4686e03177 100644 --- a/lib/arel/relations/skip.rb +++ b/lib/arel/relations/skip.rb @@ -7,7 +7,7 @@ module Arel end def ==(other) - self.class == other.class and + Skip == other.class and relation == other.relation and skipped == other.skipped end diff --git a/lib/arel/relations/table.rb b/lib/arel/relations/table.rb index b95e51a307..06625dd52a 100644 --- a/lib/arel/relations/table.rb +++ b/lib/arel/relations/table.rb @@ -17,7 +17,7 @@ module Arel end def column_for(attribute) - self[attribute] and columns.detect { |c| c.name == attribute.name.to_s } + has_attribute?(attribute) and columns.detect { |c| c.name == attribute.name.to_s } end def columns @@ -29,7 +29,7 @@ module Arel end def ==(other) - self.class == other.class and + Table == other.class and name == other.name end end diff --git a/lib/arel/relations/take.rb b/lib/arel/relations/take.rb index d2743d7a6e..80aa62a878 100644 --- a/lib/arel/relations/take.rb +++ b/lib/arel/relations/take.rb @@ -7,7 +7,7 @@ module Arel end def ==(other) - self.class == other.class and + Take == other.class and relation == other.relation and taken == other.taken end diff --git a/lib/arel/relations/update.rb b/lib/arel/relations/update.rb index f1f6776f15..486ad2ab12 100644 --- a/lib/arel/relations/update.rb +++ b/lib/arel/relations/update.rb @@ -22,7 +22,7 @@ module Arel end def ==(other) - self.class == other.class and + Update == other.class and relation == other.relation and assignments == other.assignments end -- cgit v1.2.3