In Files

Parent

Namespace

Files

Class/Module Index [+]

Quicksearch

Ini

This class represents the INI file and can be used to parse, modify, and write INI files.

Public Class Methods

load( filename ) click to toggle source
load( filename, options )

Open the given filename and load the contetns of the INI file. The following options can be passed to this method:

:comment => ';'      The line comment character(s)
:parameter => '='    The parameter / value separator
# File lib/ini.rb, line 24
def self.load( filename, opts = {} )
  new(filename, opts)
end
new( filename ) click to toggle source
new( filename, options )

Create a new INI file using the given filename. If filename exists and is a regular file, then its contents will be parsed. The following options can be passed to this method:

:comment => ';'      The line comment character(s)
:parameter => '='    The parameter / value separator
# File lib/ini.rb, line 40
def initialize( filename, opts = {} )
  @fn = filename
  @comment = opts[:comment] || ';'
  @param = opts[:parameter] || '='
  @ini = Hash.new {|h,k| h[k] = Hash.new}

  @rgxp_comment = /\A\s*\z|\A\s*[#{@comment}]/
  @rgxp_section = /\A\s*\[([^\]]+)\]/
  @rgxp_param   = /\A([^#{@param}]+)#{@param}(.*)\z/

  parse
end

Public Instance Methods

==( other ) click to toggle source
Alias for: eql?
ini_file[section] click to toggle source

Get the hash of parameter/value pairs for the given section. If the section hash does not exist it will be created.

# File lib/ini.rb, line 125
def []( section )
  return nil if section.nil?
  @ini[section.to_s]
end
clone click to toggle source

Produces a duplicate of this INI file. The duplicate is independent of the original -- i.e. the duplicate can be modified without changing the orgiinal. The tainted state and the frozen state of the original is copied to the duplicate.

# File lib/ini.rb, line 203
def clone
  other = dup
  other.freeze if self.frozen?
  other
end
delete_section( section ) click to toggle source

Deletes the named section from the INI file. Returns the parameter / value pairs if the section exists in the INI file. Otherwise, returns nil.

# File lib/ini.rb, line 114
def delete_section( section )
  @ini.delete section.to_s
end
dup click to toggle source

Produces a duplicate of this INI file. The duplicate is independent of the original -- i.e. the duplicate can be modified without changing the orgiinal. The tainted state of the original is copied to the duplicate.

# File lib/ini.rb, line 186
def dup
  other = super
  other.instance_variable_set(:@ini, Hash.new {|h,k| h[k] = Hash.new})
  @ini.each_pair {|s,h| other[s].merge! h}
  other.taint if self.tainted?
  other
end
each {|section, parameter, value| block} click to toggle source

Yield each section, parameter, value in turn to the given block. The method returns immediately if no block is supplied.

# File lib/ini.rb, line 83
def each
  return unless block_given?
  @ini.each do |section,hash|
    hash.each do |param,val|
      yield section, param, val
    end
  end
  self
end
each_section {|section| block} click to toggle source

Yield each section in turn to the given block. The method returns immediately if no block is supplied.

# File lib/ini.rb, line 100
def each_section
  return unless block_given?
  @ini.each_key {|section| yield section}
  self
end
eql?( other ) click to toggle source

Returns true if the other object is equivalent to this INI file. For two INI files to be equivalent, they must have the same sections with the same parameter / value pairs in each section.

# File lib/ini.rb, line 217
def eql?( other )
  return true if equal? other
  return false unless other.instance_of? self.class
  @ini == other.instance_variable_get(:@ini)
end
Also aliased as: ==
freeze click to toggle source

Freeze the state of the IniFile object. Any attempts to change the object will raise an error.

# File lib/ini.rb, line 157
def freeze
  super
  @ini.each_value {|h| h.freeze}
  @ini.freeze
  self
end
has_section?( section ) click to toggle source

Returns true if the named section exists in the INI file.

# File lib/ini.rb, line 136
def has_section?( section )
  @ini.has_key? section.to_s
end
save( filename = nil ) click to toggle source
Alias for: write
sections click to toggle source

Returns an array of the section names.

# File lib/ini.rb, line 146
def sections
  @ini.keys
end
taint click to toggle source

Marks the INI file as tainted -- this will traverse each section marking each section as tainted as well.

# File lib/ini.rb, line 171
def taint
  super
  @ini.each_value {|h| h.taint}
  @ini.taint
  self
end
write click to toggle source
write( filename )

Write the INI file contents to the filesystem. The given filename will be used to write the file. If filename is not given, then the named used when constructing this object will be used.

# File lib/ini.rb, line 62
def write( filename = nil )
  @fn = filename unless filename.nil?

  ::File.open(@fn, 'w') do |f|
    @ini.each do |section,hash|
      f.puts "[#{section}]"
      hash.each {|param,val| f.puts "#{param} #{@param} #{val}"}
      f.puts
    end
  end
  self
end
Also aliased as: save

[Validate]

Generated with the Darkfish Rdoc Generator 2.