Guest User

Untitled

a guest
May 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. extension Person {
  2.  
  3. public fun waldo() -> Waldo { return _Waldo(this: self) }
  4.  
  5. class _Waldo: Waldo {
  6.  
  7. var this:Person
  8. private static var type_map: [String:Any.Type] = [
  9. "name": String.self,
  10. "birthday": Date.self,
  11. "age": Int.self,
  12. ]
  13.  
  14. init (this: Person) {
  15. self.this = this
  16. }
  17.  
  18. public func type (for key: String) -> Any.Type? {
  19. return _Waldo.type_map[key]
  20. }
  21.  
  22. public func get<T> (for key: String, default value: T? = nil) -> T? {
  23. switch key {
  24. case "name": return this.name as? T
  25. case "birthday": return this.birthday as? T
  26. case "age": return this.age as? T
  27. default:
  28. return value
  29. }
  30. }
  31.  
  32. public func set<T> (_ key: String, to value: T? = nil) {
  33. switch key {
  34. case "name":
  35. if this.name is T,
  36. let tv = value as? String {
  37. this.name = tv
  38. }
  39. case "birthday":
  40. if this.birthday is T,
  41. let tv = value as? Date {
  42. this.birthday = tv
  43. }
  44. default:
  45. return
  46. }
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment