Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Add overloads for handling of numeric refinements
Browse files Browse the repository at this point in the history
This allows specifying integers, floats or `NSNumber`s without cast, including from Objective-C.
  • Loading branch information
Clément Le Provost committed Dec 13, 2016
1 parent 1aadb5e commit b785931
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
63 changes: 61 additions & 2 deletions Sources/SearchParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,38 @@ import Foundation
/// - parameter value: Value to compare the attribute to (second operand).
/// - parameter inclusive: Whether the filter is inclusive (the default) or exclusive (negated with a `NOT`).
///
@objc public init(_ name: String, _ op: Operator, _ value: NSNumber, inclusive: Bool = true) {
@objc(initWithName:operator:numberValue:inclusive:)
public init(_ name: String, _ op: Operator, _ value: NSNumber, inclusive: Bool = true) {
self.name = name
self.op = op
self.value = value
self.inclusive = inclusive
}

/// Create a numeric refinement with the specified operator and operands.
///
/// - parameter name: Name of the attribute to filter (first operand).
/// - parameter op: Comparison operator to apply.
/// - parameter value: Value to compare the attribute to (second operand).
/// - parameter inclusive: Whether the filter is inclusive (the default) or exclusive (negated with a `NOT`).
///
@objc(initWithName:operator:intValue:inclusive:)
public convenience init(_ name: String, _ op: Operator, _ value: Int, inclusive: Bool = true) {
self.init(name, op, NSNumber(value: value), inclusive: inclusive)
}

/// Create a numeric refinement with the specified operator and operands.
///
/// - parameter name: Name of the attribute to filter (first operand).
/// - parameter op: Comparison operator to apply.
/// - parameter value: Value to compare the attribute to (second operand).
/// - parameter inclusive: Whether the filter is inclusive (the default) or exclusive (negated with a `NOT`).
///
@objc(initWithName:operator:doubleValue:inclusive:)
public convenience init(_ name: String, _ op: Operator, _ value: Double, inclusive: Bool = true) {
self.init(name, op, NSNumber(value: value), inclusive: inclusive)
}

/// Create a copy of a numeric refinement.
///
/// - parameter copy: Numeric refinement to copy.
Expand Down Expand Up @@ -588,10 +613,44 @@ import Foundation
/// - parameter inclusive: Whether the refinement is treated as inclusive (the default) or exclusive
/// (negated with a `NOT`).
///
@objc(addNumericRefinementWithName:op:value:inclusive:)
@objc(addNumericRefinementWithName:op:numberValue:inclusive:)
public func addNumericRefinement(_ name: String, _ op: NumericRefinement.Operator, _ value: NSNumber, inclusive: Bool = true) {
addNumericRefinement(NumericRefinement(name, op, value, inclusive: inclusive))
}

/// Add a refinement for a given numeric.
/// The refinement will be treated as conjunctive (`AND`) or disjunctive (`OR`) based on the numeric's own
/// disjunctive/conjunctive status.
///
/// + Note: This is a convenience shortcut for `addNumericRefinement(_:)`.
///
/// - parameter name: The numeric's name (first operand to the operator).
/// - parameter op: The comparison operator to apply.
/// - parameter value: The value to compare the numeric to (second operand to the operator).
/// - parameter inclusive: Whether the refinement is treated as inclusive (the default) or exclusive
/// (negated with a `NOT`).
///
@objc(addNumericRefinementWithName:op:intValue:inclusive:)
public func addNumericRefinement(_ name: String, _ op: NumericRefinement.Operator, _ value: Int, inclusive: Bool = true) {
addNumericRefinement(NumericRefinement(name, op, value, inclusive: inclusive))
}

/// Add a refinement for a given numeric.
/// The refinement will be treated as conjunctive (`AND`) or disjunctive (`OR`) based on the numeric's own
/// disjunctive/conjunctive status.
///
/// + Note: This is a convenience shortcut for `addNumericRefinement(_:)`.
///
/// - parameter name: The numeric's name (first operand to the operator).
/// - parameter op: The comparison operator to apply.
/// - parameter value: The value to compare the numeric to (second operand to the operator).
/// - parameter inclusive: Whether the refinement is treated as inclusive (the default) or exclusive
/// (negated with a `NOT`).
///
@objc(addNumericRefinementWithName:op:doubleValue:inclusive:)
public func addNumericRefinement(_ name: String, _ op: NumericRefinement.Operator, _ value: Double, inclusive: Bool = true) {
addNumericRefinement(NumericRefinement(name, op, value, inclusive: inclusive))
}

/// Add a refinement for a given numeric.
/// The refinement will be treated as conjunctive (`AND`) or disjunctive (`OR`) based on the numeric's own
Expand Down
4 changes: 3 additions & 1 deletion Tests/ObjcBridgingTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ - (void)testSearchParameters {

[queryFilters setNumericWithName:@"name" disjunctive:YES];
[queryFilters isDisjunctiveNumericWithName:@"name"];
[queryFilters addNumericRefinementWithName:@"name" op:OperatorLessThan value:@3 inclusive:YES];
[queryFilters addNumericRefinementWithName:@"name" op:OperatorLessThan numberValue:@3 inclusive:YES];
[queryFilters addNumericRefinementWithName:@"name" op:OperatorLessThan intValue:3 inclusive:YES];
[queryFilters addNumericRefinementWithName:@"name" op:OperatorLessThan doubleValue:3.0 inclusive:YES];
[queryFilters removeNumericRefinementWithName:@"name" op:OperatorGreaterThanOrEqual value:@123.456 inclusive:NO];
[queryFilters hasNumericRefinementsWithName:@"name"];
[queryFilters toggleFacetRefinementWithName:@"name" value:@"value"];
Expand Down

0 comments on commit b785931

Please sign in to comment.