Created
April 12, 2016 13:51
-
-
Save blinker13/17a8f4f461ae04c904beffaec9dd3233 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Urn { | |
public enum Namespace : String { | |
case SoundCloud = "soundcloud" | |
} | |
public enum Collection : String { | |
case Playlists = "playlists" | |
case Tracks = "tracks" | |
case Users = "users" | |
case TrackStations = "track-stations" | |
} | |
// MARK: - | |
public let namespace:Namespace | |
public let collection:Collection | |
public let identifier:UInt | |
// MARK: - | |
public init(namespace:Namespace = .SoundCloud, collection:Collection, identifier:UInt) { | |
self.namespace = namespace | |
self.collection = collection | |
self.identifier = identifier | |
} | |
public init(string:String) { | |
let components = string.characters.split(":", maxSplit:3).map(String.init) | |
self.namespace = Namespace(rawValue:components[0])! | |
self.collection = Collection(rawValue:components[1])! | |
self.identifier = UInt(components[2])! | |
} | |
} | |
// MARK: - Hashable | |
extension Urn : Hashable { | |
public var hashValue:Int { | |
return description.hashValue | |
} | |
} | |
public func == (lhs:Urn, rhs:Urn) -> Bool { | |
return lhs.hashValue == rhs.hashValue | |
} | |
// MARK: - CustomStringConvertible | |
extension Urn.Collection : CustomStringConvertible { | |
public var description:String { | |
return self.rawValue | |
} | |
} | |
extension Urn.Namespace : CustomStringConvertible { | |
public var description:String { | |
return self.rawValue | |
} | |
} | |
extension Urn : CustomStringConvertible { | |
public var description:String { | |
return "<Urn \(namespace):\(collection):\(identifier)>" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment