Module: Multiton::MetaMethods

Includes:
Inclusive
Defined in:
lib/standard/facets/multiton.rb

Instance Method Summary collapse

Methods included from Inclusive

#included

Instance Method Details

#_load(str) ⇒ Object (protected)



249
250
251
# File 'lib/standard/facets/multiton.rb', line 249

def _load(str)
  instance(*Marshal.load(str))
end

#initialized?(*e, &b) ⇒ Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/standard/facets/multiton.rb', line 230

def initialized?(*e, &b)
  multiton_instance.key?(multiton_id(*e, &b))
end

#instance(*e, &b) ⇒ Object Also known as: new



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/standard/facets/multiton.rb', line 215

def instance(*e, &b)
  arg = multiton_id(*e, &b)
  multiton_instance.fetch(arg) do
    multiton_mutex[arg].synchronize do
      multiton_instance.fetch(arg) do
        val = multiton_instance[arg] = new!(*e, &b) #new(*e, &b)
        val.instance_variable_set(:@multiton_initializer, e, &b)
        multiton_mutex.initialized(arg)
        val
      end
    end
  end
end

#multiton_id(*e, &b) ⇒ Object (private)

Default method to to create a key to cache already constructed instances. In the use case MultitonClass.new(e), MultiClass.new(f) must be semantically equal if multiton_id(e).eql?(multiton_id(f)) evaluates to true.



259
260
261
# File 'lib/standard/facets/multiton.rb', line 259

def multiton_id(*e, &b)
  e
end

#multiton_instanceObject (protected)



236
237
238
# File 'lib/standard/facets/multiton.rb', line 236

def multiton_instance
  @multiton_instance ||= Hash.new
end

#multiton_mutexObject (protected)



240
241
242
# File 'lib/standard/facets/multiton.rb', line 240

def multiton_mutex
  @multiton_mutex ||= InstanceMutex.new
end

#reinitializeObject (protected)



244
245
246
247
# File 'lib/standard/facets/multiton.rb', line 244

def reinitialize
  multiton_instance.clear
  multiton_mutex.clear
end

#singleton_method_added(sym) ⇒ Object (private)



263
264
265
266
267
268
# File 'lib/standard/facets/multiton.rb', line 263

def singleton_method_added(sym)
  super
  if (sym == :marshal_dump) & singleton_methods.include?('marshal_dump')
    raise TypeError, "Don't use marshal_dump - rely on _dump and _load instead"
  end
end