Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add NSSortDescriptor instances to a Scope.

  • Loading branch information...
commit 5d47e128bf19441d017673417774927a3b441e2f 1 parent f7629cd
@alloy authored
Showing with 50 additions and 17 deletions.
  1. +19 −9 app/scope.rb
  2. +31 −8 spec/scope_spec.rb
View
28 app/scope.rb
@@ -9,7 +9,7 @@ def initWithTarget(target)
def initWithTarget(target, predicate:predicate, sortDescriptors:sortDescriptors, inContext:context)
if init
@target, @predicate, @context = target, predicate, context
- @sortDescriptors = sortDescriptors.dup if sortDescriptors
+ @sortDescriptors = sortDescriptors ? sortDescriptors.dup : []
end
self
end
@@ -40,18 +40,17 @@ def where(conditions, *formatArguments)
end
# Sort ascending by a key-path, or a NSSortDescriptor.
- def sortBy(property)
- sortBy(property, ascending:true)
+ def sortBy(keyPathOrSortDescriptor)
+ if keyPathOrSortDescriptor.is_a?(NSSortDescriptor)
+ addSortDescriptor(keyPathOrSortDescriptor)
+ else
+ sortBy(keyPathOrSortDescriptor, ascending:true)
+ end
end
# 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)
+ addSortDescriptor NSSortDescriptor.alloc.initWithKey(keyPath.to_s, ascending:ascending)
end
# Factory methods
@@ -74,6 +73,17 @@ def controller(options = {})
cacheName:options[:cacheName])
end
+ private
+
+ def addSortDescriptor(sortDescriptor)
+ sortDescriptors = @sortDescriptors.dup
+ sortDescriptors << sortDescriptor
+ Scope.alloc.initWithTarget(@target,
+ predicate:@predicate,
+ sortDescriptors:sortDescriptors,
+ inContext:@context)
+ end
+
#class ToManyRelationship < Scope
## Returns the relationship set, normally provided by a Core Data to-many
## relationship.
View
39 spec/scope_spec.rb
@@ -58,7 +58,7 @@ module MotionData
scope3.predicate.predicateFormat.should == '(name BEGINSWITH "bob" OR amount > 42) AND enabled == 1'
end
- it "from a normal NSPredicate" do
+ it "from a NSPredicate" do
scope1 = Scope.alloc.initWithTarget(Author)
scope2 = scope1.where(NSPredicate.predicateWithFormat('name != %@ OR amount > %@', argumentArray:['bob', 42]))
@@ -101,18 +101,41 @@ module MotionData
describe Scope, "when building a new scope by adding sort conditions" do
it "sorts by a property" do
+ scope1 = Scope.alloc.initWithTarget(Author).sortBy(:name, ascending:true)
+ scope1.sortDescriptors.should == [NSSortDescriptor.alloc.initWithKey('name', ascending:true)]
+
+ scope2 = scope1.sortBy(:amount, ascending:false)
+ scope2.sortDescriptors.should == [
+ NSSortDescriptor.alloc.initWithKey('name', ascending:true),
+ NSSortDescriptor.alloc.initWithKey('amount', ascending:false)
+ ]
+ end
+
+ it "sorts by a property and ascending" do
+ scope = Scope.alloc.initWithTarget(Author).sortBy(:name)
+ scope.sortDescriptors.should == [NSSortDescriptor.alloc.initWithKey('name', ascending:true)]
+ end
+
+ it "sorts by a NSSortDescriptor" do
+ sortDescriptor = NSSortDescriptor.alloc.initWithKey('amount', ascending:true)
+ scope = Scope.alloc.initWithTarget(Author).sortBy(sortDescriptor)
+ scope.sortDescriptors.should == [sortDescriptor]
+ end
+
+ it "does not modify the original scope" do
scope1 = Scope.alloc.initWithTarget(Author)
- scope2 = scope1.sortBy(:name, ascending:true)
+ scope2 = scope1.sortBy(:name)
scope2.object_id.should.not == scope1.object_id
- scope2.sortDescriptors.should == [NSSortDescriptor.alloc.initWithKey('name', ascending:true)]
+ scope2.sortDescriptors.size.should == scope1.sortDescriptors.size + 1
- scope3 = scope2.sortBy(:amount, ascending:false)
+ scope3 = scope2.sortBy(:name, 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)
- ]
+ scope3.sortDescriptors.size.should == scope2.sortDescriptors.size + 1
+
+ scope4 = scope3.sortBy(NSSortDescriptor.alloc.initWithKey('amount', ascending:true))
+ scope4.object_id.should.not == scope3.object_id
+ scope4.sortDescriptors.size.should == scope3.sortDescriptors.size + 1
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.