Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Protocol
- @@required_methods = []
- def self.required_method(method_name)
- @@required_methods << method_name
- end
- def self.required_methods
- return @@required_methods
- end
- end
- class SwiftClass
- @@protocols = []
- def self.inherit_protocol(protocol_name)
- protocol = Object.const_get(protocol_name)
- @@protocols << protocol
- end
- def self.inherit_protocols(*protocol_names)
- protocol_names.each do |protocol_name|
- inherit_protocol(protocol_name)
- end
- end
- def self.verify_conforms_to_protocols
- @@protocols.each do |protocol|
- if protocol.superclass != Protocol
- raise "ERROR: #{protocol} must be a subclass of Protocol"
- end
- if (missing_methods = self.missing_methods_for protocol).length > 0
- raise "nERROR: #{self} does not conform to protocol #{protocol}. nMISSING METHODS: #{missing_methods.join(", ")}"
- end
- end
- end
- def self.missing_methods_for(protocol)
- protocol.required_methods.select do |required_method|
- !self.method_defined? required_method
- end
- end
- end
- class SampleProtocol < Protocol
- required_method :number_of_sections_in
- required_method :number_of_rows_in_section
- end
- class SampleClass < SwiftClass
- inherit_protocols :SampleProtocol
- verify_conforms_to_protocols
- end
- ERROR: SampleClass does not conform to protocol SampleProtocol.
- MISSING METHODS: number_of_sections_in, number_of_rows_in_section
- class SampleClass < SwiftClass
- inherit_protocols :SampleProtocol
- def number_of_sections_in; end
- def number_of_rows_in_section; end
- verify_conforms_to_protocols
- end
- class SomeOtherProtocol < Protocol
- required_method :cell_for_row_at
- end
- ERROR: SampleClass does not conform to protocol SampleProtocol.
- MISSING METHODS: cell_for_row_at
Add Comment
Please, Sign In to add comment