Class: Zeitwerk::Cref
- Inherits:
-
Object
- Object
- Zeitwerk::Cref
- 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
-
#cname ⇒ Object
readonly
Returns the value of attribute cname.
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
Instance Method Summary collapse
- #autoload(abspath) ⇒ Object
- #autoload? ⇒ Boolean
- #defined? ⇒ Boolean
- #get ⇒ Object
-
#initialize(mod, cname) ⇒ Cref
constructor
The type of the first argument is Module because Class < Module, class objects are also valid.
- #path ⇒ Object
- #remove ⇒ Object
- #set(value) ⇒ Object
Methods included from RealModName
Constructor Details
#initialize(mod, cname) ⇒ Cref
The type of the first argument is Module because Class < Module, class objects are also valid.
26 27 28 29 30 |
# File 'lib/zeitwerk/cref.rb', line 26 def initialize(mod, cname) @mod = mod @cname = cname @path = nil end |
Instance Attribute Details
#cname ⇒ Object (readonly)
Returns the value of attribute cname.
20 21 22 |
# File 'lib/zeitwerk/cref.rb', line 20 def cname @cname end |
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
17 18 19 |
# File 'lib/zeitwerk/cref.rb', line 17 def mod @mod end |
Instance Method Details
#autoload(abspath) ⇒ Object
43 44 45 |
# File 'lib/zeitwerk/cref.rb', line 43 def autoload(abspath) @mod.autoload(@cname, abspath) end |
#autoload? ⇒ Boolean
38 39 40 |
# File 'lib/zeitwerk/cref.rb', line 38 def autoload? @mod.autoload?(@cname, false) end |
#defined? ⇒ Boolean
48 49 50 |
# File 'lib/zeitwerk/cref.rb', line 48 def defined? @mod.const_defined?(@cname, false) end |
#get ⇒ Object
59 60 61 |
# File 'lib/zeitwerk/cref.rb', line 59 def get @mod.const_get(@cname, false) end |
#path ⇒ Object
33 34 35 |
# File 'lib/zeitwerk/cref.rb', line 33 def path @path ||= Object.equal?(@mod) ? @cname.name : "#{real_mod_name(@mod)}::#{@cname.name}".freeze end |
#remove ⇒ Object
65 66 67 |
# File 'lib/zeitwerk/cref.rb', line 65 def remove @mod.__send__(:remove_const, @cname) end |
#set(value) ⇒ Object
53 54 55 |
# File 'lib/zeitwerk/cref.rb', line 53 def set(value) @mod.const_set(@cname, value) end |