Skip to content

Commit

Permalink
Ensure a Scope assigns the same target to a child scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Aug 12, 2012
1 parent 6aa4b51 commit ee7595c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/scope.rb
Expand Up @@ -4,6 +4,10 @@ class Scope

attr_reader :target, :predicate, :sortDescriptors

def self.new
alloc.initWithTarget(nil)
end

def initWithTarget(target)
initWithTarget(target, predicate:nil, sortDescriptors:nil)
end
Expand Down
23 changes: 14 additions & 9 deletions spec/scope_spec.rb
Expand Up @@ -35,13 +35,18 @@ module MotionData
scope.sortDescriptors.should == descriptors
scope.sortDescriptors.object_id.should.not == descriptors.object_id
end

it "assigns the same target to a child scope" do
scope = Scope.alloc.initWithTarget(Object.new)
scope.where(:name => 'bob').target.should == scope.target
end
end

describe Scope, "when building a new scope by adding finder conditions" do
extend Predicate::Builder::Mixin

it "from a hash" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.where(:name => 'bob', :amount => 42)
scope2.predicate.predicateFormat.should == 'name == "bob" AND amount == 42'
Expand All @@ -51,7 +56,7 @@ module MotionData
end

it "from a scope" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.where(( value(:name).caseInsensitive != 'bob' ).or( value(:amount) > 42 )).sortBy(:name)
scope3 = scope1.where(( value(:enabled) == true ).and( value('job.title') != nil )).sortBy(:amount, ascending:false)
Expand All @@ -65,7 +70,7 @@ module MotionData
end

it "from a NSPredicate" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.where(NSPredicate.predicateWithFormat('name != %@ OR amount > %@', argumentArray:['bob', 42]))
scope2.predicate.predicateFormat.should == 'name != "bob" OR amount > 42'
Expand All @@ -75,7 +80,7 @@ module MotionData
end

it "from a predicate string" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.where('name != %@ OR amount > %@', 'bob', 42)
scope2.predicate.predicateFormat.should == 'name != "bob" OR amount > 42'
Expand All @@ -85,7 +90,7 @@ module MotionData
end

it "does not modify the original scopes" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.where(:name => 'bob')
scope2.object_id.should.not == scope1.object_id
Expand All @@ -107,7 +112,7 @@ 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 = Scope.new.sortBy(:name, ascending:true)
scope1.sortDescriptors.should == [NSSortDescriptor.alloc.initWithKey('name', ascending:true)]

scope2 = scope1.sortBy(:amount, ascending:false)
Expand All @@ -118,18 +123,18 @@ module MotionData
end

it "sorts by a property and ascending" do
scope = Scope.alloc.initWithTarget(Author).sortBy(:name)
scope = Scope.new.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 = Scope.new.sortBy(sortDescriptor)
scope.sortDescriptors.should == [sortDescriptor]
end

it "does not modify the original scope" do
scope1 = Scope.alloc.initWithTarget(Author)
scope1 = Scope.new

scope2 = scope1.sortBy(:name)
scope2.object_id.should.not == scope1.object_id
Expand Down

0 comments on commit ee7595c

Please sign in to comment.