Skip to content

Commit

Permalink
Add pattern operator on Preference instance
Browse files Browse the repository at this point in the history
to be able to use switch on Equatable or object comparable in Range
  • Loading branch information
phimage committed Jun 16, 2015
1 parent ead0eea commit 815b160
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Prephirences.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|

# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.name = "Prephirences"
s.version = "1.2.0"
s.version = "1.2.1"
s.summary = "A Swift library to manage preferences"
s.description = <<-DESC
Prephirences is a Swift library that provides useful protocols and methods to manage preferences.
Expand All @@ -21,7 +21,7 @@ Pod::Spec.new do |s|
s.osx.deployment_target = "10.9"

# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.source = { :git => "https://github.com/phimage/Prephirences.git", :tag => '1.2.0' }
s.source = { :git => "https://github.com/phimage/Prephirences.git", :tag => s.version }

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

Expand Down
26 changes: 23 additions & 3 deletions Prephirences/Preference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,36 @@ public func ?=<T> (preference: MutablePreference<T>, @autoclosure expr: () -> T)
}
}

// MARK: Pattern match
public func ~=<T: Equatable> (value: T, preference: Preference<T>) -> Bool {
if let pv = preference.value {
return pv ~= value
}
return false
}
public func ~=<I: IntervalType> (value: I.Bound, preference: Preference<I>) -> Bool {
if let pv = preference.value {
return pv ~= value
}
return false
}
public func ~=<I: ForwardIndexType where I : Comparable> (value: Range<I>, preference: Preference<I>) -> Bool {
if let pv = preference.value {
return value ~= pv
}
return false
}

// MARK: Equatable
func ==<T where T: Equatable> (left: Preference<T>, right: Preference<T>) -> Bool {
public func ==<T: Equatable> (left: Preference<T>, right: Preference<T>) -> Bool {
return left.value == right.value
}
func !=<T where T: Equatable> (left: Preference<T>, right: Preference<T>) -> Bool {
public func !=<T: Equatable> (left: Preference<T>, right: Preference<T>) -> Bool {
return !(left == right)
}

// MARK: Comparable
func < <T where T: Comparable> (left: Preference<T>, right: Preference<T>) -> Bool {
public func < <T: Comparable> (left: Preference<T>, right: Preference<T>) -> Bool {
return left.value < right.value
}

Expand Down
27 changes: 27 additions & 0 deletions PrephirencesiOSTests/PrephirencesiOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ class PrephirencesiOSTests: XCTestCase {
intPref /= 3
XCTAssert(intPref.value! == 10)

switch(intPref) {
case 1: XCTFail("not equal in switch")
case 10: println("ok")
default: XCTFail("not equal in switch")
}

switch(intPref) {
case 0...9: XCTFail("not equal in switch")
case 11...999: XCTFail("not equal in switch")
case 9...11: println("ok")
default: XCTFail("not equal in switch")
}


var boolPref: MutablePreference<Bool> = Prephirences.preferenceForKey("bool", userDefaults)
boolPref.value = nil
Expand Down Expand Up @@ -190,6 +203,20 @@ class PrephirencesiOSTests: XCTestCase {
XCTAssert(boolPref.value! == false)
boolPref ||= true
XCTAssert(boolPref.value! == true)

switch(boolPref) {
case true: println("ok")
case false: XCTFail("not true")
default: XCTFail("nil")
}

var stringPref: MutablePreference<String> = Prephirences.preferenceForKey("string", userDefaults)
stringPref.value = "pref"

stringPref += "erence"
XCTAssert(stringPref.value! == "preference")


}

}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ intPref *= 20
intPref %= 7
intPref /= 3

switch(intPref) {
case 1: println("one")
case 2...10: println("not one or zero but...")
default: println("unkwown")
}

var boolPref: MutablePreference<Bool> = Prephirences.preferenceForKey("boolKey", aPrefs)

boolPref &= false
Expand Down

0 comments on commit 815b160

Please sign in to comment.