Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ext/core/multiple_inheritance.rb,
lib/ruby_ext/core/multiple_inheritance.rb

Overview

Inheritance

Instance Method Summary collapse

Instance Method Details

#class_methods(&block) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 58

def class_methods &block
  if block
    class_prototype.class_eval &block
    extend class_prototype
  else
    class_prototype.instance_methods
  end
end

#class_prototypeObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 39

def class_prototype
  unless @class_prototype
    ancestor = ancestors[1]
    if(
      !const_defined?(:ClassMethods) or
      (
        const_defined?(:ClassMethods) and ancestor and ancestor.const_defined?(:ClassMethods) and
        const_get(:ClassMethods) == ancestor.const_get(:ClassMethods)
      )
    )
      class_eval "module ClassMethods; end", __FILE__, __LINE__
    end
    @class_prototype = const_get :ClassMethods

    (class << self; self end).include2 @class_prototype
  end
  @class_prototype
end

#directly_included_byObject



18
19
20
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 18

def directly_included_by
  @directly_included_by ||= {}
end

#include2(mod) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 22

def include2 mod
  # unless mod.directly_included_by.include? self
  mod.directly_included_by[self] = true
  # end

  include mod
  directly_included_by.each do |child, v|
    child.include2 self
  end
end

#inherit(*modules) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 73

def inherit *modules
  modules.each do |mod|
    # Instance Methods
    include2 mod

    # Class Methods
    unless self.class_prototype == mod.class_prototype
      if self.class == Module
        class_prototype.include2 mod.class_prototype
      else
        (class << self; self end).include2 mod.class_prototype
      end
    end

    # Inherited callback
    if self.class == Class
      mod.ancestors.each do |anc|
        anc.inherited.each{|b| self.instance_eval &b}
      end
    end
  end
end

#inherited(&b) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_ext/core/multiple_inheritance.rb', line 67

def inherited &b
  @inherited ||= []
  @inherited << b if b
  @inherited
end