Permalink
Browse files

Modify the predicate comparison options.

  • Loading branch information...
1 parent 823a3b5 commit 0989fd3fa02412d6523850073cb8706279bd2750 @alloy committed Aug 10, 2012
Showing with 75 additions and 23 deletions.
  1. +33 −16 app/predicate.rb
  2. +42 −7 spec/predicate_spec.rb
View
@@ -1,39 +1,56 @@
-module MotionData
- class Predicate < NSComparisonPredicate
- def inspect
- "(#{predicateFormat})"
- end
+# TODO RM BridgeSupport bug
+NSLocaleSensitivePredicateOption = 8
+
+class NSPredicate
+ def inspect
+ "(#{predicateFormat})"
+ end
+end
+module MotionData
+ module Predicate
def and(predicate)
- NSCompoundPredicate.andPredicateWithSubpredicates([self, predicate])
+ Compound.andPredicateWithSubpredicates([self, predicate])
end
def or(predicate)
- NSCompoundPredicate.orPredicateWithSubpredicates([self, predicate])
+ Compound.orPredicateWithSubpredicates([self, predicate])
end
- class KeyPathExpression
+ class Comparison < NSComparisonPredicate
+ include Predicate
+ end
+
+ class Compound < NSCompoundPredicate
+ include Predicate
+ end
+
+ class ComparableKeyPathExpression
module Mixin
def keyPath(keyPath)
- KeyPathExpression.new(keyPath)
+ ComparableKeyPathExpression.new(keyPath)
end
alias_method :key, :keyPath
end
- attr_reader :expression
+ attr_reader :expression, :comparisonOptions
def initialize(keyPath)
@expression = NSExpression.expressionForKeyPath(keyPath.to_s)
+ @comparisonOptions = 0
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 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
@@ -44,11 +61,11 @@ def endsWith?(string); comparisonWith(string, type:NSEndsWithPredicateOper
def comparisonWith(value, type:comparisonType)
value = NSExpression.expressionForConstantValue(value)
- Predicate.predicateWithLeftExpression(@expression,
- rightExpression:value,
- modifier:NSDirectPredicateModifier,
- type:comparisonType,
- options:0)
+ Comparison.predicateWithLeftExpression(@expression,
+ rightExpression:value,
+ modifier:NSDirectPredicateModifier,
+ type:comparisonType,
+ options:@comparisonOptions)
end
end
end
View
@@ -1,7 +1,7 @@
module MotionData
- describe Predicate::KeyPathExpression do
- extend Predicate::KeyPathExpression::Mixin
+ describe Predicate::ComparableKeyPathExpression do
+ extend Predicate::ComparableKeyPathExpression::Mixin
it "returns a expression for the left-hand side of a comparison" do
key(:property).expression.keyPath.should == 'property'
@@ -50,19 +50,54 @@ module MotionData
predicate = key(:name).endsWith?('bob')
predicate.predicateFormat.should == 'name ENDSWITH "bob"'
end
+
+
+ it "by default returns that no comparison options should be used" do
+ key(:property).comparisonOptions.should == 0
+ end
+
+ it "adds the option to perform a case-insensitive comparison" do
+ expression = key(:name)
+ expression.caseInsensitive.object_id.should == expression.object_id
+ expression.comparisonOptions.should == NSCaseInsensitivePredicateOption
+ end
+
+ it "adds the option to perform a diacritic-insensitive comparison" do
+ expression = key(:name)
+ expression.diacriticInsensitive.object_id.should == expression.object_id
+ expression.comparisonOptions.should == NSDiacriticInsensitivePredicateOption
+ end
+
+ it "adds the option to perform a locale-sensitive comparison" do
+ expression = key(:name)
+ expression.localeSensitive.object_id.should == expression.object_id
+ expression.comparisonOptions.should == NSLocaleSensitivePredicateOption
+ end
+
+ it "combines comparison options" do
+ options = key(:name).caseInsensitive.diacriticInsensitive.localeSensitive.comparisonOptions
+ options.should == NSCaseInsensitivePredicateOption |
+ NSDiacriticInsensitivePredicateOption |
+ NSLocaleSensitivePredicateOption
+ end
+
+ it "adds the accumulated comparison options to the comparison" do
+ predicate = key(:name).caseInsensitive.diacriticInsensitive == 'bob'
+ predicate.predicateFormat.should == 'name ==[cd] "bob"'
+ end
end
describe Predicate do
- extend Predicate::KeyPathExpression::Mixin
+ extend Predicate::ComparableKeyPathExpression::Mixin
it "returns a compound `AND` predicate" do
- predicate = (key(:amount) < 42).and(key(:amount) > 42)
- predicate.predicateFormat.should == 'amount < 42 AND amount > 42'
+ predicate = (key(:amount) < 42).and(key(:amount) > 42).and(key(:amount) != 21)
+ predicate.predicateFormat.should == '(amount < 42 AND amount > 42) AND amount != 21'
end
it "returns a compound `OR` predicate" do
- predicate = (key(:amount) < 42).or(key(:amount) > 42)
- predicate.predicateFormat.should == 'amount < 42 OR amount > 42'
+ predicate = (key(:amount) < 42).or(key(:amount) > 42).or(key(:amount) != 21)
+ predicate.predicateFormat.should == '(amount < 42 OR amount > 42) OR amount != 21'
end
end

0 comments on commit 0989fd3

Please sign in to comment.