From ae0fe8f6c8c41b5abf8ced6a99b19dacdf8f57eb Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Fri, 11 Apr 2008 18:24:18 -0700 Subject: redesigned the way limit and offset work - was range now have special 'take' and 'skip' operations. (the terminology comes from linq) --- lib/active_relation/relations.rb | 3 ++- lib/active_relation/relations/compound.rb | 4 ++-- lib/active_relation/relations/range.rb | 26 -------------------------- lib/active_relation/relations/relation.rb | 18 ++++++++++++------ lib/active_relation/relations/skip.rb | 18 ++++++++++++++++++ lib/active_relation/relations/take.rb | 18 ++++++++++++++++++ 6 files changed, 52 insertions(+), 35 deletions(-) delete mode 100644 lib/active_relation/relations/range.rb create mode 100644 lib/active_relation/relations/skip.rb create mode 100644 lib/active_relation/relations/take.rb (limited to 'lib') diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index d914fca094..240c20736e 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -8,7 +8,8 @@ require 'active_relation/relations/aggregation' require 'active_relation/relations/projection' require 'active_relation/relations/selection' require 'active_relation/relations/order' -require 'active_relation/relations/range' +require 'active_relation/relations/take' +require 'active_relation/relations/skip' require 'active_relation/relations/rename' require 'active_relation/relations/deletion' require 'active_relation/relations/insertion' diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index 5778fe9b22..c5af453e4b 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -4,8 +4,8 @@ module ActiveRelation hash_on :relation - delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, - :offset, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, + delegate :joins, :selects, :orders, :groupings, :table_sql, :inserts, :take, + :skip, :name, :alias, :aggregation?, :alias?, :prefix_for, :column_for, :engine, :to => :relation diff --git a/lib/active_relation/relations/range.rb b/lib/active_relation/relations/range.rb deleted file mode 100644 index fafdef5902..0000000000 --- a/lib/active_relation/relations/range.rb +++ /dev/null @@ -1,26 +0,0 @@ -module ActiveRelation - class Range < Compound - attr_reader :range - - def initialize(relation, range) - @relation, @range = relation, range - end - - def ==(other) - relation == other.relation and - range == other.range - end - - def limit - range.end - range.begin + 1 - end - - def offset - range.begin - end - - def descend(&block) - Range.new(relation.descend(&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 1b80494cd7..b26fac4e85 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -35,8 +35,6 @@ module ActiveRelation case index when Symbol, String attribute_for_name(index) - when ::Range - Range.new(self, index) when Attribute, Expression attribute_for_attribute(index) end @@ -57,6 +55,14 @@ module ActiveRelation def order(*attributes) Order.new(self, *attributes) end + + def take(taken) + Take.new(self, taken) + end + + def skip(skipped) + Skip.new(self, skipped) + end def rename(attribute, aliaz) Rename.new(self, attribute => aliaz) @@ -114,8 +120,8 @@ module ActiveRelation ("WHERE #{selects.collect { |s| s.to_sql(Sql::WhereClause.new(engine)) }.join("\n\tAND ")}" unless selects.blank? ), ("ORDER BY #{orders.collect { |o| o.to_sql(Sql::OrderClause.new(engine)) }.join(', ')}" unless orders.blank? ), ("GROUP BY #{groupings.collect(&:to_sql)}" unless groupings.blank? ), - ("LIMIT #{limit}" unless limit.blank? ), - ("OFFSET #{offset}" unless offset.blank? ) + ("LIMIT #{take}" unless take.blank? ), + ("OFFSET #{skip}" unless skip.blank? ) ].compact.join("\n"), self.alias end alias_method :to_s, :to_sql @@ -153,8 +159,8 @@ module ActiveRelation def inserts; [] end def groupings; [] end def joins; nil end - def limit; nil end - def offset; nil end + def take; nil end + def skip; nil end def alias; nil end end end \ No newline at end of file diff --git a/lib/active_relation/relations/skip.rb b/lib/active_relation/relations/skip.rb new file mode 100644 index 0000000000..f963ee6468 --- /dev/null +++ b/lib/active_relation/relations/skip.rb @@ -0,0 +1,18 @@ +module ActiveRelation + class Skip < Compound + attr_reader :skip + + def initialize(relation, skip) + @relation, @skip = relation, skip + end + + def ==(other) + relation == other.relation and + skip == other.skip + end + + def descend(&block) + Skip.new(relation.descend(&block), skip) + end + end +end \ No newline at end of file diff --git a/lib/active_relation/relations/take.rb b/lib/active_relation/relations/take.rb new file mode 100644 index 0000000000..9f98207798 --- /dev/null +++ b/lib/active_relation/relations/take.rb @@ -0,0 +1,18 @@ +module ActiveRelation + class Take < Compound + attr_reader :take + + def initialize(relation, take) + @relation, @take = relation, take + end + + def ==(other) + relation == other.relation and + take == other.take + end + + def descend(&block) + Take.new(relation.descend(&block), take) + end + end +end \ No newline at end of file -- cgit v1.2.3