Class: ConfigString

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

Instance Method Summary collapse

Constructor Details

#initialize(name, args, desc, defaults) ⇒ ConfigString

Takes as args the arguments to the specific type (like positive), as desc the description of the value, and as defaults an Array or a single default value that is shown when no other value is added.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/helpers.rb', line 9

def initialize(name, args, desc, defaults)
  @list = false
  @positive = false
  args.split(",").each{|arg| eval("@#{arg} = true") unless arg.empty?}
  @desc = desc
  @defaults = defaults
  @defaults = "" if @defaults.empty?
  @name = name
  clear
  value
end

Instance Method Details

#add(value) ⇒ Object

Adds the value(s) to @value (can be an Array). Doesn’t strip!



54
55
56
57
58
59
60
# File 'lib/helpers.rb', line 54

def add(value)
  if value.class == Array then
    value.each{ |v| addvalue(v) }
  else
    addvalue(value)
  end
end

#clearObject



68
69
70
# File 'lib/helpers.rb', line 68

def clear
  @value = []
end

#nameObject

Returns the name of this option.



40
41
42
# File 'lib/helpers.rb', line 40

def name
  @name
end

#set(value) ⇒ Object

Sets the value as value of the object (can be an Array).



63
64
65
66
# File 'lib/helpers.rb', line 63

def set(value)
  clear
  add(value)
end

#to_sObject

Prints out the configfile-text with description.



45
46
47
48
49
50
51
# File 'lib/helpers.rb', line 45

def to_s
  if @list then
    ([@desc] + value.collect{|value| format(value)}).join("\n" + name + " ")
  else
    @desc + "\n" + name + " " + format(value)
  end
end

#valueObject

Returns the value, as an Array if list is false, as a single value otherwise. If the value is empty, return the default value(s).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/helpers.rb', line 23

def value
  val = nil
  if @value.empty? then
    set(@defaults)
    val = @value
    clear
  else
    val = @value
  end
  if @list then
    val
  else
    val[0]
  end
end