Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. class Protocol
  2. @@required_methods = []
  3.  
  4. def self.required_method(method_name)
  5. @@required_methods << method_name
  6. end
  7.  
  8. def self.required_methods
  9. return @@required_methods
  10. end
  11. end
  12.  
  13. class SwiftClass
  14. @@protocols = []
  15.  
  16. def self.inherit_protocol(protocol_name)
  17. protocol = Object.const_get(protocol_name)
  18. @@protocols << protocol
  19. end
  20.  
  21. def self.inherit_protocols(*protocol_names)
  22. protocol_names.each do |protocol_name|
  23. inherit_protocol(protocol_name)
  24. end
  25. end
  26.  
  27. def self.verify_conforms_to_protocols
  28. @@protocols.each do |protocol|
  29. if protocol.superclass != Protocol
  30. raise "ERROR: #{protocol} must be a subclass of Protocol"
  31. end
  32. if (missing_methods = self.missing_methods_for protocol).length > 0
  33. raise "nERROR: #{self} does not conform to protocol #{protocol}. nMISSING METHODS: #{missing_methods.join(", ")}"
  34. end
  35. end
  36. end
  37.  
  38. def self.missing_methods_for(protocol)
  39. protocol.required_methods.select do |required_method|
  40. !self.method_defined? required_method
  41. end
  42. end
  43.  
  44. end
  45.  
  46. class SampleProtocol < Protocol
  47. required_method :number_of_sections_in
  48. required_method :number_of_rows_in_section
  49. end
  50.  
  51. class SampleClass < SwiftClass
  52. inherit_protocols :SampleProtocol
  53. verify_conforms_to_protocols
  54. end
  55.  
  56. ERROR: SampleClass does not conform to protocol SampleProtocol.
  57. MISSING METHODS: number_of_sections_in, number_of_rows_in_section
  58.  
  59. class SampleClass < SwiftClass
  60. inherit_protocols :SampleProtocol
  61.  
  62. def number_of_sections_in; end
  63. def number_of_rows_in_section; end
  64.  
  65. verify_conforms_to_protocols
  66. end
  67.  
  68. class SomeOtherProtocol < Protocol
  69. required_method :cell_for_row_at
  70. end
  71.  
  72. ERROR: SampleClass does not conform to protocol SampleProtocol.
  73. MISSING METHODS: cell_for_row_at
Add Comment
Please, Sign In to add comment