module FTW::Singleton

A mixin that provides singleton-ness

Usage:

class Foo
  extend FTW::Singleton

  ...
end

foo = Foo.singleton

Public Class Methods

included(klass) click to toggle source

This is invoked when you include this module. It raises an exception because you should be using 'extend' not 'include' for this module..

# File lib/ftw/singleton.rb, line 17
def self.included(klass)
  raise ArgumentError.new("In #{klass.name}, you want to use 'extend #{self.name}', not 'include ...'")
end

Public Instance Methods

singleton() click to toggle source

Create a singleton instance of whatever class this module is extended into.

Example:

class Foo
  extend FTW::Singleton
  def bar
    "Hello!"
  end
end

p Foo.singleton.bar   # == "Hello!"
# File lib/ftw/singleton.rb, line 33
def singleton
  @instance ||= self.new
  return @instance
end