Sha256: ca364e2c1337c174751da8435c741433e49ca8e08b75a794939c7220b5e3b895

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module CDQ
  class CDQPartialPredicate < CDQObject

    attr_reader :key, :scope, :operation

    OPERATORS = {
      :eq => [NSEqualToPredicateOperatorType, :equal],
      :ne => [NSNotEqualToPredicateOperatorType, :not_equal],
      :lt => [NSLessThanPredicateOperatorType, :less_than],
      :le => [NSLessThanOrEqualToPredicateOperatorType, :less_than_or_equal],
      :gt => [NSGreaterThanPredicateOperatorType, :greater_than],
      :ge => [NSGreaterThanOrEqualToPredicateOperatorType, :greater_than_or_equal],
      :contains => [NSContainsPredicateOperatorType, :include],
      :matches => [NSMatchesPredicateOperatorType],
      :in => [NSInPredicateOperatorType],
      :begins_with => [NSBeginsWithPredicateOperatorType],
      :ends_with => [NSEndsWithPredicateOperatorType]
    }
      
    def initialize(key, scope, operation = :and)
      @key = key
      @scope = scope
      @operation = operation
    end

    OPERATORS.each do |op, (type, synonym)|
      define_method(op) do |value, options = 0| 
        make_scope(type, value, options)
      end
      alias_method synonym, op if synonym
    end

    def between(min, max);   make_scope(NSBetweenPredicateOperatorType, [min, max]); end
      
    private

    def make_pred(key, type, value, options = 0)
      NSComparisonPredicate.predicateWithLeftExpression(
        NSExpression.expressionForKeyPath(key.to_s),
        rightExpression:NSExpression.expressionForConstantValue(value),
        modifier:NSDirectPredicateModifier,
        type:type,
        options:options)
    end

    def make_scope(type, value, options = 0)
      scope.send(operation, make_pred(key, type, value, options), key)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cdq-0.1.1 motion/cdq/partial_predicate.rb