Method: NameMagic#name=
- Defined in:
- lib/y_support/name_magic.rb
#name=(ɴ) ⇒ Object
Names an instance, cautiously (ie. no overwriting of existing names).
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/y_support/name_magic.rb', line 119 def name=( ɴ ) old_ɴ = namespace.__instances__[ self ] # previous name if ɴ then puts "NameMagic: Naming with argument #{ɴ}." if DEBUG ɴ = namespace.send( :validate_name, # honor the hook namespace.name_set_closure.( ɴ, self, old_ɴ ) ).to_sym puts "NameMagic: Name adjusted to #{ɴ}." if DEBUG return if old_ɴ == ɴ # already named as required fail NameError, "Name '#{ɴ}' already exists in #{namespace} namespace!" if self.class.__instances__.rassoc( ɴ ) namespace.const_set ɴ, self # write a constant namespace.__instances__[ self ] = ɴ # write to @instances namespace.__forget__ old_ɴ # forget the old name of self else puts "NameMagic: Unnaming #{old_ɴ || self}" if DEBUG namespace.__instances__.update( self => nil ) # unname in @instances namespace.send :remove_const, old_ɴ if old_ɴ # remove namespace const. end end |