Module: Redwood::Singleton::ClassMethods

Defined in:
lib/sup/util.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *a, &b) ⇒ Object



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/sup/util.rb', line 566

def method_missing meth, *a, &b
  raise "no #{name} instance defined in method call to #{meth}!" unless defined? @instance

  ## if we've been deinstantiated, just drop all calls. this is
  ## useful because threads that might be active during the
  ## cleanup process (e.g. polling) would otherwise have to
  ## special-case every call to a Singleton object
  return nil if @instance.nil?

  # Speed up further calls by defining a shortcut around method_missing
  if meth.to_s[-1,1] == '='
    # Argh! Inconsistency! Setters do not work like all the other methods.
    class_eval "def self.#{meth}(a); @instance.send :#{meth}, a; end"
  else
    class_eval "def self.#{meth}(*a, &b); @instance.send :#{meth}, *a, &b; end"
  end

  @instance.send meth, *a, &b
end

Instance Method Details

#deinstantiate!Object



565
# File 'lib/sup/util.rb', line 565

def deinstantiate!; @instance = nil; end

#init(*args) ⇒ Object



585
586
587
588
# File 'lib/sup/util.rb', line 585

def init *args
  raise "there can be only one! (instance)" if instantiated?
  @instance = new(*args)
end

#instanceObject



563
# File 'lib/sup/util.rb', line 563

def instance; @instance; end

#instantiated?Boolean

Returns:

  • (Boolean)


564
# File 'lib/sup/util.rb', line 564

def instantiated?; defined?(@instance) && !@instance.nil?; end