Class: Zeitwerk::Cref

Inherits:
Object
  • Object
show all
Includes:
RealModName
Defined in:
lib/zeitwerk/cref.rb

Overview

This private class encapsulates pairs (mod, cname).

Objects represent the constant cname in the class or module object mod, and have API to manage them that encapsulates the constants API. Examples:

cref.path
cref.set(value)
cref.get

The constant may or may not exist in mod.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RealModName

#real_mod_name

Constructor Details

#initialize(mod, cname) ⇒ Cref

The type of the first argument is Module because Class < Module, class objects are also valid.



23
24
25
26
27
# File 'lib/zeitwerk/cref.rb', line 23

def initialize(mod, cname)
  @mod   = mod
  @cname = cname
  @path  = nil
end

Instance Attribute Details

#cnameObject (readonly)

Returns the value of attribute cname.



17
18
19
# File 'lib/zeitwerk/cref.rb', line 17

def cname
  @cname
end

Instance Method Details

#autoload(abspath) ⇒ Object



74
75
76
# File 'lib/zeitwerk/cref.rb', line 74

def autoload(abspath)
  @mod.autoload(@cname, abspath)
end

#autoload?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/zeitwerk/cref.rb', line 63

def autoload?
  @mod.autoload?(@cname) if self.defined?
end

#defined?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/zeitwerk/cref.rb', line 79

def defined?
  @mod.const_defined?(@cname, false)
end

#getObject

Raises:



90
91
92
# File 'lib/zeitwerk/cref.rb', line 90

def get
  @mod.const_get(@cname, false)
end

#pathObject



34
35
36
# File 'lib/zeitwerk/cref.rb', line 34

def path
  @path ||= Object.equal?(@mod) ? @cname.name : "#{real_mod_name(@mod)}::#{@cname.name}"
end

#removeObject

Raises:



96
97
98
# File 'lib/zeitwerk/cref.rb', line 96

def remove
  @mod.__send__(:remove_const, @cname)
end

#set(value) ⇒ Object



84
85
86
# File 'lib/zeitwerk/cref.rb', line 84

def set(value)
  @mod.const_set(@cname, value)
end