Sha256: 3928889fd15c84a51e58978e5f856790441f3c0df73c613e0f806f46e3f5a824

Contents?: true

Size: 878 Bytes

Versions: 40

Compression:

Stored size: 878 Bytes

Contents

require "ftw/namespace"

# A mixin that provides singleton-ness
#
# Usage:
#
#     class Foo
#       extend FTW::Singleton
#
#       ...
#     end
#
# foo = Foo.singleton
module FTW::Singleton
  # This is invoked when you include this module. It raises an exception because you should be
  # using 'extend' not 'include' for this module..
  def self.included(klass)
    raise ArgumentError.new("In #{klass.name}, you want to use 'extend #{self.name}', not 'include ...'")
  end # def included

  # 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!"
  def singleton
    @instance ||= self.new
    return @instance
  end # def self.singleton
end # module FTW::Singleton

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
ftw-0.0.49 lib/ftw/singleton.rb
ftw-0.0.48 lib/ftw/singleton.rb
ftw-0.0.47 lib/ftw/singleton.rb
ftw-0.0.46 lib/ftw/singleton.rb
ftw-0.0.45 lib/ftw/singleton.rb
ftw-0.0.44 lib/ftw/singleton.rb
ftw-0.0.43 lib/ftw/singleton.rb
ftw-0.0.42 lib/ftw/singleton.rb
ftw-0.0.41 lib/ftw/singleton.rb
ftw-0.0.40 lib/ftw/singleton.rb
ftw-0.0.39 lib/ftw/singleton.rb
ftw-0.0.38 lib/ftw/singleton.rb
ftw-0.0.37 lib/ftw/singleton.rb
ftw-0.0.36 lib/ftw/singleton.rb
ftw-0.0.35 lib/ftw/singleton.rb
ftw-0.0.34 lib/ftw/singleton.rb
ftw-0.0.33 lib/ftw/singleton.rb
ftw-0.0.32 lib/ftw/singleton.rb
ftw-0.0.31 lib/ftw/singleton.rb
ftw-0.0.30 lib/ftw/singleton.rb