aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/engines/memory
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arel/engines/memory')
-rw-r--r--lib/arel/engines/memory/primitives.rb14
-rw-r--r--lib/arel/engines/memory/relations.rb1
-rw-r--r--lib/arel/engines/memory/relations/compound.rb5
-rw-r--r--lib/arel/engines/memory/relations/operations.rb33
4 files changed, 53 insertions, 0 deletions
diff --git a/lib/arel/engines/memory/primitives.rb b/lib/arel/engines/memory/primitives.rb
index 77d4c1a52c..f8bbcedb55 100644
--- a/lib/arel/engines/memory/primitives.rb
+++ b/lib/arel/engines/memory/primitives.rb
@@ -10,4 +10,18 @@ module Arel
value
end
end
+
+ class Ordering
+ def eval(row1, row2)
+ (attribute.eval(row1) <=> attribute.eval(row2)) * direction
+ end
+ end
+
+ class Descending < Ordering
+ def direction; -1 end
+ end
+
+ class Ascending < Ordering
+ def direction; 1 end
+ end
end \ No newline at end of file
diff --git a/lib/arel/engines/memory/relations.rb b/lib/arel/engines/memory/relations.rb
index 9f8264c439..1b009537b9 100644
--- a/lib/arel/engines/memory/relations.rb
+++ b/lib/arel/engines/memory/relations.rb
@@ -1,3 +1,4 @@
require 'arel/engines/memory/relations/array'
require 'arel/engines/memory/relations/operations'
+require 'arel/engines/memory/relations/compound'
diff --git a/lib/arel/engines/memory/relations/compound.rb b/lib/arel/engines/memory/relations/compound.rb
new file mode 100644
index 0000000000..b029082d57
--- /dev/null
+++ b/lib/arel/engines/memory/relations/compound.rb
@@ -0,0 +1,5 @@
+module Arel
+ class Compound < Relation
+ delegate :array, :to => :relation
+ end
+end
diff --git a/lib/arel/engines/memory/relations/operations.rb b/lib/arel/engines/memory/relations/operations.rb
index eb11fb55fd..115df054df 100644
--- a/lib/arel/engines/memory/relations/operations.rb
+++ b/lib/arel/engines/memory/relations/operations.rb
@@ -4,4 +4,37 @@ module Arel
relation.eval.select { |row| predicate.eval(row) }
end
end
+
+ class Order < Compound
+ def eval
+ relation.eval.sort do |row1, row2|
+ ordering = orderings.detect { |o| o.eval(row1, row2) != 0 } || orderings.last
+ ordering.eval(row1, row2)
+ end
+ end
+ end
+
+ class Project < Compound
+ def eval
+ relation.eval.collect { |r| r.slice(*projections) }
+ end
+ end
+
+ class Take < Compound
+ def eval
+ relation.eval[0, taken]
+ end
+ end
+
+ class Skip < Compound
+ def eval
+ relation.eval[skipped..-1]
+ end
+ end
+
+ class Group < Compound
+ def eval
+ raise NotImplementedError
+ end
+ end
end \ No newline at end of file