Class: Inifile

Inherits:
Hash show all
Defined in:
lib/vr/contrib/inifile.rb

Instance Method Summary collapse

Constructor Details

#initialize(iniFilename) ⇒ Inifile

Inifile

It makes inifile like Window’s in working directory

section

key=value …

Methods

—deletekey(section,key)

Delete ((|key|)) and associated value in ((|section|)).

—erasesection(section)

Erase ((|section|)) and associated keys and values.

—read( section, key, [default=“”])

Read value associated ((|key|)) in ((|section|)).
If ((|section|)) or ((|key|)) not exists ,value uses ((|default|)).

—write( section, key, value)

Write ((|value|)) in assiciated ((|key|)) and ((|section|)).
If ((|section|)) or ((|key|)) not exists , make them.

—frash

Write into ((|inifilename|)) of instance.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vr/contrib/inifile.rb', line 33

def initialize(iniFilename) #privatemethod. Use "new(inFilename)" instead.
  sec={}
  section=""
  @fn = Dir.getwd + "/" + iniFilename
  if File.exist?(@fn) then
    f=open @fn
    f.each do |t|
      if t =~ /\[.+\]/ then
        section= t.sub(/\[(.+)\]/,'\1').strip
        sec={section=>{}}
        self.update sec
      elsif t =~/.+=/ then
        a=t.split(/=/)
        val={a[0].strip=>a[1].strip}
        self[section].update(val)
      end
    end
    f.close
  end
end

Instance Method Details

#deletekey(section, key) ⇒ Object



54
55
56
# File 'lib/vr/contrib/inifile.rb', line 54

def deletekey(section,key)
  self[section.strip].delete(key)
end

#erasesection(section) ⇒ Object



58
59
60
# File 'lib/vr/contrib/inifile.rb', line 58

def erasesection(section)
  self.delete(section)
end

#flashObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/vr/contrib/inifile.rb', line 71

def flash
  f=open @fn,"w"
  self.each do |k,v|
    f.write'['+k+']' +"\n"
    v.each do |k1,v1|
       f.write k1+"="+v1.to_s+"\n"
    end
  end
  f.close
end

#read(section, key, default = "") ⇒ Object



62
63
64
# File 'lib/vr/contrib/inifile.rb', line 62

def read( section, key, default="")
  if self[section] && r=self[section][key] then r else default end
end

#write(section, key, value) ⇒ Object



66
67
68
69
# File 'lib/vr/contrib/inifile.rb', line 66

def write( section, key, value)
  self.update({section.strip=>{}}) if self[section.strip] == nil
  self[section.strip].update({key.strip => (value.to_s).strip})
end