Sha256: 0b22cdcd05fca26c12a7f9685c1df84fcbb59f640787cd0f4015ebd9fe0ff7a9

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

module Interface
    Interface::VERSION = "0.1.0"
    class MethodMissing < RuntimeError; end
 
    def extends(o)
        extend o
    end
 
    def extend_object(o)
        return append_features(o) if Interface === o
        append_features(class << o; self end)
        included(o)
    end
 
    def append_features(mod)
        return super if Interface === mod
        inherited = (self.ancestors-[self]).select{|x| Interface === x}.
            map{|x| x.instance_variable_get("@ids")}
        ids = @ids + inherited.flatten
        @unreq ||= []
        (ids-@unreq).uniq.each do |id|
            unless mod.instance_methods( true ).include?( id.to_s )
                raise Interface::MethodMissing, id.to_s
            end
        end
        super mod
    end
    
    def required_methods(*ids)
        @ids = ids
    end
 
    def unrequired_methods(*ids)
        @unreq ||= []
        @unreq += ids
    end
end
 
def interface(&block)
    mod = Module.new
    mod.extend Interface
    mod.instance_eval(&block)
    mod
end
 
class Module
    def implements m
        include m
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat-dev/interface_work/SCRAP/j-interface/interface-0.1.0/lib/interface.rb