Permalink
Browse files

Add sort descriptors for properties to a Scope.

  • Loading branch information...
1 parent 25a462f commit 0d1d2a0705566e53c57cf61deab4e99d65fcb30a @alloy committed Aug 11, 2012
Showing with 45 additions and 7 deletions.
  1. +11 −6 app/scope.rb
  2. +34 −1 spec/scope_spec.rb
View
@@ -38,14 +38,19 @@ def where(conditions, *formatArguments)
inContext:@context)
end
- # Sort ascending by an attribute, or a NSSortDescriptor.
- def sortBy(attribute)
- sortBy(attribute, ascending:true)
+ # Sort ascending by a key-path, or a NSSortDescriptor.
+ def sortBy(property)
+ sortBy(property, ascending:true)
end
- # Sort by an attribute.
- def sortBy(attribute, ascending:ascending)
-
+ # Sort by a key-path.
+ def sortBy(keyPath, ascending:ascending)
+ sortDescriptors = @sortDescriptors ? @sortDescriptors.dup : []
+ sortDescriptors << NSSortDescriptor.alloc.initWithKey(keyPath.to_s, ascending:ascending)
+ Scope.alloc.initWithTarget(@target,
+ predicate:@predicate,
+ sortDescriptors:sortDescriptors,
+ inContext:@context)
end
# Factory methods
View
@@ -1,3 +1,13 @@
+class NSSortDescriptor
+ def inspect
+ description
+ end
+
+ def ==(other)
+ description == other.description
+ end
+end
+
module MotionData
describe Scope do
@@ -6,9 +16,13 @@ module MotionData
scope.target.should == Author
scope.context.should == Context.current
end
+
+ it "stores a copy of the given sort descriptors" do
+
+ end
end
- describe Scope, "when building a new scope by applying finder options" do
+ describe Scope, "when building a new scope by adding finder conditions" do
extend Predicate::Builder::Mixin
it "from a hash" do
@@ -82,4 +96,23 @@ module MotionData
end
end
+ describe Scope, "when building a new scope by adding sort conditions" do
+ extend Predicate::Builder::Mixin
+
+ it "sorts by a property" do
+ scope1 = Scope.alloc.initWithTarget(Author)
+
+ scope2 = scope1.sortBy(:name, ascending:true)
+ scope2.object_id.should.not == scope1.object_id
+ scope2.sortDescriptors.should == [NSSortDescriptor.alloc.initWithKey('name', ascending:true)]
+
+ scope3 = scope2.sortBy(:amount, ascending:false)
+ scope3.object_id.should.not == scope2.object_id
+ scope3.sortDescriptors.should == [
+ NSSortDescriptor.alloc.initWithKey('name', ascending:true),
+ NSSortDescriptor.alloc.initWithKey('amount', ascending:false)
+ ]
+ end
+ end
+
end

0 comments on commit 0d1d2a0

Please sign in to comment.