module Interface

Constants

UnknownInterface
VERSION

Public Class Methods

included(base) click to toggle source
# File lib/interface.rb, line 27
def self.included(base)
  base.extend ClassMethods
end
new(target) click to toggle source
# File lib/interface.rb, line 69
def initialize(target)
  @target = target
end

Public Instance Methods

as(interface) click to toggle source
Calls superclass method
# File lib/interface.rb, line 65
def as(interface)
  raise UnknownInterface.new(interface) unless self.class.interfaces.include?(interface)
  cached_interface_class(interface) do
    Class.new do
      def initialize(target)
        @target = target
      end

      interface.instance_methods.select do |method|
        interface.instance_method(method.to_sym).owner == interface
      end.each do |method|
        define_method(method) do |*args, &block|
          @target.public_send(method, *args, &block)
        end
      end

      [:is_a?, :kind_of?, :instance_of?].each do |type_method|
        define_method(type_method) do |target, &block|
          return true if target == interface
          super
        end
      end

      define_singleton_method :name do
        interface.name
      end

      define_singleton_method :inspect do
        name
      end

      define_method(:inspect) do
        "#<#{interface.name}:#{self.object_id}>"
      end
    end
  end.new(self)
end
cached_interface_class(interface) { || ... } click to toggle source
# File lib/interface.rb, line 56
def cached_interface_class(interface)
  interface.instance_variable_get("@__cached_class__") || begin
    klass = yield
    interface.instance_variable_set("@__cached_class__", klass)
    klass
  end
end