Class: Module

Inherits:
Object show all
Defined in:
lib/include_complete.rb

Instance Method Summary collapse

Instance Method Details

#append_features(include) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'ext/include_complete/patched_include.c', line 90

static VALUE
rb_patched_mod_append_features(VALUE module, VALUE include)
{
  switch (TYPE(include)) {
  case T_CLASS:
  case T_MODULE:
    break;
  default:
    Check_Type(include, T_CLASS);
    break;
  }
  rb_patched_include_module(include, module);

  return module;
}

#include_complete(*mods) ⇒ Object

include modules (and their singletons) into an inheritance chain

Examples:

module M
  def self.hello
    puts "hello"
  end
end
class C
  include_complete M
end
C.hello #=> "hello"

Parameters:

  • mods (Module)

    Modules to include_complete

Returns:

  • Returns the receiver



36
37
38
39
40
41
# File 'lib/include_complete.rb', line 36

def include_complete(*mods)
  mods.reverse.each do |mod|
    include_complete_one mod
  end
  self
end

#include_complete_one(module) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'ext/include_complete/include_complete_one.c', line 92

static VALUE
rb_include_complete_module_one(VALUE klass, VALUE module)
{
  VALUE p, c;
  int changed = 0;

  rb_frozen_class_p(klass);
  if (!OBJ_UNTRUSTED(klass)) {
    rb_secure(4);
  }

  if (TYPE(module) != T_MODULE) {
    Check_Type(module, T_MODULE);
  }

  OBJ_INFECT(klass, module);
  c = klass;

  /* ensure singleton classes exist, both for includer and includee */
  rb_singleton_class(module);
  rb_singleton_class(klass);
  
  while (module) {
    int superclass_seen = FALSE;

    if (RCLASS_M_TBL(klass) == RCLASS_M_TBL(module))
      rb_raise(rb_eArgError, "cyclic include detected");
    /* ignore if the module included already in superclasses */
    for (p = RCLASS_SUPER(klass); p; p = RCLASS_SUPER(p)) {
      switch (BUILTIN_TYPE(p)) {
      case T_ICLASS:
        if (RCLASS_M_TBL(p) == RCLASS_M_TBL(module)) {
          if (!superclass_seen) {
            c = p;  /* move insertion point */
          }
          goto skip;
        }
        break;
      case T_CLASS:
        superclass_seen = TRUE;
        break;
      }
    }

    /* we're including the module, so create the iclass */
    VALUE imod = include_class_new(module, RCLASS_SUPER(c));

    /* module gets included directly above c, so set the super */
    RCLASS_SUPER(c) = imod;

    /* also do the same for parallel inheritance chain (singleton classes) */
    RCLASS_SUPER(KLASS_OF(c)) = KLASS_OF(imod);
    c = imod;
        
    if (RMODULE_M_TBL(module) && RMODULE_M_TBL(module)->num_entries)
      changed = 1;
  skip:
    module = RCLASS_SUPER(module);
  }
  if (changed) rb_clear_cache();

  return Qnil;
}