Skip to content
This repository has been archived by the owner on Nov 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #21 from samodom/develop
Browse files Browse the repository at this point in the history
v0.2.12: Updates to object association
  • Loading branch information
samodom authored Feb 14, 2017
2 parents 43eae08 + dc9664c commit 8768848
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 352 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
FoundationSwagger Changelog
===========================

### v0.2.12 (February 13, 2017)
- Removed default-value typed association accessors
- Added class-level typed association accessors

### v0.2.11 (February 13, 2017)
- Adding type-level object associations

### v0.2.10
- Extended file permissions support

### v0.2.9
- Added file permissions types

### v0.2.8 (January 24, 2017)
- Exposed `MethodType` enumerated type

Expand Down
83 changes: 53 additions & 30 deletions Docs/ObjectAssociation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Object Association

## Protocol

In order to use instance and class methods instead of free functions for object association, the `AssociatingObject` and `AssociatingClass` protocols provide a simple interface. Simply extend your class to declare conformance to on or both of the protocols. Keys are unsafe raw pointers to anything.
In order to use instance and class methods instead of free functions for object association, the `AssociatingObject` and `AssociatingClass` protocols provide a simple interface. Simply extend your class to declare conformance to one or both protocol(s). Keys are unsafe raw pointers to anything.

```swift
typealias ObjectAssociationKey = UnsafeRawPointer
Expand Down Expand Up @@ -75,35 +75,58 @@ myClass.removeAssociation(for: MyKey)

## Type-specific accessors

For convenience several common atomic data types have pairs of accessors for retrieving associations: one providing purely optional associations and one for retrieving assured values by providing default value associations.
For convenience, several common atomic data types have a convenience accessor for retrieving associations in a typed manner.

```swift
/// For boolean associations
booleanAssociation(for: ObjectAssociationKey) -> Bool?
booleanAssociation(for: ObjectAssociationKey, defaultValue: Bool) -> Bool

/// For signed integer associations
integerAssociation(for: ObjectAssociationKey) -> Int?
integerAssociation(for: ObjectAssociationKey, defaultValue: Int) -> Int

/// For unsigned integer associations
unsignedIntegerAssociation(for: ObjectAssociationKey) -> UInt?
unsignedIntegerAssociation(for:
ObjectAssociationKey, defaultValue: UInt) -> UInt

/// For floating-point associations
floatAssociation(for: ObjectAssociationKey) -> Float?
floatAssociation(for: ObjectAssociationKey, defaultValue: Float) -> Float

/// For double-precision floating-point associations
doubleAssociation(for: ObjectAssociationKey) -> Double?
doubleAssociation(for: ObjectAssociationKey, defaultValue: Double) -> Double

/// For string associations
stringAssociation(for: ObjectAssociationKey) -> String?
stringAssociation(for: ObjectAssociationKey, defaultValue: String) -> String

/// For URL associations
urlAssociation(for: ObjectAssociationKey) -> URL?
urlAssociation(for: ObjectAssociationKey, defaultValue: URL) -> URL
extension AssociatingObject {

/// For boolean associations
func booleanAssociation(for: ObjectAssociationKey) -> Bool?

/// For signed integer associations
func integerAssociation(for: ObjectAssociationKey) -> Int?

/// For unsigned integer associations
func unsignedIntegerAssociation(for: ObjectAssociationKey) -> UInt?

/// For floating-point associations
func floatAssociation(for: ObjectAssociationKey) -> Float?

/// For double-precision floating-point associations
func doubleAssociation(for: ObjectAssociationKey) -> Double?

/// For string associations
func stringAssociation(for: ObjectAssociationKey) -> String?

/// For URL associations
func urlAssociation(for: ObjectAssociationKey) -> URL?

}
```

```swift
extension AssociatingClass {

/// For boolean associations
static func booleanAssociation(for: ObjectAssociationKey) -> Bool?

/// For signed integer associations
static func integerAssociation(for: ObjectAssociationKey) -> Int?

/// For unsigned integer associations
static func unsignedIntegerAssociation(for: ObjectAssociationKey) -> UInt?

/// For floating-point associations
static func floatAssociation(for: ObjectAssociationKey) -> Float?

/// For double-precision floating-point associations
static func doubleAssociation(for: ObjectAssociationKey) -> Double?

/// For string associations
static func stringAssociation(for: ObjectAssociationKey) -> String?

/// For URL associations
static func urlAssociation(for: ObjectAssociationKey) -> URL?

}
```
16 changes: 12 additions & 4 deletions FoundationSwagger.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
0A3784D11A100B1D0003C7F3 /* FileExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A3784D01A100B1D0003C7F3 /* FileExtensionsTests.swift */; };
0A3784D41A100D3A0003C7F3 /* FileExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A3784D31A100D3A0003C7F3 /* FileExtensions.swift */; };
0A3BB9C019EB78010075B32A /* FoundationSwagger.h in Headers */ = {isa = PBXBuildFile; fileRef = 0A3BB9BF19EB78010075B32A /* FoundationSwagger.h */; settings = {ATTRIBUTES = (Public, ); }; };
0A4261F71E527C390029F70D /* ObjectAssociationConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A4261F61E527C390029F70D /* ObjectAssociationConstants.swift */; };
0A563D631E4BFC4500D7C5E1 /* FilePermissionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A563D621E4BFC4500D7C5E1 /* FilePermissionsTests.swift */; };
0A563D651E4BFFCE00D7C5E1 /* FilePermissions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A563D641E4BFFCE00D7C5E1 /* FilePermissions.swift */; };
0A563D671E4C233200D7C5E1 /* ClassPermissions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A563D661E4C233200D7C5E1 /* ClassPermissions.swift */; };
Expand All @@ -32,9 +33,10 @@
0A7DC4831DBEE68300BCFC56 /* MethodSwizzling.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A7DC4821DBEE68300BCFC56 /* MethodSwizzling.swift */; };
0A90C86E1A86D2E70047FE16 /* AssociatingObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A90C86D1A86D2E70047FE16 /* AssociatingObject.swift */; };
0A90C8711A86D3120047FE16 /* ObjectAssociationTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A90C8701A86D3120047FE16 /* ObjectAssociationTestCase.swift */; };
0AA456F91E5280320067783B /* AssociatingClassTypedAccessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AA456F81E5280320067783B /* AssociatingClassTypedAccessorTests.swift */; };
0AB47CBE1DC537DE00B5C86A /* SampleType.h in Headers */ = {isa = PBXBuildFile; fileRef = 0AB47CBD1DC537DE00B5C86A /* SampleType.h */; settings = {ATTRIBUTES = (Public, ); }; };
0ABBD3E21E1B33C700F6F194 /* MethodSwizzlingTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A048B861E134FF300CF003F /* MethodSwizzlingTestCase.swift */; };
0ABD48CD1DE2C17B005DD470 /* TypedObjectAssociationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ABD48CC1DE2C17B005DD470 /* TypedObjectAssociationTests.swift */; };
0ABD48CD1DE2C17B005DD470 /* AssociatingObjectTypedAccessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ABD48CC1DE2C17B005DD470 /* AssociatingObjectTypedAccessorTests.swift */; };
0ABD48CF1DE3B07C005DD470 /* TypedAssociationAccessors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ABD48CE1DE3B07C005DD470 /* TypedAssociationAccessors.swift */; };
0ABFB6291DC3180700B50A55 /* CodeSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0ABFB6281DC3180700B50A55 /* CodeSource.swift */; };
0AEDD6A61DC52E3E005D042C /* SampleTypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 0AEDD6A41DC52E3E005D042C /* SampleTypes.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -81,6 +83,7 @@
0A3BB9BF19EB78010075B32A /* FoundationSwagger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FoundationSwagger.h; sourceTree = "<group>"; };
0A3BB9C519EB78010075B32A /* FoundationSwaggerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FoundationSwaggerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
0A3BB9CB19EB78010075B32A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
0A4261F61E527C390029F70D /* ObjectAssociationConstants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectAssociationConstants.swift; sourceTree = "<group>"; };
0A563D621E4BFC4500D7C5E1 /* FilePermissionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FilePermissionsTests.swift; sourceTree = "<group>"; };
0A563D641E4BFFCE00D7C5E1 /* FilePermissions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FilePermissions.swift; sourceTree = "<group>"; };
0A563D661E4C233200D7C5E1 /* ClassPermissions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClassPermissions.swift; sourceTree = "<group>"; };
Expand All @@ -94,8 +97,9 @@
0A7DC4821DBEE68300BCFC56 /* MethodSwizzling.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MethodSwizzling.swift; sourceTree = "<group>"; };
0A90C86D1A86D2E70047FE16 /* AssociatingObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociatingObject.swift; sourceTree = "<group>"; };
0A90C8701A86D3120047FE16 /* ObjectAssociationTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ObjectAssociationTestCase.swift; sourceTree = "<group>"; };
0AA456F81E5280320067783B /* AssociatingClassTypedAccessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociatingClassTypedAccessorTests.swift; sourceTree = "<group>"; };
0AB47CBD1DC537DE00B5C86A /* SampleType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SampleType.h; sourceTree = "<group>"; };
0ABD48CC1DE2C17B005DD470 /* TypedObjectAssociationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypedObjectAssociationTests.swift; sourceTree = "<group>"; };
0ABD48CC1DE2C17B005DD470 /* AssociatingObjectTypedAccessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AssociatingObjectTypedAccessorTests.swift; sourceTree = "<group>"; };
0ABD48CE1DE3B07C005DD470 /* TypedAssociationAccessors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypedAssociationAccessors.swift; sourceTree = "<group>"; };
0ABFB6281DC3180700B50A55 /* CodeSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeSource.swift; sourceTree = "<group>"; };
0ABFD8841DAF28070029F590 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -210,7 +214,9 @@
isa = PBXGroup;
children = (
0A114AD11E52651800B907ED /* AssociatingClassTests.swift */,
0AA456F81E5280320067783B /* AssociatingClassTypedAccessorTests.swift */,
0A114ACF1E52642400B907ED /* AssociatingObjectTests.swift */,
0ABD48CC1DE2C17B005DD470 /* AssociatingObjectTypedAccessorTests.swift */,
0A1161C71E4D16B600CD6DD8 /* ClassPermissionsTests.swift */,
0A5BCC031AACDDA000484777 /* DateRangeTests.swift */,
0A5BCBFB1AACB3B200484777 /* DateTests.swift */,
Expand All @@ -222,7 +228,6 @@
0A90C8701A86D3120047FE16 /* ObjectAssociationTestCase.swift */,
0A048B881E1B2C3800CF003F /* ObjectiveCMethodSwizzlingTests.swift */,
0A7DC47A1DBED0E100BCFC56 /* SwiftMethodSwizzlingTests.swift */,
0ABD48CC1DE2C17B005DD470 /* TypedObjectAssociationTests.swift */,
);
path = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -271,6 +276,7 @@
0A1161CB1E4D59F400CD6DD8 /* FilePermissionsConstants.swift */,
0AF8C3DA1DD0207200DEDF1C /* MethodSwizzlingAssertions.swift */,
0AEDD6B41DC52FE5005D042C /* MethodSwizzlingHelpers.swift */,
0A4261F61E527C390029F70D /* ObjectAssociationConstants.swift */,
);
path = Tools;
sourceTree = "<group>";
Expand Down Expand Up @@ -480,15 +486,17 @@
0ABFB6291DC3180700B50A55 /* CodeSource.swift in Sources */,
0ABBD3E21E1B33C700F6F194 /* MethodSwizzlingTestCase.swift in Sources */,
0A114AD01E52642400B907ED /* AssociatingObjectTests.swift in Sources */,
0AA456F91E5280320067783B /* AssociatingClassTypedAccessorTests.swift in Sources */,
0A7DC47B1DBED0E100BCFC56 /* SwiftMethodSwizzlingTests.swift in Sources */,
0A1161C81E4D16B600CD6DD8 /* ClassPermissionsTests.swift in Sources */,
0A1161CC1E4D59F400CD6DD8 /* FilePermissionsConstants.swift in Sources */,
0A5BCC041AACDDA000484777 /* DateRangeTests.swift in Sources */,
0ABD48CD1DE2C17B005DD470 /* TypedObjectAssociationTests.swift in Sources */,
0ABD48CD1DE2C17B005DD470 /* AssociatingObjectTypedAccessorTests.swift in Sources */,
0AEDD6B51DC52FE5005D042C /* MethodSwizzlingHelpers.swift in Sources */,
0A1161D01E4E576000CD6DD8 /* FileManagerStubs.swift in Sources */,
0A114AD21E52651800B907ED /* AssociatingClassTests.swift in Sources */,
0A90C8711A86D3120047FE16 /* ObjectAssociationTestCase.swift in Sources */,
0A4261F71E527C390029F70D /* ObjectAssociationConstants.swift in Sources */,
0A563D631E4BFC4500D7C5E1 /* FilePermissionsTests.swift in Sources */,
0A5BCBFC1AACB3B200484777 /* DateTests.swift in Sources */,
0A7DC47F1DBED91A00BCFC56 /* MethodSurrogateTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
BlueprintName = "FoundationSwaggerTests"
ReferencedContainer = "container:FoundationSwagger.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "ObjectAssociationTestCase">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
<MacroExpansion>
Expand Down
63 changes: 1 addition & 62 deletions FoundationSwagger/Object/TypedAssociationAccessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,29 @@

public extension AssociatingObject {

// MARK: - Booleans

/// Typed method for associated boolean values.
/// - parameter for: The key for the association to retrieve.
/// - returns: The associated boolean value or `nil` if no such boolean association exists.
public func booleanAssociation(for key: ObjectAssociationKey) -> Bool? {
return association(for: key) as? Bool
}

/// Typed method for associated boolean values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: Boolean value to use when the boolean association does not exist.
/// - returns: The associated boolean value or `defaultValue` if no such boolean association exists.
public func booleanAssociation(for key: ObjectAssociationKey, defaultValue: Bool) -> Bool {
return booleanAssociation(for: key) ?? defaultValue
}


// MARK: - Signed integers

/// Typed method for associated signed integer values.
/// - parameter for: The key for the association to retrieve.
/// - returns: The associated signed integer value or `nil` if no such signed integer association exists.
public func integerAssociation(for key: ObjectAssociationKey) -> Int? {
return association(for: key) as? Int
}

/// Typed method for associated signed integer values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: Signed integer value to use when the signed integer association does not exist.
/// - returns: The associated signed integer value or `defaultValue` if no such signed integer association exists.
public func integerAssociation(for key: ObjectAssociationKey, defaultValue: Int) -> Int {
return integerAssociation(for: key) ?? defaultValue
}


// MARK: - Unsigned integers

/// Typed method for associated unsigned integer values.
/// - parameter for: The key for the association to retrieve.
/// - returns: The associated unsigned integer value or `nil` if no such unsigned integer association exists.
public func unsignedIntegerAssociation(for key: ObjectAssociationKey) -> UInt? {
return association(for: key) as? UInt
}

/// Typed method for associated unsigned integer values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: Unsigned integer value to use when the unsigned integer association does not exist.
/// - returns: The associated unsigned integer value or `defaultValue` if no such unsigned integer association exists.
public func unsignedIntegerAssociation(for key: ObjectAssociationKey, defaultValue: UInt) -> UInt {
return unsignedIntegerAssociation(for: key) ?? defaultValue
}


// MARK: - Floats

/// Typed method for associated float values.
/// - parameter for: The key for the association to retrieve.
Expand All @@ -72,16 +40,6 @@ public extension AssociatingObject {
return association(for: key) as? Float
}

/// Typed method for associated float values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: Float value to use when the float association does not exist.
/// - returns: The associated float value or `defaultValue` if no such float association exists.
public func floatAssociation(for key: ObjectAssociationKey, defaultValue: Float) -> Float {
return floatAssociation(for: key) ?? defaultValue
}


// MARK: - Doubles

/// Typed method for associated double values.
/// - parameter for: The key for the association to retrieve.
Expand All @@ -90,16 +48,6 @@ public extension AssociatingObject {
return association(for: key) as? Double
}

/// Typed method for associated double values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: Double value to use when the double association does not exist.
/// - returns: The associated double value or `defaultValue` if no such double association exists.
public func doubleAssociation(for key: ObjectAssociationKey, defaultValue: Double) -> Double {
return doubleAssociation(for: key) ?? defaultValue
}


// MARK: - Strings

/// Typed method for associated string values.
/// - parameter for: The key for the association to retrieve.
Expand All @@ -108,16 +56,6 @@ public extension AssociatingObject {
return association(for: key) as? String
}

/// Typed method for associated string values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: String value to use when the string association does not exist.
/// - returns: The associated string value or `defaultValue` if no such string association exists.
public func stringAssociation(for key: ObjectAssociationKey, defaultValue: String) -> String {
return stringAssociation(for: key) ?? defaultValue
}


// MARK: - URLs

/// Typed method for associated URL values.
/// - parameter for: The key for the association to retrieve.
Expand All @@ -126,6 +64,7 @@ public extension AssociatingObject {
return association(for: key) as? URL
}


/// Typed method for associated URL values with a default value to use.
/// - parameter for: The key for the association to retrieve.
/// - parameter defaultValue: URL value to use when the URL association does not exist.
Expand Down
Loading

0 comments on commit 8768848

Please sign in to comment.