Class: IniFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/inifile.rb

Overview

encoding: UTF-8

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.4.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, opts = {}) ⇒ IniFile

call-seq:

IniFile.new( filename )
IniFile.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
:encoding => nil     The encoding used for read/write (RUBY 1.9)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inifile.rb', line 46

def initialize( filename, opts = {} )
  @fn = filename
  @comment = opts[:comment] || ';#'
  @param = opts[:parameter] || '='
  @encoding = opts[:encoding]
  @ini = Hash.new {|h,k| h[k] = Hash.new}

  @rgxp_comment = %r/\A\s*\z|\A\s*[#{@comment}]/
  @rgxp_section = %r/\A\s*\[([^\]]+)\]/o
  @rgxp_param   = %r/\A([^#{@param}]+)#{@param}\s*"?([^"]*)"?\z/

  @rgxp_multiline_start = %r/\A([^#{@param}]+)#{@param}\s*"+([^"]*)?\z/
  @rgxp_multiline_value = %r/\A([^"]*)\z/
  @rgxp_multiline_end   = %r/\A([^"]*)"\z/

  parse
end

Class Method Details

.load(filename, opts = {}) ⇒ Object

call-seq:

IniFile.load( filename )
IniFile.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


29
30
31
# File 'lib/inifile.rb', line 29

def self.load( filename, opts = {} )
  new(filename, opts)
end

Instance Method Details

#[](section) ⇒ Object

call-seq:

ini_file[section]

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



170
171
172
173
# File 'lib/inifile.rb', line 170

def []( section )
  return nil if section.nil?
  @ini[section.to_s]
end

#[]=(section, value) ⇒ Object

call-seq:

ini_file[section] = hash

Set the hash of parameter/value pairs for the given section.



181
182
183
# File 'lib/inifile.rb', line 181

def []=( section, value )
  @ini[section.to_s] = value
end

#cloneObject

call-seq:

clone

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.



258
259
260
261
262
# File 'lib/inifile.rb', line 258

def clone
  other = dup
  other.freeze if self.frozen?
  other
end

#delete_section(section) ⇒ Object

call-seq:

delete_section( section )

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



159
160
161
# File 'lib/inifile.rb', line 159

def delete_section( section )
  @ini.delete section.to_s
end

#dupObject

call-seq:

dup

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.



241
242
243
244
245
246
247
# File 'lib/inifile.rb', line 241

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

#eachObject

call-seq:

each {|section, parameter, value| block}

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



128
129
130
131
132
133
134
135
136
# File 'lib/inifile.rb', line 128

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_sectionObject

call-seq:

each_section {|section| block}

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



145
146
147
148
149
# File 'lib/inifile.rb', line 145

def each_section
  return unless block_given?
  @ini.each_key {|section| yield section}
  self
end

#eql?(other) ⇒ Boolean Also known as: ==

call-seq:

eql?( other )

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.

Returns:

  • (Boolean)


272
273
274
275
276
# File 'lib/inifile.rb', line 272

def eql?( other )
  return true if equal? other
  return false unless other.instance_of? self.class
  @ini == other.instance_variable_get(:@ini)
end

#freezeObject

call-seq:

freeze

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



212
213
214
215
216
217
# File 'lib/inifile.rb', line 212

def freeze
  super
  @ini.each_value {|h| h.freeze}
  @ini.freeze
  self
end

#has_section?(section) ⇒ Boolean

call-seq:

has_section?( section )

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

Returns:

  • (Boolean)


191
192
193
# File 'lib/inifile.rb', line 191

def has_section?( section )
  @ini.has_key? section.to_s
end

#restoreObject

call-seq:

restore

Restore data from the ini file. If the state of this object has been changed but not yet saved, this will effectively undo the changes.



286
287
288
# File 'lib/inifile.rb', line 286

def restore
  parse
end

#sectionsObject

call-seq:

sections

Returns an array of the section names.



201
202
203
# File 'lib/inifile.rb', line 201

def sections
  @ini.keys
end

#taintObject

call-seq:

taint

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



226
227
228
229
230
231
# File 'lib/inifile.rb', line 226

def taint
  super
  @ini.each_value {|h| h.taint}
  @ini.taint
  self
end

#to_hObject

call-seq:

to_h

Convert IniFile to hash format.



117
118
119
# File 'lib/inifile.rb', line 117

def to_h
  @ini.dup
end

#to_sObject

call-seq:

to_s

Convert IniFile to text format.



101
102
103
104
105
106
107
108
109
# File 'lib/inifile.rb', line 101

def to_s
  s = []
  @ini.each do |section,hash|
    s << "[#{section}]"
    hash.each {|param,val| s << "#{param} #{@param} #{val}"}
    s << ""
  end
  s.join("\n")
end

#write(filename = nil, opts = {}) ⇒ Object Also known as: save

call-seq:

write
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. The following options can be passed to this method:

:encoding => nil     The encoding used for writing (RUBY 1.9)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/inifile.rb', line 76

def write( filename = nil, opts={} )
  @fn = filename unless filename.nil?

  encoding = opts[:encoding] || @encoding
  mode = (RUBY_VERSION >= '1.9' && @encoding) ?
       "w:#{encoding.to_s}" :
       'w'

  File.open(@fn, mode) 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