Module: Rant::MetaUtils

Included in:
Generators::Archive::Base
Defined in:
lib/rant/metautils.rb

Instance Method Summary collapse

Instance Method Details

#rant_attr(attr_name) ⇒ Object

Creates three accessor methods:

obj.attr_name::	Return value of instance variable

@attr_name

obj.attr_name = val::	Set value instance variable

@attr_name to val

obj.attr_name val::	same as above


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rant/metautils.rb', line 14

def rant_attr attr_name
    attr_name = valid_attr_name attr_name
    attr_writer attr_name
    module_eval <<-EOD
	def #{attr_name} val=Rant.__rant_no_value__
	    if val.equal? Rant.__rant_no_value__
		@#{attr_name}
	    else
		@#{attr_name} = val
	    end
	end
    EOD
    nil
end

#rant_flag(attr_name) ⇒ Object

Creates three accessor methods:

obj.attr_name?::    Return value, true or false
obj.attr_name::     Set attribute to true
obj.no_attr_name::  Set attribute to false


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rant/metautils.rb', line 32

def rant_flag attr_name
    attr_name = valid_attr_name attr_name
    module_eval <<-EOD
        def #{attr_name}?
            @#{attr_name}
        end
        def #{attr_name}
            @#{attr_name} = true
        end
        def no_#{attr_name}
            @#{attr_name} = false
        end
    EOD
end

#redirect_accessor(receiver, *attributes) ⇒ Object



70
71
72
73
# File 'lib/rant/metautils.rb', line 70

def redirect_accessor(receiver, *attributes)
    redirect_reader(receiver, *attributes)
    redirect_writer(receiver, *attributes)
end

#redirect_message(receiver, *messages) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rant/metautils.rb', line 97

def redirect_message(receiver, *messages)
    messages.each { |message|
        module_eval <<-EOD
            def #{message}(*args, &blk)
                # the first ; on the next line is needed
                # because of rant-import
                ;#{receiver}.#{message}(*args, &blk)
            end
        EOD
    }
    nil
end

#redirect_reader(receiver, *attributes) ⇒ Object

Create attribute reader methods that redirect to the entity given as first argument (e.g. an instance variable name).



76
77
78
79
80
81
82
83
84
# File 'lib/rant/metautils.rb', line 76

def redirect_reader(receiver, *attributes)
    attributes.each { |attr_name|
        attr_name = valid_attr_name attr_name
        module_eval <<-EOD
            def #{attr_name}; #{receiver}.#{attr_name}; end
        EOD
    }
    nil
end

#redirect_writer(receiver, *attributes) ⇒ Object

Create attribute writer methods that redirect to the entity given as first argument (e.g. an instance variable name).



87
88
89
90
91
92
93
94
95
96
# File 'lib/rant/metautils.rb', line 87

def redirect_writer(receiver, *attributes)
    attributes.each { |attr_name|
        attr_name = valid_attr_name attr_name
        module_eval <<-EOD
            def #{attr_name}=(val); #{receiver}.#{attr_name}=
                val; end
        EOD
    }
    nil
end

#string_attr(attr_name) ⇒ Object

Creates accessor methods like #rant_attr for the attribute attr_name. Additionally, values are converted with to_str before assignment to instance variables happens.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rant/metautils.rb', line 49

def string_attr attr_name
    attr_name = valid_attr_name attr_name
    module_eval <<-EOD
	def #{attr_name}=(val)
	    if val.respond_to? :to_str
		@#{attr_name} = val.to_str
	    else
		raise ArgumentError,
		    "string (#to_str) value required", caller
	    end
	end
	def #{attr_name} val=Rant.__rant_no_value__
	    if val.equal? Rant.__rant_no_value__
		@#{attr_name}
	    else
		self.__send__(:#{attr_name}=, val)
	    end
	end
    EOD
    nil
end

#valid_attr_name(attr_name) ⇒ Object

attr_name is converted to a string with #to_s and has to match /^w+$/. Returns attr_name.to_s.



111
112
113
114
115
116
117
# File 'lib/rant/metautils.rb', line 111

def valid_attr_name attr_name
    attr_name = attr_name.to_s
    attr_name =~ /^\w+\??$/ or
	raise ArgumentError,
	    "argument has to match /^\w+\??$/", caller
    attr_name
end