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(target) ⇒ 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 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/y_support/name_magic.rb', line 36 def self.included target case target when Class then # we will decorate its #new method class << target alias :original_method_new :new # Make space to decorate #new end # Attach the decorators etc. target.extend ::NameMagic::ClassMethods target.extend ::NameMagic::NamespaceMethods # Attach namespace methods also to the namespace, if given. begin if target.namespace == target then target.define_singleton_method :namespace do target end else target.namespace.extend ::NameMagic::NamespaceMethods end rescue NoMethodError end else # it is a Module; we'll infect it with this #included method target_included = target.method( :included ) this_included = self.method( :included ) target_pre_included = begin target.method( :pre_included ) rescue NameError end if target_pre_included then # target has #pre_included hook target.define_singleton_method :included do | |
Instance Method Details
#name ⇒ Object Also known as: ɴ
Retrieves an instance name (demodulized).
78 79 80 81 82 83 84 85 |
# File 'lib/y_support/name_magic.rb', line 78 def name self.class.const_magic |
#name!(ɴ) ⇒ Object
Names an instance, aggresively (overwrites existing names).
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/y_support/name_magic.rb', line 116 def name!( |
#name=(ɴ) ⇒ Object
Names an instance, cautiously (ie. no overwriting of existing names).
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/y_support/name_magic.rb', line 97 def name=( |
#name_or_object_id ⇒ Object Also known as: ɴ_
Retrieves either an instance name (if present), or an object id.
90 91 92 |
# File 'lib/y_support/name_magic.rb', line 90 def name_or_object_id name || object_id end |