Permalink
Browse files

Rename ComparableKeyPathExpression to Predicate::Builder.

  • Loading branch information...
1 parent b31da1a commit 25a462f6187680eab2f08fac39c643c02c851494 @alloy committed Aug 11, 2012
Showing with 70 additions and 66 deletions.
  1. +53 −49 app/predicate.rb
  2. +1 −1 app/scope.rb
  3. +15 −15 spec/predicate_spec.rb
  4. +1 −1 spec/scope_spec.rb
View
@@ -1,67 +1,71 @@
# TODO RM BridgeSupport bug
NSLocaleSensitivePredicateOption = 8
-# I did not want open up classes, but it's undoable to handle NSPredicate and
-# its subclasses consistently throughout the lib without a lot of extending, in
-# which case all instances are techinally opened-up anyways.
-class NSPredicate
- def inspect
- "(#{predicateFormat})"
- end
+module MotionData
+ module Predicate
+ def inspect
+ "(#{predicateFormat})"
+ end
- def and(predicate)
- NSCompoundPredicate.andPredicateWithSubpredicates([self, predicate])
- end
+ def and(predicate)
+ NSCompoundPredicate.andPredicateWithSubpredicates([self, predicate])
+ end
- def or(predicate)
- NSCompoundPredicate.orPredicateWithSubpredicates([self, predicate])
- end
-end
+ def or(predicate)
+ NSCompoundPredicate.orPredicateWithSubpredicates([self, predicate])
+ end
-module MotionData
- class ComparableKeyPathExpression
- module Mixin
- def value(keyPath)
- ComparableKeyPathExpression.new(keyPath)
+ class Builder
+ module Mixin
+ def value(keyPath)
+ Builder.new(keyPath)
+ end
end
- end
- attr_reader :expression, :comparisonOptions, :negate
+ attr_reader :leftExpression, :comparisonOptions, :negate
- def initialize(keyPath)
- @expression = NSExpression.expressionForKeyPath(keyPath.to_s)
- @comparisonOptions = 0
- @negate = false
- end
+ def initialize(keyPath)
+ @leftExpression = NSExpression.expressionForKeyPath(keyPath.to_s)
+ @comparisonOptions = 0
+ @negate = false
+ end
- def not; @negate = true; self; end
+ def not; @negate = true; self; end
- def caseInsensitive; @comparisonOptions |= NSCaseInsensitivePredicateOption; self; end
- def diacriticInsensitive; @comparisonOptions |= NSDiacriticInsensitivePredicateOption; self; end
- def localeSensitive; @comparisonOptions |= NSLocaleSensitivePredicateOption; self; end
+ def caseInsensitive; @comparisonOptions |= NSCaseInsensitivePredicateOption; self; end
+ def diacriticInsensitive; @comparisonOptions |= NSDiacriticInsensitivePredicateOption; self; end
+ def localeSensitive; @comparisonOptions |= NSLocaleSensitivePredicateOption; self; end
- def <(value); comparisonWith(value, type:NSLessThanPredicateOperatorType); end
- def >(value); comparisonWith(value, type:NSGreaterThanPredicateOperatorType); end
- def <=(value); comparisonWith(value, type:NSLessThanOrEqualToPredicateOperatorType); end
- def >=(value); comparisonWith(value, type:NSGreaterThanOrEqualToPredicateOperatorType); end
- def !=(value); comparisonWith(value, type:NSNotEqualToPredicateOperatorType); end
- def ==(value); comparisonWith(value, type:NSEqualToPredicateOperatorType); end
+ def <(value); comparisonWith(value, type:NSLessThanPredicateOperatorType); end
+ def >(value); comparisonWith(value, type:NSGreaterThanPredicateOperatorType); end
+ def <=(value); comparisonWith(value, type:NSLessThanOrEqualToPredicateOperatorType); end
+ def >=(value); comparisonWith(value, type:NSGreaterThanOrEqualToPredicateOperatorType); end
+ def !=(value); comparisonWith(value, type:NSNotEqualToPredicateOperatorType); end
+ def ==(value); comparisonWith(value, type:NSEqualToPredicateOperatorType); end
- def between?(min, max); comparisonWith([min, max], type:NSBetweenPredicateOperatorType); end
- def include?(value); comparisonWith(value, type:NSContainsPredicateOperatorType); end
- def in?(value); comparisonWith(value, type:NSInPredicateOperatorType); end
- def beginsWith?(string); comparisonWith(string, type:NSBeginsWithPredicateOperatorType); end
- def endsWith?(string); comparisonWith(string, type:NSEndsWithPredicateOperatorType); end
+ def between?(min, max); comparisonWith([min, max], type:NSBetweenPredicateOperatorType); end
+ def include?(value); comparisonWith(value, type:NSContainsPredicateOperatorType); end
+ def in?(value); comparisonWith(value, type:NSInPredicateOperatorType); end
+ def beginsWith?(string); comparisonWith(string, type:NSBeginsWithPredicateOperatorType); end
+ def endsWith?(string); comparisonWith(string, type:NSEndsWithPredicateOperatorType); end
- private
+ private
- def comparisonWith(value, type:comparisonType)
- predicate = NSComparisonPredicate.predicateWithLeftExpression(@expression,
- rightExpression:NSExpression.expressionForConstantValue(value),
- modifier:NSDirectPredicateModifier,
- type:comparisonType,
- options:@comparisonOptions)
- @negate ? NSCompoundPredicate.notPredicateWithSubpredicate(predicate) : predicate
+ def comparisonWith(value, type:comparisonType)
+ predicate = NSComparisonPredicate.predicateWithLeftExpression(@leftExpression,
+ rightExpression:NSExpression.expressionForConstantValue(value),
+ modifier:NSDirectPredicateModifier,
+ type:comparisonType,
+ options:@comparisonOptions)
+ @negate ? NSCompoundPredicate.notPredicateWithSubpredicate(predicate) : predicate
+ end
end
end
end
+
+# I did not want open up classes, but it's undoable to handle NSPredicate and
+# its subclasses consistently throughout the lib without a lot of extending, in
+# which case all instances are techinally opened-up anyways.
+class NSPredicate
+ include MotionData::Predicate
+end
View
@@ -21,7 +21,7 @@ def where(conditions, *formatArguments)
predicate = case conditions
when Hash
NSCompoundPredicate.andPredicateWithSubpredicates(conditions.map do |keyPath, value|
- ComparableKeyPathExpression.new(keyPath) == value
+ Predicate::Builder.new(keyPath) == value
end)
when Scope
conditions.predicate
View
@@ -1,11 +1,11 @@
module MotionData
- describe ComparableKeyPathExpression do
- extend ComparableKeyPathExpression::Mixin
+ describe Predicate::Builder do
+ extend Predicate::Builder::Mixin
it "returns a expression for the left-hand side of a comparison" do
- value(:property).expression.keyPath.should == 'property'
- value('property.subproperty').expression.keyPath.should == 'property.subproperty'
+ value(:property).leftExpression.keyPath.should == 'property'
+ value('property.subproperty').leftExpression.keyPath.should == 'property.subproperty'
end
it "returns comparison predicates" do
@@ -36,21 +36,21 @@ module MotionData
end
it "adds the option to perform a case-insensitive comparison" do
- expression = value(:name)
- expression.caseInsensitive.object_id.should == expression.object_id
- expression.comparisonOptions.should == NSCaseInsensitivePredicateOption
+ builder = value(:name)
+ builder.caseInsensitive.object_id.should == builder.object_id
+ builder.comparisonOptions.should == NSCaseInsensitivePredicateOption
end
it "adds the option to perform a diacritic-insensitive comparison" do
- expression = value(:name)
- expression.diacriticInsensitive.object_id.should == expression.object_id
- expression.comparisonOptions.should == NSDiacriticInsensitivePredicateOption
+ builder = value(:name)
+ builder.diacriticInsensitive.object_id.should == builder.object_id
+ builder.comparisonOptions.should == NSDiacriticInsensitivePredicateOption
end
it "adds the option to perform a locale-sensitive comparison" do
- expression = value(:name)
- expression.localeSensitive.object_id.should == expression.object_id
- expression.comparisonOptions.should == NSLocaleSensitivePredicateOption
+ builder = value(:name)
+ builder.localeSensitive.object_id.should == builder.object_id
+ builder.comparisonOptions.should == NSLocaleSensitivePredicateOption
end
it "combines comparison options" do
@@ -66,8 +66,8 @@ module MotionData
end
end
- describe NSPredicate do
- extend ComparableKeyPathExpression::Mixin
+ describe Predicate do
+ extend Predicate::Builder::Mixin
it "returns a compound `AND` predicate" do
predicate = ( value(:amount) < 42 ).and( value(:amount) > 42 ).and( value(:amount) != 21 )
View
@@ -9,7 +9,7 @@ module MotionData
end
describe Scope, "when building a new scope by applying finder options" do
- extend ComparableKeyPathExpression::Mixin
+ extend Predicate::Builder::Mixin
it "from a hash" do
scope1 = Scope.alloc.initWithTarget(Author)

0 comments on commit 25a462f

Please sign in to comment.