Module: NameMagic::NamespaceMethods

Defined in:
lib/y_support/name_magic.rb

Instance Method Summary collapse

Instance Method Details

#__avid_instances__Object

Presents class-owned @avid_instances (no const_magic).



157
158
159
160
# File 'lib/y_support/name_magic.rb', line 157

def __avid_instances__
  namespace.instance_variable_get( :@avid_instances ) ||
    namespace.instance_variable_set( :@avid_instances, [] )
end

#__forget__(instance) ⇒ Object

Clears class-owned references to a specified instance without performing #const_magic first. The argument must be an instance of the target class.



231
232
233
234
235
236
# File 'lib/y_support/name_magic.rb', line 231

def __forget__( instance )
  name = __instances__.delete instance # remove @instances entry
  __avid_instances__.delete( instance ) # remove if any
  namespace.send :remove_const, name if name
  return instance
end

#__instances__Object

Presents class-owned @instances without const_magic.



150
151
152
153
# File 'lib/y_support/name_magic.rb', line 150

def __instances__
  namespace.instance_variable_get( :@instances ) ||
    namespace.instance_variable_set( :@instances, {} )
end

#const_magicObject

The method will search all the modules in the the object space for receiver class objects assigned to constants, and name these instances accordingly. Number of the remaining nameless instances is returned.



201
202
203
204
205
# File 'lib/y_support/name_magic.rb', line 201

def const_magic
  return 0 if nameless_instances.size == 0
  serve_all_modules
  return nameless_instances.size
end

#forget(which_instance) ⇒ Object

Clears class-owned references to a specified instance.



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/y_support/name_magic.rb', line 215

def forget( which_instance )
  inst = begin
           instance( which_instance )
         rescue ArgumentError
           return nil            # nothing to forget
         end
  

#forget_all_instancesObject

Clears class-owned references to all the instances.



250
251
252
253
254
255
# File 'lib/y_support/name_magic.rb', line 250

def forget_all_instances
  __instances__.clear           # clears @instances
  constants( false ).each { |

#forget_anonymous_instancesObject Also known as: forget_nameless_instances

Clears class-owned references anonymous instances.



240
241
242
243
244
245
# File 'lib/y_support/name_magic.rb', line 240

def forget_anonymous_instances
  nameless_instances.each { |inst, 

#instance(arg) ⇒ Object

Returns the instance identified by the argument. NameError is raised, if the argument does not identify an instance. (It can be an instance name as string, symbol, or an instance itself, in which case, the instance in question is merely returned without changes.)



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/y_support/name_magic.rb', line 182

def instance arg
  # In @instances hash, name 'nil' means nameless!
  msg = "'nil' is not a valid argument type for NameMagic#instance method!"
  fail TypeError, msg if arg.nil?
  # if the argument is an actual instance, just return it
  ii = instances
  return arg if ii.include? arg
  # otherwise, assume arg is a name
  begin
    ii.find { |i| i.name == arg || i.name == arg.to_sym }
  rescue NoMethodError
  end or raise NameError, "No instance #{arg} in #{namespace}."
end

#instance_namesObject

Presents an array of all the instance names (disregarding anonymous instances).



144
145
146
# File 'lib/y_support/name_magic.rb', line 144

def instance_names
  instances.map( &:name ).compact
end

#instancesObject

Presents class-owned @instances hash of { instance => name } pairs.



136
137
138
139
# File 'lib/y_support/name_magic.rb', line 136

def instances
  const_magic
  __instances__.keys.select { |i| i.kind_of? self }
end

#name_get_closure(&block) ⇒ Object

Registers a hook to execute whenever the instance is asked about its name. The name object contained in __instances__ is subjected to the name_get_closure before being returned as instance name.



277
# File 'lib/y_support/name_magic.rb', line 277

def name_get_closure █ @name_get_closure = block end

#name_set_closure(&block) ⇒ Object

Registers a hook to execute whenever name setting is performed on an instance. The block should take three arguments (instance, name, old_name). The output value of the block is the name to be actually used – the hook thus allows to define transformations on the name when naming. It is the responsibility of the block to output a suitable symbol (capitalized, usable as a constant name etc.)



271
# File 'lib/y_support/name_magic.rb', line 271

def name_set_closure █ @name_set_closure = block end

#nameless_instancesObject

Returns those instances, which are nameless (@instances hash value is nil).



209
210
211
# File 'lib/y_support/name_magic.rb', line 209

def nameless_instances
  __instances__.select { |key, val| val.nil? }.keys
end

#namespaceObject

Presents class-owned namespace. Normally, this is the class itself, but can be overriden so as to define constants holding the instances in some other module.



166
167
168
# File 'lib/y_support/name_magic.rb', line 166

def namespace
  self
end

#namespace!(namespc = self) ⇒ Object

Makes the class use the namespace supplied as the argument. If no argument is given, self will be the namespace.



173
174
175
# File 'lib/y_support/name_magic.rb', line 173

def namespace! namespc=self
  namespc.tap { |n| define_singleton_method :namespace do n end }
end

#new_instance_closure(&block) ⇒ Object

Registers a hook to execute whenever name magic creates a new instance of the class including NameMagic. The block should take one argument (the new instance that was created) and is called in #new method right after instantiation, but before naming.



262
# File 'lib/y_support/name_magic.rb', line 262

def new_instance_closure █ @new_instance_closure = block end