Module: GollyUtils::Singleton

Defined in:
lib/golly-utils/singleton.rb

Overview

Makes the including class a singleton. Much like Ruby's Singleton module, with extra features.

Features:

  • Target class includes Ruby's Singleton module too.
  • Class methods are added to the target class that delegate to the singleton instance.
  • A convenience method def_accessor is provided to create an accessor in the calling class, as per attr_accessor, except that the value defaults to the singleton instance.

Examples:

class Stam1na
  include GollyUtils::Singleton

  def band_rating
    puts 'AWESOME!!'
  end
end

Stam1na.instance.band_rating  #=> AWESOME!!
Stam1na.band_rating           #=> AWESOME!!

Defined Under Namespace

Modules: ClassMethods