Hglib::

MethodUtilities

module

A collection of methods for declaring other methods.

class MyClass
    extend Hglib::MethodUtilities

    singleton_attr_accessor :types
    singleton_method_alias :kinds, :types
end

MyClass.types = [ :pheno, :proto, :stereo ]
MyClass.kinds # => [:pheno, :proto, :stereo]

Public Instance Methods

anchor
attr_predicate( attrname )

Create a reader in the form of a predicate for the given attrname.

# File lib/hglib/mixins.rb, line 56
def attr_predicate( attrname )
        attrname = attrname.to_s.chomp( '?' )
        define_method( "#{attrname}?" ) do
                ivar = "@#{attrname}"
                instance_variable_defined?( ivar ) && instance_variable_get( ivar ) ?
                        true :
                        false
        end
end
anchor
attr_predicate_accessor( attrname )

Create a reader in the form of a predicate for the given attrname as well as a regular writer method.

# File lib/hglib/mixins.rb, line 69
def attr_predicate_accessor( attrname )
        attrname = attrname.to_s.chomp( '?' )
        attr_writer( attrname )
        attr_predicate( attrname )
end
anchor
singleton_attr_accessor( *symbols )

Creates readers and writers that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.

# File lib/hglib/mixins.rb, line 43
def singleton_attr_accessor( *symbols )
        symbols.each do |sym|
                singleton_class.__send__( :attr_accessor, sym )
        end
end
anchor
singleton_attr_reader( *symbols )

Creates instance variables and corresponding methods that return their values for each of the specified symbols in the singleton of the declaring object (e.g., class instance variables and methods if declared in a Class).

# File lib/hglib/mixins.rb, line 26
def singleton_attr_reader( *symbols )
        symbols.each do |sym|
                singleton_class.__send__( :attr_reader, sym )
        end
end
anchor
singleton_attr_writer( *symbols )

Creates methods that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.

# File lib/hglib/mixins.rb, line 34
def singleton_attr_writer( *symbols )
        symbols.each do |sym|
                singleton_class.__send__( :attr_writer, sym )
        end
end
anchor
singleton_method_alias( newname, original )

Creates an alias for the original method named newname.

# File lib/hglib/mixins.rb, line 50
def singleton_method_alias( newname, original )
        singleton_class.__send__( :alias_method, newname, original )
end