Class with methods to read from and write into ini files.
A ini file is a text file in a specific format, it may include several fields which are sparated by field headlines which are enclosured by “[]”. Each field may include several key-value pairs.
Each key-value pair is represented by one line and the value is sparated from the key by a “=”.
# this is the first comment which will be saved in the comment attribute mail=info@example.com domain=example.com # this is a comment which will not be saved [database] db=example user=john passwd=very-secure host=localhost # this is another comment [filepaths] tmp=/tmp/example lib=/home/john/projects/example/lib htdocs=/home/john/projects/example/htdocs [ texts ] wellcome=Wellcome on my new website! Website description = This is only a example. # and another comment
A Ini#comment stores: "this is the first comment which will be saved in the comment attribute" A Ini object stores: { "mail" => "info@example.com", "domain" => "example.com", "database" => { "db" => "example", "user" => "john", "passwd" => "very-secure", "host" => "localhost" }, "filepaths" => { "tmp" => "/tmp/example", "lib" => "/home/john/projects/example/lib", "htdocs" => "/home/john/projects/example/htdocs" } "texts" => { "wellcome" => "Wellcome on my new website!", "Website description" => "This is only a example." } }
As you can see this module gets rid of all comments, linebreaks and unnecessary spaces at the beginning and the end of each field headline, key or value.
Using the object is stright forward:
ini = Ini.new("path/settings.ini") ini["mail"] = "info@example.com" ini["filepaths"] = { "tmp" => "/tmp/example" } ini.comment = "This is\na comment" puts ini["filepaths"]["tmp"] # => /tmp/example ini.write()
Creating a new Ini object
path is a path to the ini file load if nil restores the data if possible
if true restores the data, if not possible raises an error if false does not resotre the data
# File lib/more/facets/ini.rb, line 103 103: def initialize(path, load=nil) 104: @path = path 105: @inihash = {} 106: 107: if load or ( load.nil? and FileTest.readable_real? @path ) 108: restore() 109: end 110: end
Reading comments from file
path is a path to the ini file
Returns a string with comments from the beginning of the ini file.
# File lib/more/facets/ini.rb, line 201 201: def Ini.read_comment_from_file(path) 202: comment = "" 203: 204: IO.foreach(path) do |line| 205: line.strip! 206: break unless line[0,1] == "#" or line == "" 207: 208: comment << "#{line[1, line.length ].strip}\n" 209: end 210: 211: comment 212: end
Reading data from file
path is a path to the ini file
returns a hash which represents the data from the file
# File lib/more/facets/ini.rb, line 156 156: def Ini.read_from_file(path) 157: 158: inihash = {} 159: headline = nil 160: 161: IO.foreach(path) do |line| 162: 163: line = line.strip.split(/#/)[0] 164: 165: # read it only if the line doesn't begin with a "=" and is long enough 166: unless line.length < 2 and line[0,1] == "=" 167: 168: # it's a headline if the line begins with a "[" and ends with a "]" 169: if line[0,1] == "[" and line[line.length - 1, line.length] == "]" 170: 171: # get rid of the [] and unnecessary spaces 172: headline = line[1, line.length - 2 ].strip 173: inihash[headline] = {} 174: else 175: 176: key, value = line.split(/=/, 2) 177: 178: key = key.strip unless key.nil? 179: value = value.strip unless value.nil? 180: 181: unless headline.nil? 182: inihash[headline][key] = value 183: else 184: inihash[key] = value unless key.nil? 185: end 186: end 187: end 188: end 189: 190: inihash 191: end
Turn a hash (up to 2 levels deepness) into a ini string
inihash is a hash representing the ini File. Default is a empty hash.
Returns a string in the ini file format.
# File lib/more/facets/ini.rb, line 246 246: def Ini.to_s(inihash={}) 247: str = "" 248: 249: inihash.each do |key, value| 250: 251: if value.is_a? Hash 252: str << "[#{key.to_s}]\n" 253: 254: value.each do |under_key, under_value| 255: str << "#{under_key.to_s}=#{under_value.to_s unless under_value.nil?}\n" 256: end 257: 258: else 259: str << "#{key.to_s}=#{value.to_s unless value2.nil?}\n" 260: end 261: end 262: 263: str 264: end
Writing a ini hash into a file
path is a path to the ini file inihash is a hash representing the ini File. Default is a empty hash. comment is a string with comments which appear on the
top of the file. Each line will get a "#" before. Default is no comment.
# File lib/more/facets/ini.rb, line 223 223: def Ini.write_to_file(path, inihash={}, comment=nil) 224: raise TypeError, "String expected" unless comment.is_a? String or comment.nil? 225: 226: raise TypeError, "Hash expected" unless inihash.is_a? Hash 227: File.open(path, "w") { |file| 228: 229: unless comment.nil? 230: comment.each do |line| 231: file << "# #{line}" 232: end 233: end 234: 235: file << Ini.to_s(inihash) 236: } 237: end
Retrive the ini data for the key key
# File lib/more/facets/ini.rb, line 115 115: def [](key) 116: @inihash[key] 117: end
Set the ini data for the key key
# File lib/more/facets/ini.rb, line 122 122: def []=(key, value) 123: raise TypeError, "String expected" unless key.is_a? String 124: raise TypeError, "String or Hash expected" unless value.is_a? String or value.is_a? Hash 125: 126: @inihash[key] = value 127: end
Restores the data from file into the object
# File lib/more/facets/ini.rb, line 132 132: def restore() 133: @inihash = Ini.read_from_file(@path) 134: @comment = Ini.read_comment_from_file(@path) 135: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.
:inihash is a hash which holds all ini data :comment is a string which holds the comments on the top of the file