Class Ini
In: lib/more/facets/ini.rb
Parent: Object

ini.rb - read and write ini files

Copyright (C) 2007 Jeena Paradies License: GPL Author: Jeena Paradies (info@jeenaparadies.net)

Overview

This file provides a read-wite handling for ini files. The data of a ini file is represented by a object which is populated with strings.

Methods

Attributes

comment  [RW]  :inihash is a hash which holds all ini data :comment is a string which holds the comments on the top of the file
inihash  [RW]  :inihash is a hash which holds all ini data :comment is a string which holds the comments on the top of the file

Public Class methods

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

[Source]

# File lib/more/facets/ini.rb, line 105
  def initialize(path, load=nil)
    @path = path
    @inihash = {}
    
    if load or ( load.nil? and FileTest.readable_real? @path )
      restore()
    end
  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.

[Source]

# File lib/more/facets/ini.rb, line 198
  def Ini.read_comment_from_file(path)
    comment = ""
    
    IO.foreach(path) do |line|
      line.strip!
      break unless line[0,1] == "#" or line == ""
      
      comment << "#{line[1, line.length ].strip}\n"
    end
    
    comment
  end

Reading data from file

path is a path to the ini file

returns a hash which represents the data from the file

[Source]

# File lib/more/facets/ini.rb, line 153
  def Ini.read_from_file(path)
        
    inihash = {}
    headline = nil
    
    IO.foreach(path) do |line|

      line = line.strip.split(/#/)[0]
      
      # read it only if the line doesn't begin with a "=" and is long enough
      unless line.length < 2 and line[0,1] == "="
        
        # it's a headline if the line begins with a "[" and ends with a "]"
        if line[0,1] == "[" and line[line.length - 1, line.length] == "]"
          
          # get rid of the [] and unnecessary spaces
          headline = line[1, line.length - 2 ].strip
          inihash[headline] = {}
        else
        
          key, value = line.split(/=/, 2)
          
          key = key.strip unless key.nil?
          value = value.strip unless value.nil?
          
          unless headline.nil?
            inihash[headline][key] = value
          else
            inihash[key] = value unless key.nil?
          end
        end        
      end
    end
    
    inihash
  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.

[Source]

# File lib/more/facets/ini.rb, line 243
  def Ini.to_s(inihash={})
    str = ""
    
    inihash.each do |key, value|

      if value.is_a? Hash
        str << "[#{key.to_s}]\n"
        
        value.each do |under_key, under_value|
          str << "#{under_key.to_s}=#{under_value.to_s unless under_value.nil?}\n"
        end

      else
        str << "#{key.to_s}=#{value.to_s unless value2.nil?}\n"
      end
    end
    
    str
  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.

[Source]

# File lib/more/facets/ini.rb, line 220
  def Ini.write_to_file(path, inihash={}, comment=nil)
    raise TypeError, "String expected" unless comment.is_a? String or comment.nil?
    
    raise TypeError, "Hash expected" unless inihash.is_a? Hash
    File.open(path, "w") { |file|
      
      unless comment.nil?
        comment.each do |line|
          file << "# #{line}"
        end
      end
      
      file << Ini.to_s(inihash)
    }
  end

Public Instance methods

Retrive the ini data for the key key

[Source]

# File lib/more/facets/ini.rb, line 117
  def [](key)
    @inihash[key]
  end

Set the ini data for the key key

[Source]

# File lib/more/facets/ini.rb, line 124
  def []=(key, value)
    raise TypeError, "String expected" unless key.is_a? String
    raise TypeError, "String or Hash expected" unless value.is_a? String or value.is_a? Hash
    
    @inihash[key] = value
  end

Restores the data from file into the object

[Source]

# File lib/more/facets/ini.rb, line 134
  def restore()
    @inihash = Ini.read_from_file(@path)
    @comment = Ini.read_comment_from_file(@path)
  end

Store data from the object in the file

[Source]

# File lib/more/facets/ini.rb, line 142
  def update()
    Ini.write_to_file(@path, @inihash, @comment)
  end

[Validate]