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. Examples:
cref.path
cref.set(value)
cref.get
The constant may or may not exist in ‘mod`.
Defined Under Namespace
Classes: Map
Instance Attribute Summary collapse
-
#cname ⇒ Object
readonly
: Symbol.
-
#mod ⇒ Object
readonly
: Module.
Instance Method Summary collapse
-
#autoload(abspath) ⇒ Object
: (String) -> nil.
-
#autoload? ⇒ Boolean
: () -> String?.
-
#defined? ⇒ Boolean
: () -> bool.
-
#get ⇒ Object
: () -> top ! NameError.
-
#initialize(mod, cname) ⇒ Cref
constructor
The type of the first argument is Module because Class < Module, class objects are also valid.
-
#path ⇒ Object
(also: #to_s)
: () -> String.
-
#remove ⇒ Object
: () -> void ! NameError.
-
#set(value) ⇒ Object
: (top) -> top.
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.
: (Module, Symbol) -> void
28 29 30 31 32 |
# File 'lib/zeitwerk/cref.rb', line 28 def initialize(mod, cname) @mod = mod @cname = cname @path = nil end |
Instance Attribute Details
#cname ⇒ Object (readonly)
: Symbol
22 23 24 |
# File 'lib/zeitwerk/cref.rb', line 22 def cname @cname end |
#mod ⇒ Object (readonly)
: Module
19 20 21 |
# File 'lib/zeitwerk/cref.rb', line 19 def mod @mod end |
Instance Method Details
#autoload(abspath) ⇒ Object
: (String) -> nil
46 47 48 |
# File 'lib/zeitwerk/cref.rb', line 46 def autoload(abspath) @mod.autoload(@cname, abspath) end |
#autoload? ⇒ Boolean
: () -> String?
41 42 43 |
# File 'lib/zeitwerk/cref.rb', line 41 def autoload? @mod.autoload?(@cname, false) end |
#defined? ⇒ Boolean
: () -> bool
51 52 53 |
# File 'lib/zeitwerk/cref.rb', line 51 def defined? @mod.const_defined?(@cname, false) end |
#get ⇒ Object
: () -> top ! NameError
61 62 63 |
# File 'lib/zeitwerk/cref.rb', line 61 def get @mod.const_get(@cname, false) end |
#path ⇒ Object Also known as: to_s
: () -> String
35 36 37 |
# File 'lib/zeitwerk/cref.rb', line 35 def path @path ||= Object == @mod ? @cname.name : "#{real_mod_name(@mod)}::#{@cname.name}".freeze end |
#remove ⇒ Object
: () -> void ! NameError
66 67 68 |
# File 'lib/zeitwerk/cref.rb', line 66 def remove @mod.__send__(:remove_const, @cname) end |
#set(value) ⇒ Object
: (top) -> top
56 57 58 |
# File 'lib/zeitwerk/cref.rb', line 56 def set(value) @mod.const_set(@cname, value) end |