From 99b2188f9495068399000b891c2c9b66331c4b02 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sat, 16 Feb 2008 20:46:05 -0800 Subject: introduced __collect__, an internal enumerating operator. This is a map down the tree representing the relation. Monadic map to be precise --- lib/active_relation/.DS_Store | Bin 0 -> 6148 bytes lib/active_relation/predicates.rb | 15 ++++++++++----- lib/active_relation/relations/aggregation.rb | 15 ++++++++------- lib/active_relation/relations/alias.rb | 12 ++++++++---- lib/active_relation/relations/compound.rb | 4 ++++ lib/active_relation/relations/join.rb | 6 +++++- lib/active_relation/relations/order.rb | 5 +++-- lib/active_relation/relations/projection.rb | 7 ++++--- lib/active_relation/relations/range.rb | 5 +++-- lib/active_relation/relations/relation.rb | 4 ++++ lib/active_relation/relations/rename.rb | 9 +++++---- lib/active_relation/relations/selection.rb | 11 ++++++----- lib/active_relation/relations/table.rb | 2 +- 13 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 lib/active_relation/.DS_Store (limited to 'lib') diff --git a/lib/active_relation/.DS_Store b/lib/active_relation/.DS_Store new file mode 100644 index 0000000000..2a449ff62e Binary files /dev/null and b/lib/active_relation/.DS_Store differ diff --git a/lib/active_relation/predicates.rb b/lib/active_relation/predicates.rb index 76288595dc..880412fdb5 100644 --- a/lib/active_relation/predicates.rb +++ b/lib/active_relation/predicates.rb @@ -15,18 +15,23 @@ module ActiveRelation def ==(other) super and @attribute == other.attribute and @operand == other.operand end - - def qualify - self.class.new(attribute.qualify, operand.qualify) - end def bind(relation) - self.class.new(attribute.bind(relation), operand.bind(relation)) + __collect__{ |x| x.bind(relation) } + end + + def qualify + __collect__(&:qualify) end def to_sql(strategy = Sql::Predicate.new) "#{attribute.to_sql(strategy)} #{predicate_sql} #{operand.to_sql(strategy)}" end + + protected + def __collect__ + self.class.new(yield(attribute), yield(operand)) + end end class Equality < Binary diff --git a/lib/active_relation/relations/aggregation.rb b/lib/active_relation/relations/aggregation.rb index 7910223673..bcdfaa7d53 100644 --- a/lib/active_relation/relations/aggregation.rb +++ b/lib/active_relation/relations/aggregation.rb @@ -7,15 +7,11 @@ module ActiveRelation end def ==(other) - self.class == other.class and - relation == other.relation and - groupings == other.groupings and + self.class == other.class and + relation == other.relation and + groupings == other.groupings and expressions == other.expressions end - - def qualify - Aggregation.new(relation.qualify, :expressions => expressions.collect(&:qualify), :groupings => groupings.collect(&:qualify)) - end def attributes expressions.collect { |e| e.bind(self) } @@ -24,5 +20,10 @@ module ActiveRelation def aggregation? true end + + protected + def __collect__(&block) + Aggregation.new(relation.__collect__(&block), :expressions => expressions.collect(&block), :groupings => groupings.collect(&block)) + end end end \ No newline at end of file diff --git a/lib/active_relation/relations/alias.rb b/lib/active_relation/relations/alias.rb index 941a95c58e..cc6e9b09b8 100644 --- a/lib/active_relation/relations/alias.rb +++ b/lib/active_relation/relations/alias.rb @@ -1,15 +1,19 @@ module ActiveRelation class Alias < Compound attr_reader :alias + + def initialize(relation, aliaz) + @relation, @alias = relation, aliaz + end def aliased_prefix_for(attribute) self[attribute] and @alias end - - def initialize(relation, aliaz) - @relation, @alias = relation, aliaz + + def __collect__(&block) + Alias.new(relation.__collect__(&block), @alias) end - + def ==(other) self.class == other.class and relation == other.relation and diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index d11d09fbf6..a02fbbeef5 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -8,5 +8,9 @@ module ActiveRelation def attributes relation.attributes.collect { |a| a.bind(self) } end + + def qualify + __collect__(&:qualify) + end end end \ No newline at end of file diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index c61680fd55..a5d2b5355b 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -15,7 +15,7 @@ module ActiveRelation end def qualify - Join.new(join_sql, relation1.qualify, relation2.qualify, *predicates.collect(&:qualify)) + __collect__(&:qualify) end def attributes @@ -48,5 +48,9 @@ module ActiveRelation def table_sql relation1.aggregation?? relation1.to_sql(Sql::Aggregation.new) : relation1.send(:table_sql) end + + def __collect__(&block) + Join.new(join_sql, relation1.__collect__(&block), relation2.__collect__(&block), *predicates.collect(&block)) + end end end \ No newline at end of file diff --git a/lib/active_relation/relations/order.rb b/lib/active_relation/relations/order.rb index a23da967fd..90568a90ac 100644 --- a/lib/active_relation/relations/order.rb +++ b/lib/active_relation/relations/order.rb @@ -11,8 +11,9 @@ module ActiveRelation orders == other.orders end - def qualify - Order.new(relation.qualify, *orders.collect(&:qualify)) + protected + def __collect__(&block) + Order.new(relation.__collect__(&block), *orders.collect(&block)) end end end \ No newline at end of file diff --git a/lib/active_relation/relations/projection.rb b/lib/active_relation/relations/projection.rb index d0c68869bd..3fb594e2bb 100644 --- a/lib/active_relation/relations/projection.rb +++ b/lib/active_relation/relations/projection.rb @@ -15,9 +15,10 @@ module ActiveRelation relation == other.relation and projections == other.projections end - - def qualify - Projection.new(relation.qualify, *projections.collect(&:qualify)) + + protected + def __collect__(&block) + Projection.new(relation.__collect__(&block), *projections.collect(&block)) end end end \ No newline at end of file diff --git a/lib/active_relation/relations/range.rb b/lib/active_relation/relations/range.rb index 7f120ab9e5..5cc5db094d 100644 --- a/lib/active_relation/relations/range.rb +++ b/lib/active_relation/relations/range.rb @@ -19,8 +19,9 @@ module ActiveRelation range.begin end - def qualify - Range.new(relation.qualify, range) + protected + def __collect__(&block) + Range.new(relation.__collect__(&block), range) end end end \ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index c65120a75c..1c48d8be02 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -104,6 +104,10 @@ module ActiveRelation alias_method :to_s, :to_sql protected + def __collect__ + yield self + end + def connection ActiveRecord::Base.connection end diff --git a/lib/active_relation/relations/rename.rb b/lib/active_relation/relations/rename.rb index 20e79ebefb..718dbfe30f 100644 --- a/lib/active_relation/relations/rename.rb +++ b/lib/active_relation/relations/rename.rb @@ -13,15 +13,16 @@ module ActiveRelation attribute == other.attribute and pseudonym == other.pseudonym end - - def qualify - Rename.new(relation.qualify, attribute.qualify => pseudonym) - end def attributes relation.attributes.collect(&method(:baptize)) end + protected + def __collect__(&block) + Rename.new(relation.__collect__(&block), yield(attribute) => pseudonym) + end + private def baptize(attribute) (attribute =~ self.attribute ? attribute.as(pseudonym) : attribute).bind(self) rescue nil diff --git a/lib/active_relation/relations/selection.rb b/lib/active_relation/relations/selection.rb index 98f56b0cbd..93e0e16dc5 100644 --- a/lib/active_relation/relations/selection.rb +++ b/lib/active_relation/relations/selection.rb @@ -12,14 +12,15 @@ module ActiveRelation relation == other.relation and predicate == other.predicate end - - def qualify - Selection.new(relation.qualify, predicate.qualify) - end - + protected def selects relation.send(:selects) + [predicate] end + + def __collect__(&block) + Selection.new(relation.__collect__(&block), yield(predicate)) + end + end end \ No newline at end of file diff --git a/lib/active_relation/relations/table.rb b/lib/active_relation/relations/table.rb index d85c268019..18c851c847 100644 --- a/lib/active_relation/relations/table.rb +++ b/lib/active_relation/relations/table.rb @@ -25,7 +25,7 @@ module ActiveRelation def table_sql "#{quote_table_name(name)}" end - + private def qualifications attributes.zip(attributes.collect(&:qualified_name)).to_hash -- cgit v1.2.3