Class: ConfigTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers.rb

Instance Method Summary collapse

Constructor Details

#initializeConfigTemplate

Initializes a new ConfigTemplate and creates the internal structures needed. To read the actual data use read_template.



145
146
147
148
# File 'lib/helpers.rb', line 145

def initialize
  @hash = Hash.new
  @order = Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name_orig, *args) ⇒ Object (private)



235
236
237
238
239
240
241
242
243
244
# File 'lib/helpers.rb', line 235

def method_missing(name_orig, *args)
  name = name_orig.to_s
  if @hash.has_key?(name)
    @hash[name].value
  elsif @hash.has_key?(name[0..-2]) and name[-1] == "="[0]
    @hash[name[0..-2]].set(args.first)
  else
    super
  end
end

Instance Method Details

#clearObject



163
164
165
166
167
168
169
# File 'lib/helpers.rb', line 163

def clear
  @order.each do |section|
    section[1..-1].each do |option|
      @hash[option].clear
    end
  end
end

#parse(string) ⇒ Object

Parses the string (or Array of strings) as if it was part of a configfile, and adds the value accordingly, if it is not already there.



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/helpers.rb', line 187

def parse(string)
  if string.class == Array then
    string
  else
    string.split("\n")
  end.each do |line|
    if line =~ /^(\w)+ +.+/
      line = line.strip.split(" ", 2)
      raise "No option '#{line[0]}' existing!" if @hash[line[0]].nil?
      @hash[line[0]].add(line[1])
    end
  end
end

#parse_section(section) ⇒ Object

Raises:



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/helpers.rb', line 206

def parse_section(section)
  if section[0,1] == "=" then
    @order << [section[1..-2]]
    return
  end
  lines = section.split("\n")
  first = lines.shift.split(": ")
  name = first.shift
  raise InternalError, "Order still empty!" if @order.empty?
  @order.last << name

  classname = /^[^(]*/.match(first[0]).to_s
                   arguments = /\(.*\)/.match(first[0]).to_s
                   arguments = arguments[1..-2] unless arguments.nil?

                   desc = []
                   defaults = []
                   lines.each do |line|
                     if line =~ /^#{name} */ then
                       defaults << line[(name.length+1)..-1] unless line[(name.length+1)..-1].nil?
                     else
                       desc << line
                     end
                   end
                   @hash[name] = eval("Config#{classname}").new(name, arguments.to_s, desc.join("\n"), defaults)
end

#preference_text(string) ⇒ Object



201
202
203
204
# File 'lib/helpers.rb', line 201

def preference_text(string)
  i = (80 - string.length)/2
  "-"*i + string + "-"*(80-i-string.length)
end

#read_template(path) ⇒ Object

Reads the template file at the given path and



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/helpers.rb', line 172

def read_template(path)
  sections = nil
  File.open(path, "r") do |file|
    sections = file.readlines
    sections.each { |line| line.rstrip! }
    sections = sections.join("\n").split("\n\n")
  end

  sections.each do |section|
    parse_section(section)
  end
end

#to_sObject

Returns everything within this ConfigTemplate as a parseable configfile text.



152
153
154
155
156
157
158
159
160
161
# File 'lib/helpers.rb', line 152

def to_s
  out = ""
  @order.each do |section|
    out += preference_text(section[0]) + "\n\n"
    section[1..-1].each do |option|
      out +=  @hash[option].to_s + "\n\n"
    end
  end
  out
end