Module: NameMagic
- Defined in:
- lib/y_support/name_magic.rb
Overview
This mixin imitates Ruby constant magic and automates the named argument :name (alias :ɴ). One thus can write:
class Someclass; include NameMagic end SomeName = SomeClass.new
and the resulting object will know its #name:
SomeName.name = "SomeName"
This is done by searching the whole Ruby namespace for constants, to which the object might have been assigned. The search is performed by the method #const_magic defined by this mixin. Once the object is found to be assigned to a constant, and named accordingly, its subsequent assignments to other constants have no additional effect.
Alternative way to create a named object is by specifying :name (alias :ɴ) named argument:
SomeClass.new a, b, ..., name: "SomeName", aa: v1, bb: v2 ...
Lastly, a name can be assigned by #name= accssor, as in
o = SomeClass.new o.name = "SomeName"
Hook is provided for when the name magic is performed, as well as when the name is retrieved.
Defined Under Namespace
Modules: ClassMethods, NamespaceMethods
Constant Summary collapse
- DEBUG =
false
Class Method Summary collapse
Instance Method Summary collapse
-
#name ⇒ Object
(also: #ɴ)
Retrieves an instance name (demodulized).
-
#name!(ɴ) ⇒ Object
Names an instance, aggresively (overwrites existing names).
-
#name=(ɴ) ⇒ Object
Names an instance, cautiously (ie. no overwriting of existing names).
-
#name_or_object_id ⇒ Object
(also: #ɴ_)
Retrieves either an instance name (if present), or an object id.
Class Method Details
.included(ɱ) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/y_support/name_magic.rb', line 36 def self.included |
Instance Method Details
#name ⇒ Object Also known as: ɴ
Retrieves an instance name (demodulized).
65 66 67 68 69 70 71 72 |
# File 'lib/y_support/name_magic.rb', line 65 def name self.class.const_magic |
#name!(ɴ) ⇒ Object
Names an instance, aggresively (overwrites existing names).
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/y_support/name_magic.rb', line 105 def name!( |
#name=(ɴ) ⇒ Object
Names an instance, cautiously (ie. no overwriting of existing names).
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/y_support/name_magic.rb', line 84 def name=( |
#name_or_object_id ⇒ Object Also known as: ɴ_
Retrieves either an instance name (if present), or an object id.
77 78 79 |
# File 'lib/y_support/name_magic.rb', line 77 def name_or_object_id name || object_id end |