Skip to content

Commit

Permalink
Add sort descriptors for properties to a Scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Aug 11, 2012
1 parent 25a462f commit 0d1d2a0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
17 changes: 11 additions & 6 deletions app/scope.rb
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion spec/scope_spec.rb
@@ -1,3 +1,13 @@
class NSSortDescriptor
def inspect
description
end

def ==(other)
description == other.description
end
end

module MotionData

describe Scope do
Expand All @@ -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
Expand Down Expand Up @@ -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.