Module: Multiton::Semi

Defined in:
lib/mega/multiton.rb

Overview

A Semi-Multiton allows for both regular instantiation and multiton use. In other words the #new method is left accessible. (see Multiton)

Class Method Summary collapse

Class Method Details

.append_features(klass) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mega/multiton.rb', line 143

def self.append_features(klass)
  def klass.instance(*args, &block)
    # if the class defined 'multiton_id' we use this as the key
    # otherwise we simply use the argument list.
    k = (respond_to?(MULTITON_ID_HOOK) ? send(MULTITON_ID_HOOK, *args, &block) : args)
    unless (obj = (POOLS[self] ||= {})[k])
      begin
        critical = Thread.critical
        Thread.critical = true
        meth = self.respond_to?(MULTITON_NEW_HOOK) ? MULTITON_NEW_HOOK : :new
        obj = (POOLS[self][k] = self.send(meth, *args, &block))
      ensure
        Thread.critical = critical # restore state
      end
    end
    return obj
  end
end