Module: Hglib::MethodUtilities

Included in:
Extension::Topic::Entry, Extension::Topic::StackEntry, Repo::Bookmark, Repo::Id
Defined in:
lib/hglib/mixins.rb

Overview

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]

Instance Method Summary collapse

Instance Method Details

#attr_predicate(attrname) ⇒ Object

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



57
58
59
60
61
62
63
64
65
# File 'lib/hglib/mixins.rb', line 57

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

#attr_predicate_accessor(attrname) ⇒ Object

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



70
71
72
73
74
# File 'lib/hglib/mixins.rb', line 70

def attr_predicate_accessor( attrname )
	attrname = attrname.to_s.chomp( '?' )
	attr_writer( attrname )
	attr_predicate( attrname )
end

#singleton_attr_accessor(*symbols) ⇒ Object

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



44
45
46
47
48
# File 'lib/hglib/mixins.rb', line 44

def singleton_attr_accessor( *symbols )
	symbols.each do |sym|
		singleton_class.__send__( :attr_accessor, sym )
	end
end

#singleton_attr_reader(*symbols) ⇒ Object

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).



27
28
29
30
31
# File 'lib/hglib/mixins.rb', line 27

def singleton_attr_reader( *symbols )
	symbols.each do |sym|
		singleton_class.__send__( :attr_reader, sym )
	end
end

#singleton_attr_writer(*symbols) ⇒ Object

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



35
36
37
38
39
# File 'lib/hglib/mixins.rb', line 35

def singleton_attr_writer( *symbols )
	symbols.each do |sym|
		singleton_class.__send__( :attr_writer, sym )
	end
end

#singleton_method_alias(newname, original) ⇒ Object

Creates an alias for the original method named newname.



51
52
53
# File 'lib/hglib/mixins.rb', line 51

def singleton_method_alias( newname, original )
	singleton_class.__send__( :alias_method, newname, original )
end