Sha256: 28560aa872a1adc6ca70b027cc6a68c87df5f0bc82619b654bfbc285e4f70510

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

# 
# Fix for ruby's broken include.
# Included modules doesn't propagated to it's children.
# 
# Test case:
# module A; end
# module B
#   include A
# end
# 
# module Plugin; end
# A.send(:include, Plugin)
# 
# p "Ancestors of A: " + A.ancestors.join(', ') # => "Ancestors of A: A, Plugin"
# p "Ancestors of B: " + B.ancestors.join(', ') # => "Ancestors of B: B, A" << NO PLUGIN!
# 
class Module
  def directly_included_by
    @directly_included_by ||= Set.new
  end
  
  def fixed_include mod
    unless mod.directly_included_by.include? self
      mod.directly_included_by.add self
    end

    include mod
    directly_included_by.each do |child|
      child.fixed_include self
    end
  end
end


# 
# Inheritance
# 
class Module
  def class_prototype
    unless @class_prototype
      unless const_defined? :ClassMethods
        class_eval "module ClassMethods; end", __FILE__, __LINE__        
      end      
      @class_prototype = const_get :ClassMethods
      
      (class << self; self end).fixed_include @class_prototype
    end
    @class_prototype
  end
  
  def class_methods &block
    if block      
      class_prototype.class_eval &block      
      extend class_prototype
    else
      class_prototype.instance_methods
    end
  end
  
  def inherit *modules
    modules.each do |mod|
      # Instance Methods
      fixed_include mod
      
      # Class Methods
      if self.class == Module        
        class_prototype.fixed_include mod.class_prototype
      else
        (class << self; self end).fixed_include mod.class_prototype
      end          
      
      # callback
      mod.inherited self if mod.respond_to? :inherited
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
ruby_ext-0.4.11 lib/ruby_ext/multiple_inheritance.rb
ruby_ext-0.4.10 lib/ruby_ext/multiple_inheritance.rb
ruby_ext-0.4.9 lib/ruby_ext/multiple_inheritance.rb
ruby_ext-0.4.7 lib/ruby_ext/multiple_inheritance.rb
ruby_ext-0.4.6 lib/ruby_ext/multiple_inheritance.rb
ruby-ext-0.4.6 lib/ruby_ext/multiple_inheritance.rb
ruby-ext-0.4.4 lib/ruby_ext/multiple_inheritance.rb
ruby-ext-0.4.3 lib/ruby_ext/multiple_inheritance.rb
ruby-ext-0.4.2 lib/ruby_ext/multiple_inheritance.rb