Module: GollyUtils::Singleton::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#def_accessor(target, name = nil) ⇒ nil

Creates an instance accessor as attr_accessor does, execpt that the default value will be the singleton instance.

Examples:

class HappyDays
  include GollyUtils::Singleton
end

class A
  # @!attribute [rw] happy_days
  #   @return [HappyDays]
  HappyDays.def_accessor self
end

A.new.happy_days == HappyDays.instance  #=> true

Parameters:

  • target (Class|Module)

    The object definition to add the attribute methods to.

  • name (String|Symbol) (defaults to: nil)

    The attribute name. Defaults to the singleton class name, with underscores and in lowercase.

Returns:

  • (nil)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/golly-utils/singleton.rb', line 78

def def_accessor(target, name=nil)
  name ||= __default_singleton_attr_name
  target.class_eval <<-EOB
    def #{name}
       @#{name} ||= ::#{self}.instance
    end
    def #{name}=(v)
       @#{name}= v
    end
  EOB
  nil
end

#hide_singleton_methods(*matchers) ⇒ nil

Prevents class-level delegate methods from being created for certain instance methods.

Parameters:

  • matchers (Array<Regexp|String>)

    Either the name of the method, or a regex that matches methods.

Returns:

  • (nil)


95
96
97
98
99
100
101
# File 'lib/golly-utils/singleton.rb', line 95

def hide_singleton_methods(*matchers)
  r=  __gu_singleton_rejects
  r.concat matchers
  r.flatten!
  r.uniq!
  nil
end