Module: Arborist::MethodUtilities

Included in:
Arborist, CLI, Manager, Monitor, Node, Node::Root, Observer, TreeAPI
Defined in:
lib/arborist/mixins.rb

Overview

A collection of methods for declaring other methods.

class MyClass
    extend Arborist::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.



73
74
75
76
77
78
# File 'lib/arborist/mixins.rb', line 73

def attr_predicate( attrname )
	attrname = attrname.to_s.chomp( '?' )
	define_method( "#{attrname}?" ) do
		instance_variable_get( "@#{attrname}" ) ? 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.



83
84
85
86
87
# File 'lib/arborist/mixins.rb', line 83

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

#dsl_accessor(attrname) ⇒ Object

Create an method that is both a reader and a writer for an instance variable. If called with a (non-nil) argument, it will set the variable to the new value. It returns whatever the instance variable is set to.



93
94
95
96
97
98
# File 'lib/arborist/mixins.rb', line 93

def dsl_accessor( attrname )
	define_method( attrname ) do |arg=nil|
		instance_variable_set( "@#{attrname}", arg ) unless arg.nil?
		return instance_variable_get( "@#{attrname}" )
	end
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.



52
53
54
55
56
# File 'lib/arborist/mixins.rb', line 52

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/arborist/mixins.rb', line 27

def singleton_attr_reader( *symbols )
	singleton_class.instance_exec( symbols ) do |attrs|
		attr_reader( *attrs )
	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.



43
44
45
46
47
# File 'lib/arborist/mixins.rb', line 43

def singleton_attr_writer( *symbols )
	singleton_class.instance_exec( symbols ) do |attrs|
		attr_writer( *attrs )
	end
end

#singleton_method_alias(newname, original) ⇒ Object

Creates an alias for the original method named newname.



67
68
69
# File 'lib/arborist/mixins.rb', line 67

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

#singleton_predicate_accessor(*symbols) ⇒ Object

Create predicate methods and writers that allow assignment to the attributes of the singleton of the declaring object that correspond to the specified symbols.



61
62
63
64
# File 'lib/arborist/mixins.rb', line 61

def singleton_predicate_accessor( *symbols )
	singleton_class.extend( Arborist::MethodUtilities )
	singleton_class.attr_predicate_accessor( *symbols )
end

#singleton_predicate_reader(*symbols) ⇒ Object

Create instance variables and corresponding methods that return true or false values for each of the specified symbols in the singleton of the declaring object.



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

def singleton_predicate_reader( *symbols )
	singleton_class.extend( Arborist::MethodUtilities )
	singleton_class.attr_predicate( *symbols )
end