module Interface

Constants

DoesNotImplementError
IncorrectArgumentsError
IncorrectReturnType

Public Class Methods

for_messages(&block) click to toggle source
# File lib/interface_semantics.rb, line 18
  def self.for_messages(&block)
    attributes = DSL.run(&block)

    Module.new do
      attributes_string = attributes.map { |a| "Attribute.new(:#{a.name}, #{a.argument_types}, #{a.return_type})" }.join(",")
      init = <<-RUBY
        def initialize(*)
          @__attributes = [#{attributes_string}]
          super
          after_initialize
        end
      RUBY

      attributes.each do |a|
        argument_checks = a.argument_types.each_with_index.map do |at, i|
          "raise IncorrectArgumentsError unless args[#{i}].class == #{at.name};"
        end.join(" ")

        method_wrapper = "def #{a.name}(*args); #{argument_checks} super.tap do |return_val| raise IncorrectReturnType unless return_val.class == #{a.return_type}; end; end"

        module_eval(method_wrapper)
      end

      module_eval(init)

      module_eval("def run_checks; " + attributes.map { |a| "raise DoesNotImplementError, :#{a.name} if self.method(:#{a.name}).super_method.nil?;" }.join("") + "end")

      def after_initialize
        run_checks
      end
    end
  end

Public Instance Methods

after_initialize() click to toggle source
# File lib/interface_semantics.rb, line 45
def after_initialize
  run_checks
end