class Module

Monkey patch the standard Module class

Public Instance Methods

implements(*args) click to toggle source

Called by a class to indicate it implements the specified interface(s)

@param [AInterface] args One or more interface names that

are being implemented

@return [void]

# File lib/ainterface.rb, line 17
  def implements(*args)
    raise AInterface::Error::MissingArgument if args == []
    # Gather the methods from all specified interfaces
    m = []
    args.each do |module_name|
      raise AInterface::Error::NosuchInterface, module_name.to_s unless
        @@__interface_methods_per_interface[module_name.to_s]
      @@__interface_methods_per_interface[module_name.to_s].each do |method_name|
        m << [module_name.name, method_name]
      end
    end

    # Create a method that checks for the existence
    # of all methods
    t = <<-EOF
      def __check_interface_methods
        #{m}.each do |module_name, method_name|
          next if respond_to? method_name
          raise AInterface::Error::NotImplementedError.new(
                self.class.name,
                method_name,
                module_name
              )
        end
      end
      private :__check_interface_methods
    EOF
    class_eval t unless disabled_interface?

    # Redefine the new method
    t = <<-EOF
      def self.new(*args, &block)
        obj = self.allocate
        obj.send :initialize, *args, &block
        obj.send :__check_interface_methods
        obj
      end
    EOF
    class_eval t unless disabled_interface?

    args.each do |module_name|
      prepend module_name
    end
  end
must_implement(*args) click to toggle source

Errors raised here identify the class_name that is not implementing the method required by the Interface.

Specifies that an method for an interface must be implemented

@param [Symbol] args One ore more methods that need implementation

@return [void]

# File lib/ainterface.rb, line 73
def must_implement(*args)
  raise AInterface::Error::MissingArgument if args == []
  @@__interface_methods_per_interface[self.name] ||= []
  args.each do |method_name|
    @@__interface_methods_per_interface[self.name] << method_name
  end
end

Private Instance Methods

disabled_interface?() click to toggle source

Checks if the Interface error message must be surpressed

@return [Boolean] Return true if error messages should be

surpressed.
# File lib/ainterface.rb, line 88
def disabled_interface?
  ENV['DISABLE_RUBY_INTERFACE'] == '1'
end