module JsDuck::Util::Singleton

A more convenient Singleton implementation.

With the standard ruby Singleton you need to call the methods of your singleton instance as follows:

MyClass.instance.my_method()

But with JsDuck::Util::Singleton you can skip the .instance. part:

MyClass.my_method()

This also conveniently hides from the calling code the fact that a class is implemented as Singleton - it could just as well only have static methods.

Public Class Methods

included(base) click to toggle source
# File lib/jsduck/util/singleton.rb, line 22
def self.included(base)
  base.class_eval do
    include ::Singleton

    # Redirect calls from MyClass.method to MyClass.instance.method
    def self.method_missing(meth, *args, &block)
      self.instance.send(meth, *args, &block)
    end
  end
end
method_missing(meth, *args, &block) click to toggle source

Redirect calls from MyClass.method to MyClass.instance.method

# File lib/jsduck/util/singleton.rb, line 27
def self.method_missing(meth, *args, &block)
  self.instance.send(meth, *args, &block)
end