Class: Module

Inherits:
Object show all
Defined in:
opal/opal/corelib/module.rb,
opal/opal/corelib/unsupported.rb,
opal/opal/corelib/marshal/write_buffer.rb

Overview

helpers: truthy, coerce_to, const_set, Object, return_ivar, assign_ivar, ivar, deny_frozen_access, freeze, prop, jsid backtick_javascript: true

Direct Known Subclasses

Refinement

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Module

Returns a new instance of Module.



30
31
32
# File 'opal/opal/corelib/module.rb', line 30

def initialize(&block)
  module_eval(&block) if block_given?
end

Class Method Details

.allocateObject



21
22
23
24
25
26
27
28
# File 'opal/opal/corelib/module.rb', line 21

def self.allocate
  %x{
    var module = Opal.allocate_module(nil, function(){});
    // Link the prototype of Module subclasses
    if (self !== Opal.Module) Object.setPrototypeOf(module, self.$$prototype);
    return module;
  }
end

.constants(inherit = undefined) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'opal/opal/corelib/module.rb', line 294

def self.constants(inherit = undefined)
  %x{
    if (inherit == null) {
      var nesting = (self.$$nesting || []).concat($Object),
          constant, constants = {},
          i, ii;

      for(i = 0, ii = nesting.length; i < ii; i++) {
        for (constant in nesting[i].$$const) {
          constants[constant] = true;
        }
      }
      return Object.keys(constants);
    } else {
      return Opal.constants(self, inherit)
    }
  }
end

.nestingObject



313
314
315
# File 'opal/opal/corelib/module.rb', line 313

def self.nesting
  `self.$$nesting || []`
end

Instance Method Details

#<(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'opal/opal/corelib/module.rb', line 40

def <(other)
  unless ::Module === other
    ::Kernel.raise ::TypeError, 'compared with non class/module'
  end

  # class cannot be a descendant of itself
  %x{
    var working = self,
        ancestors,
        i, length;

    if (working === other) {
      return false;
    }

    for (i = 0, ancestors = Opal.ancestors(self), length = ancestors.length; i < length; i++) {
      if (ancestors[i] === other) {
        return true;
      }
    }

    for (i = 0, ancestors = Opal.ancestors(other), length = ancestors.length; i < length; i++) {
      if (ancestors[i] === self) {
        return false;
      }
    }

    return nil;
  }
end

#<=(other) ⇒ Object



71
72
73
# File 'opal/opal/corelib/module.rb', line 71

def <=(other)
  equal?(other) || self < other
end

#<=>(other) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'opal/opal/corelib/module.rb', line 87

def <=>(other)
  %x{
    if (self === other) {
      return 0;
    }
  }

  unless ::Module === other
    return nil
  end

  lt = self < other
  return nil if lt.nil?
  lt ? -1 : 1
end

#===(object) ⇒ Object



34
35
36
37
38
# File 'opal/opal/corelib/module.rb', line 34

def ===(object)
  return false if `object == null`

  `Opal.is_a(object, self)`
end

#>(other) ⇒ Object



75
76
77
78
79
80
81
# File 'opal/opal/corelib/module.rb', line 75

def >(other)
  unless ::Module === other
    ::Kernel.raise ::TypeError, 'compared with non class/module'
  end

  other < self
end

#>=(other) ⇒ Object



83
84
85
# File 'opal/opal/corelib/module.rb', line 83

def >=(other)
  equal?(other) || self > other
end

#__marshal__(buffer) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 115

def __marshal__(buffer)
  unless name
    ::Kernel.raise ::TypeError, "can't dump anonymous module"
  end

  buffer.save_link(self)
  buffer.append('m')
  buffer.write_module(self)
end

#alias_method(newname, oldname) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'opal/opal/corelib/module.rb', line 103

def alias_method(newname, oldname)
  `$deny_frozen_access(self)`

  newname = `$coerce_to(newname, #{::String}, 'to_str')`
  oldname = `$coerce_to(oldname, #{::String}, 'to_str')`
  `Opal.alias(self, newname, oldname)`

  self
end

#alias_native(mid, jsid = mid) ⇒ Object



113
114
115
116
117
118
119
# File 'opal/opal/corelib/module.rb', line 113

def alias_native(mid, jsid = mid)
  `$deny_frozen_access(self)`

  `Opal.alias_native(self, mid, jsid)`

  self
end

#ancestorsObject



121
122
123
# File 'opal/opal/corelib/module.rb', line 121

def ancestors
  `Opal.ancestors(self)`
end

#append_features(includer) ⇒ Object



125
126
127
128
129
130
# File 'opal/opal/corelib/module.rb', line 125

def append_features(includer)
  `$deny_frozen_access(includer)`

  `Opal.append_features(self, includer)`
  self
end

#attr(*args) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'opal/opal/corelib/module.rb', line 137

def attr(*args)
  %x{
    if (args.length == 2 && (args[1] === true || args[1] === false)) {
      #{warn 'optional boolean argument is obsoleted', uplevel: 1}

      args[1] ? #{attr_accessor(`args[0]`)} : #{attr_reader(`args[0]`)};
      return nil;
    }
  }

  attr_reader(*args)
end

#attr_accessor(*names) ⇒ Object



132
133
134
135
# File 'opal/opal/corelib/module.rb', line 132

def attr_accessor(*names)
  attr_reader(*names)
  attr_writer(*names)
end

#attr_reader(*names) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'opal/opal/corelib/module.rb', line 150

def attr_reader(*names)
  %x{
    $deny_frozen_access(self);

    var proto = self.$$prototype;

    for (var i = names.length - 1; i >= 0; i--) {
      var name = names[i],
          id   = $jsid(name),
          ivar = $ivar(name);

      var body = $return_ivar(ivar);

      // initialize the instance variable as nil
      Opal.prop(proto, ivar, nil);

      body.$$parameters = [];
      body.$$arity = 0;

      Opal.defn(self, id, body);
    }
  }

  nil
end

#attr_writer(*names) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'opal/opal/corelib/module.rb', line 176

def attr_writer(*names)
  %x{
    $deny_frozen_access(self);

    var proto = self.$$prototype;

    for (var i = names.length - 1; i >= 0; i--) {
      var name = names[i],
          id   = $jsid(name + '='),
          ivar = $ivar(name);

      var body = $assign_ivar(ivar)

      body.$$parameters = [['req']];
      body.$$arity = 1;

      // initialize the instance variable as nil
      Opal.prop(proto, ivar, nil);

      Opal.defn(self, id, body);
    }
  }

  nil
end

#autoload(const, path) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'opal/opal/corelib/module.rb', line 202

def autoload(const, path)
  %x{
    $deny_frozen_access(self);

    if (!#{Opal.const_name?(const)}) {
      #{::Kernel.raise ::NameError, "autoload must be constant name: #{const}"}
    }

    if (path == "") {
      #{::Kernel.raise ::ArgumentError, 'empty file name'}
    }

    if (!self.$$const.hasOwnProperty(#{const})) {
      if (!self.$$autoload) {
        self.$$autoload = {};
      }
      Opal.const_cache_version++;
      self.$$autoload[#{const}] = { path: #{path}, loaded: false, required: false, success: false, exception: false };

      if (self.$const_added && !self.$const_added.$$pristine) {
        self.$const_added(#{const});
      }
    }
    return nil;
  }
end

#autoload?(const) ⇒ Boolean

Returns:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'opal/opal/corelib/module.rb', line 229

def autoload?(const)
  %x{
    if (self.$$autoload && self.$$autoload[#{const}] && !self.$$autoload[#{const}].required && !self.$$autoload[#{const}].success) {
      return self.$$autoload[#{const}].path;
    }

    var ancestors = self.$ancestors();

    for (var i = 0, length = ancestors.length; i < length; i++) {
      if (ancestors[i].$$autoload && ancestors[i].$$autoload[#{const}] && !ancestors[i].$$autoload[#{const}].required && !ancestors[i].$$autoload[#{const}].success) {
        return ancestors[i].$$autoload[#{const}].path;
      }
    }
    return nil;
  }
end

#class_variable_defined?(name) ⇒ Boolean

Returns:



264
265
266
267
268
# File 'opal/opal/corelib/module.rb', line 264

def class_variable_defined?(name)
  name = ::Opal.class_variable_name!(name)

  `Opal.class_variables(self).hasOwnProperty(name)`
end

#class_variable_get(name) ⇒ Object



250
251
252
253
254
# File 'opal/opal/corelib/module.rb', line 250

def class_variable_get(name)
  name = ::Opal.class_variable_name!(name)

  `Opal.class_variable_get(self, name, false)`
end

#class_variable_set(name, value) ⇒ Object



256
257
258
259
260
261
262
# File 'opal/opal/corelib/module.rb', line 256

def class_variable_set(name, value)
  `$deny_frozen_access(self)`

  name = ::Opal.class_variable_name!(name)

  `Opal.class_variable_set(self, name, value)`
end

#class_variablesObject



246
247
248
# File 'opal/opal/corelib/module.rb', line 246

def class_variables
  `Object.keys(Opal.class_variables(self))`
end

#const_added(name) ⇒ Object



270
271
# File 'opal/opal/corelib/module.rb', line 270

def const_added(name)
end

#const_defined?(name, inherit = true) ⇒ Boolean

check for constant within current scope if inherit is true or self is Object, will also check ancestors

Returns:



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'opal/opal/corelib/module.rb', line 319

def const_defined?(name, inherit = true)
  name = Opal.const_name!(name)

  ::Kernel.raise ::NameError.new("wrong constant name #{name}", name) unless name =~ ::Opal::CONST_NAME_REGEXP

  %x{
    var module, modules = [self], module_constants, i, ii;

    // Add up ancestors if inherit is true
    if (inherit) {
      modules = modules.concat(Opal.ancestors(self));

      // Add Object's ancestors if it's a module – modules have no ancestors otherwise
      if (self.$$is_module) {
        modules = modules.concat([$Object]).concat(Opal.ancestors($Object));
      }
    }

    for (i = 0, ii = modules.length; i < ii; i++) {
      module = modules[i];
      if (module.$$const[#{name}] != null) { return true; }
      if (
        module.$$autoload &&
        module.$$autoload[#{name}] &&
        !module.$$autoload[#{name}].required &&
        !module.$$autoload[#{name}].success
      ) {
        return true;
      }
    }

    return false;
  }
end

#const_get(name, inherit = true) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'opal/opal/corelib/module.rb', line 354

def const_get(name, inherit = true)
  name = Opal.const_name!(name)

  %x{
    if (name.indexOf('::') === 0 && name !== '::'){
      name = name.slice(2);
    }
  }

  if `name.indexOf('::') != -1 && name != '::'`
    return name.split('::').inject(self) { |o, c| o.const_get(c) }
  end

  ::Kernel.raise ::NameError.new("wrong constant name #{name}", name) unless name =~ ::Opal::CONST_NAME_REGEXP

  %x{
    if (inherit) {
      return Opal.$$([self], name);
    } else {
      return Opal.const_get_local(self, name);
    }
  }
end

#const_missing(name) ⇒ Object



378
379
380
381
382
# File 'opal/opal/corelib/module.rb', line 378

def const_missing(name)
  full_const_name = self == ::Object ? name : "#{self}::#{name}"

  ::Kernel.raise ::NameError.new("uninitialized constant #{full_const_name}", name)
end

#const_set(name, value) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'opal/opal/corelib/module.rb', line 384

def const_set(name, value)
  `$deny_frozen_access(self)`

  name = ::Opal.const_name!(name)

  if name !~ ::Opal::CONST_NAME_REGEXP || name.start_with?('::')
    ::Kernel.raise ::NameError.new("wrong constant name #{name}", name)
  end

  `$const_set(self, name, value)`

  value
end

#constants(inherit = true) ⇒ Object



290
291
292
# File 'opal/opal/corelib/module.rb', line 290

def constants(inherit = true)
  `Opal.constants(self, inherit)`
end

#copy_class_variables(other) ⇒ Object



803
804
805
806
807
808
809
# File 'opal/opal/corelib/module.rb', line 803

def copy_class_variables(other)
  %x{
    for (var name in other.$$cvars) {
      self.$$cvars[name] = other.$$cvars[name];
    }
  }
end

#copy_constants(other) ⇒ Object



811
812
813
814
815
816
817
818
819
# File 'opal/opal/corelib/module.rb', line 811

def copy_constants(other)
  %x{
    var name, other_constants = other.$$const;

    for (name in other_constants) {
      $const_set(self, name, other_constants[name]);
    }
  }
end

#define_method(name, method = undefined, &block) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'opal/opal/corelib/module.rb', line 401

def define_method(name, method = undefined, &block)
  %x{
    $deny_frozen_access(self);

    if (method === undefined && block === nil)
      #{::Kernel.raise ::ArgumentError, 'tried to create a Proc object without a block'}

    name = ensure_symbol_or_string(name);
  }

  if `method !== undefined`
    block = case method
            when ::Proc
              method

            when ::Method
              `#{method.to_proc}.$$unbound`

            when ::UnboundMethod
              `Opal.wrap_method_body(method.$$method)`

            else
              ::Kernel.raise ::TypeError, "wrong argument type #{method.class} (expected Proc/Method/UnboundMethod)"
            end

    if `!method.$$is_proc`
      owner = method.owner
      if `owner.$$is_class` && !(self <= owner) # rubocop:disable Style/InverseMethods
        message = `owner.$$is_singleton` ? "can't bind singleton method to a different class" : "bind argument must be a subclass of #{owner}"
        ::Kernel.raise ::TypeError, message
      end
    end
  end

  %x{
    if (typeof(Proxy) !== 'undefined') {
      var meta = Object.create(null)

      block.$$proxy_target = block
      block = new Proxy(block, {
        apply: function(target, self, args) {
          var old_name = target.$$jsid, old_lambda = target.$$is_lambda;
          target.$$jsid = name;
          target.$$is_lambda = true;
          try {
            return target.apply(self, args);
          } catch(e) {
            if (e === target.$$brk || e === target.$$ret) return e.$v;
            throw e;
          } finally {
            target.$$jsid = old_name;
            target.$$is_lambda = old_lambda;
          }
        }
      })
    }

    block.$$jsid        = name;
    block.$$s           = null;
    block.$$def         = block;
    block.$$define_meth = true;

    return Opal.defn(self, $jsid(name), block);
  }
end

#extend_object(object) ⇒ Object



565
566
567
568
# File 'opal/opal/corelib/module.rb', line 565

def extend_object(object)
  `$deny_frozen_access(object)`
  nil
end

#extended(mod) ⇒ Object



562
563
# File 'opal/opal/corelib/module.rb', line 562

def extended(mod)
end

#freezeObject



467
468
469
470
471
472
473
474
475
476
477
478
# File 'opal/opal/corelib/module.rb', line 467

def freeze
  # Specialized version of freeze, because the $$base_module property needs to be
  # accessible despite the frozen status

  return self if frozen?

  %x{
    if (!self.hasOwnProperty('$$base_module')) { $prop(self, '$$base_module', null); }

    return $freeze(self);
  }
end

#include(*mods) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'opal/opal/corelib/module.rb', line 497

def include(*mods)
  %x{
    for (var i = mods.length - 1; i >= 0; i--) {
      var mod = mods[i];

      if (!mod.$$is_module) {
        #{::Kernel.raise ::TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
      }

      #{`mod`.append_features self};
      #{`mod`.included self};
    }
  }

  self
end

#include?(mod) ⇒ Boolean

Returns:



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'opal/opal/corelib/module.rb', line 518

def include?(mod)
  %x{
    if (!mod.$$is_module) {
      #{::Kernel.raise ::TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
    }

    var i, ii, mod2, ancestors = Opal.ancestors(self);

    for (i = 0, ii = ancestors.length; i < ii; i++) {
      mod2 = ancestors[i];
      if (mod2 === mod && mod2 !== self) {
        return true;
      }
    }

    return false;
  }
end

#included(mod) ⇒ Object



559
560
# File 'opal/opal/corelib/module.rb', line 559

def included(mod)
end

#included_modulesObject



514
515
516
# File 'opal/opal/corelib/module.rb', line 514

def included_modules
  `Opal.included_modules(self)`
end

#initialize_copy(other) ⇒ Object



786
787
788
789
790
791
792
793
794
795
# File 'opal/opal/corelib/module.rb', line 786

def initialize_copy(other)
  %x{
    copyInstanceMethods(other, self);
    copyIncludedModules(other, self);
    copyPrependedModules(other, self);
    self.$$cloned_from = other.$$cloned_from.concat(other);
  }
  copy_class_variables(other)
  copy_constants(other)
end

#initialize_dup(other) ⇒ Object



797
798
799
800
801
# File 'opal/opal/corelib/module.rb', line 797

def initialize_dup(other)
  super
  # Unlike other classes, Module's singleton methods are copied on Object#dup.
  copy_singleton_methods(other)
end

#instance_method(name) ⇒ Object Also known as: public_instance_method



537
538
539
540
541
542
543
544
545
546
547
# File 'opal/opal/corelib/module.rb', line 537

def instance_method(name)
  %x{
    var meth = self.$$prototype[$jsid(name)];

    if (!meth || meth.$$stub) {
      #{::Kernel.raise ::NameError.new("undefined method `#{name}' for class `#{self.name}'", name)};
    }

    return #{::UnboundMethod.new(self, `meth.$$owner || #{self}`, `meth`, name)};
  }
end

#instance_methods(include_super = true) ⇒ Object Also known as: public_instance_methods



549
550
551
552
553
554
555
556
557
# File 'opal/opal/corelib/module.rb', line 549

def instance_methods(include_super = true)
  %x{
    if ($truthy(#{include_super})) {
      return Opal.instance_methods(self);
    } else {
      return Opal.own_instance_methods(self);
    }
  }
end

#instance_variablesObject



742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'opal/opal/corelib/module.rb', line 742

def instance_variables
  consts = constants
  %x{
    var result = [];

    for (var name in self) {
      if (self.hasOwnProperty(name) && name.charAt(0) !== '$' && name !== 'constructor' && !#{consts.include?(`name`)}) {
        result.push('@' + name);
      }
    }

    return result;
  }
end

#method_addedObject



570
571
# File 'opal/opal/corelib/module.rb', line 570

def method_added(*)
end

#method_defined?(method) ⇒ Boolean Also known as: public_method_defined?

Returns:



623
624
625
626
627
628
# File 'opal/opal/corelib/module.rb', line 623

def method_defined?(method)
  %x{
    var body = self.$$prototype[$jsid(method)];
    return (!!body) && !body.$$stub;
  }
end

#method_removedObject



573
574
# File 'opal/opal/corelib/module.rb', line 573

def method_removed(*)
end

#method_undefinedObject



576
577
# File 'opal/opal/corelib/module.rb', line 576

def method_undefined(*)
end

#module_eval(*args, &block) ⇒ Object Also known as: class_eval



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'opal/opal/corelib/module.rb', line 579

def module_eval(*args, &block)
  if block.nil? && `!!Opal.compile`
    ::Kernel.raise ::ArgumentError, 'wrong number of arguments (0 for 1..3)' unless (1..3).cover? args.size

    string, file, _lineno = *args
    default_eval_options = { file: (file || '(eval)'), eval: true }
    compiling_options = __OPAL_COMPILER_CONFIG__.merge(default_eval_options)
    compiled = ::Opal.compile string, compiling_options
    block = ::Kernel.proc do
      %x{new Function("Opal,self", "return " + compiled)(Opal, self)}
    end
  elsif args.any?
    ::Kernel.raise ::ArgumentError, "wrong number of arguments (#{args.size} for 0)" \
                                    "\n\n  NOTE:If you want to enable passing a String argument please add \"require 'opal-parser'\" to your script\n"
  end

  %x{
    var old = block.$$s,
        result;

    block.$$s = null;
    result = block.apply(self, [self]);
    block.$$s = old;

    return result;
  }
end

#module_exec(*args, &block) ⇒ Object Also known as: class_exec



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'opal/opal/corelib/module.rb', line 607

def module_exec(*args, &block)
  %x{
    if (block === nil) {
      #{::Kernel.raise ::LocalJumpError, 'no block given'}
    }

    var block_self = block.$$s, result;

    block.$$s = null;
    result = block.apply(self, args);
    block.$$s = block_self;

    return result;
  }
end

#module_function(*methods) ⇒ Object



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'opal/opal/corelib/module.rb', line 630

def module_function(*methods)
  %x{
    $deny_frozen_access(self);

    if (methods.length === 0) {
      self.$$module_function = true;
      return nil;
    }
    else {
      for (var i = 0, length = methods.length; i < length; i++) {
        var meth = methods[i],
            id   = $jsid(meth),
            func = self.$$prototype[id];

        Opal.defs(self, id, func);
      }
      return methods.length === 1 ? methods[0] : methods;
    }

    return self;
  }
end

#nameObject



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'opal/opal/corelib/module.rb', line 653

def name
  %x{
    if (self.$$full_name) {
      return self.$$full_name;
    }

    var result = [], base = self;

    while (base) {
      // Give up if any of the ancestors is unnamed
      if (base.$$name === nil || base.$$name == null) return nil;

      result.unshift(base.$$name);

      base = base.$$base_module;

      if (base === $Object) {
        break;
      }
    }

    if (result.length === 0) {
      return nil;
    }

    return self.$$full_name = result.join('::');
  }
end

#prepend(*mods) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'opal/opal/corelib/module.rb', line 682

def prepend(*mods)
  %x{
    if (mods.length === 0) {
      #{::Kernel.raise ::ArgumentError, 'wrong number of arguments (given 0, expected 1+)'}
    }

    for (var i = mods.length - 1; i >= 0; i--) {
      var mod = mods[i];

      if (!mod.$$is_module) {
        #{::Kernel.raise ::TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
      }

      #{`mod`.prepend_features self};
      #{`mod`.prepended self};
    }
  }

  self
end

#prepend_features(prepender) ⇒ Object



703
704
705
706
707
708
709
710
711
712
713
714
# File 'opal/opal/corelib/module.rb', line 703

def prepend_features(prepender)
  %x{
    $deny_frozen_access(prepender);

    if (!self.$$is_module) {
      #{::Kernel.raise ::TypeError, "wrong argument type #{self.class} (expected Module)"};
    }

    Opal.prepend_features(self, prepender)
  }
  self
end

#prepended(mod) ⇒ Object



716
717
# File 'opal/opal/corelib/module.rb', line 716

def prepended(mod)
end

#private_class_method(*methods) ⇒ Object Also known as: public_class_method



73
74
75
# File 'opal/opal/corelib/unsupported.rb', line 73

def private_class_method(*methods)
  `return (methods.length === 1) ? methods[0] : methods`
end

#private_constantObject



81
82
# File 'opal/opal/corelib/unsupported.rb', line 81

def private_constant(*)
end

#private_method_defined?(obj) ⇒ Boolean Also known as: protected_method_defined?

Returns:



77
78
79
# File 'opal/opal/corelib/unsupported.rb', line 77

def private_method_defined?(obj)
  false
end

#public(*methods) ⇒ Object Also known as: nesting, private, protected



63
64
65
66
67
68
69
70
71
# File 'opal/opal/corelib/unsupported.rb', line 63

def public(*methods)
  %x{
    if (methods.length === 0) {
      self.$$module_function = false;
      return nil;
    }
    return (methods.length === 1) ? methods[0] : methods;
  }
end

#public_constant(const_name) ⇒ Object



398
399
# File 'opal/opal/corelib/module.rb', line 398

def public_constant(const_name)
end

#refine(klass, &block) ⇒ Object



821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'opal/opal/corelib/module.rb', line 821

def refine(klass, &block)
  refinement_module, m, klass_id = self, nil, nil
  %x{
    klass_id = Opal.id(klass);
    if (typeof self.$$refine_modules === "undefined") {
      self.$$refine_modules = Object.create(null);
    }
    if (typeof self.$$refine_modules[klass_id] === "undefined") {
      m = self.$$refine_modules[klass_id] = #{::Refinement.new};
    }
    else {
      m = self.$$refine_modules[klass_id];
    }
    m.refinement_module = refinement_module
    m.refined_class = klass
  }
  m.class_exec(&block)
  m
end

#refinementsObject



841
842
843
844
845
846
847
848
849
850
# File 'opal/opal/corelib/module.rb', line 841

def refinements
  %x{
    var refine_modules = self.$$refine_modules, hash = #{{}};;
    if (typeof refine_modules === "undefined") return hash;
    for (var id in refine_modules) {
      hash['$[]='](refine_modules[id].refined_class, refine_modules[id]);
    }
    return hash;
  }
end

#remove_class_variable(name) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'opal/opal/corelib/module.rb', line 274

def remove_class_variable(name)
  `$deny_frozen_access(self)`

  name = ::Opal.class_variable_name!(name)

  %x{
    if (Opal.hasOwnProperty.call(self.$$cvars, name)) {
      var value = self.$$cvars[name];
      delete self.$$cvars[name];
      return value;
    } else {
      #{::Kernel.raise ::NameError, "cannot remove #{name} for #{self}"}
    }
  }
end

#remove_const(name) ⇒ Object



719
720
721
722
723
# File 'opal/opal/corelib/module.rb', line 719

def remove_const(name)
  `$deny_frozen_access(self)`

  `Opal.const_remove(self, name)`
end

#remove_method(*names) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
# File 'opal/opal/corelib/module.rb', line 480

def remove_method(*names)
  %x{
    for (var i = 0; i < names.length; i++) {
      var name = ensure_symbol_or_string(names[i]);
      $deny_frozen_access(self);

      Opal.rdef(self, "$" + name);
    }
  }

  self
end

#singleton_class?Boolean

Returns:



493
494
495
# File 'opal/opal/corelib/module.rb', line 493

def singleton_class?
  `!!self.$$is_singleton`
end

#to_sObject Also known as: inspect



725
726
727
# File 'opal/opal/corelib/module.rb', line 725

def to_s
  `Opal.Module.$name.call(self)` || "#<#{`self.$$is_module ? 'Module' : 'Class'`}:0x#{__id__.to_s(16)}>"
end

#undef_method(*names) ⇒ Object



729
730
731
732
733
734
735
736
737
738
739
740
# File 'opal/opal/corelib/module.rb', line 729

def undef_method(*names)
  %x{
    for (var i = 0; i < names.length; i++) {
      var name = ensure_symbol_or_string(names[i]);
      $deny_frozen_access(self);

      Opal.udef(self, "$" + name);
    }
  }

  self
end

#using(mod) ⇒ Object

Compiler overrides this method



853
854
855
# File 'opal/opal/corelib/module.rb', line 853

def using(mod)
  ::Kernel.raise 'Module#using is not permitted in methods'
end