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 "  def \#{attr_name} val=Rant.__rant_no_value__\n      if val.equal? Rant.__rant_no_value__\n    @\#{attr_name}\n      else\n    @\#{attr_name} = val\n      end\n  end\n    EOD\n    nil\nend\n"

#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 "        def \#{attr_name}?\n            @\#{attr_name}\n        end\n        def \#{attr_name}\n            @\#{attr_name} = true\n        end\n        def no_\#{attr_name}\n            @\#{attr_name} = false\n        end\n    EOD\nend\n"

#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 "            def \#{message}(*args, &blk)\n                # the first ; on the next line is needed\n                # because of rant-import\n                ;\#{receiver}.\#{message}(*args, &blk)\n            end\n        EOD\n    }\n    nil\nend\n"

#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 "            def \#{attr_name}; \#{receiver}.\#{attr_name}; end\n        EOD\n    }\n    nil\nend\n"

#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 "            def \#{attr_name}=(val); \#{receiver}.\#{attr_name}=\n                val; end\n        EOD\n    }\n    nil\nend\n"

#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 "  def \#{attr_name}=(val)\n      if val.respond_to? :to_str\n    @\#{attr_name} = val.to_str\n      else\n    raise ArgumentError,\n        \"string (#to_str) value required\", caller\n      end\n  end\n  def \#{attr_name} val=Rant.__rant_no_value__\n      if val.equal? Rant.__rant_no_value__\n    @\#{attr_name}\n      else\n    self.__send__(:\#{attr_name}=, val)\n      end\n  end\n    EOD\n    nil\nend\n"

#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