Guest User

Untitled

a guest
Dec 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. //: # Feature Flag example
  2.  
  3. //: ## Implementation
  4.  
  5. import Foundation
  6.  
  7. enum Feature: String {
  8. case Login, AdminTool
  9.  
  10. var key: String {
  11. return "FEATURE_FLAG_\(rawValue.uppercased())"
  12. }
  13.  
  14. var isEnabled: Bool {
  15. guard let flag = ProcessInfo().environment[key] else { return false }
  16. return flag == "on"
  17. }
  18.  
  19. func toggledOn(block: ()->()) {
  20. if isEnabled {
  21. block()
  22. }
  23. }
  24.  
  25. func toggledOff(block: ()->()) {
  26. if !isEnabled {
  27. block()
  28. }
  29. }
  30. }
  31.  
  32. //: ## Usage Example
  33.  
  34. Feature.Login.toggledOn {
  35. print("do this if feature is on")
  36. }
  37.  
  38. Feature.Login.toggledOff {
  39. print("do this if feature is off")
  40. }
Add Comment
Please, Sign In to add comment