Module: IncludeAny

Included in:
Class, Module
Defined in:
lib/include_any/version.rb,
ext/include_any/include_any.c

Constant Summary collapse

VERSION =
'0.0.2'

Instance Method Summary collapse

Instance Method Details

#include_any(module) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/include_any/include_any.c', line 63

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

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

    OBJ_INFECT(klass, module);
    c = klass;
    while (module) {
        int superclass_seen = 0;

        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 = 1;
                break;
            }
        }
        c = RCLASS_SUPER(c) = include_class_new(module, RCLASS_SUPER(c));
        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;
}