Class: IniFile
Overview
This class represents the INI file and can be used to parse, modify, and write INI files.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.3.0'
Class Method Summary collapse
-
.load(filename, opts = {}) ⇒ Object
call-seq: IniFile.load( filename ) IniFile.load( filename, options ).
Instance Method Summary collapse
-
#[](section) ⇒ Object
call-seq: ini_file.
-
#[]=(section, value) ⇒ Object
call-seq: ini_file = hash.
-
#clone ⇒ Object
call-seq: clone.
-
#delete_section(section) ⇒ Object
call-seq: delete_section( section ).
-
#dup ⇒ Object
call-seq: dup.
-
#each ⇒ Object
call-seq: each {|section, parameter, value| block}.
-
#each_section ⇒ Object
call-seq: each_section {|section| block}.
-
#eql?(other) ⇒ Boolean
(also: #==)
call-seq: eql?( other ).
-
#freeze ⇒ Object
call-seq: freeze.
-
#has_section?(section) ⇒ Boolean
call-seq: has_section?( section ).
-
#initialize(filename, opts = {}) ⇒ IniFile
constructor
call-seq: IniFile.new( filename ) IniFile.new( filename, options ).
-
#restore ⇒ Object
call-seq: restore.
-
#sections ⇒ Object
call-seq: sections.
-
#taint ⇒ Object
call-seq: taint.
-
#to_h ⇒ Object
call-seq: to_h.
-
#to_s ⇒ Object
call-seq: to_s.
-
#write(filename = nil) ⇒ Object
(also: #save)
call-seq: write write( filename ).
Constructor Details
#initialize(filename, opts = {}) ⇒ IniFile
call-seq:
IniFile.new( filename )
IniFile.new( filename, )
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
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/inifile.rb', line 42 def initialize( filename, opts = {} ) @fn = filename @comment = opts[:comment] || ';#' @param = opts[:parameter] || '=' @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}(.*)\z/ parse end |
Class Method Details
.load(filename, opts = {}) ⇒ Object
26 27 28 |
# File 'lib/inifile.rb', line 26 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.
153 154 155 156 |
# File 'lib/inifile.rb', line 153 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.
164 165 166 |
# File 'lib/inifile.rb', line 164 def []=( section, value ) @ini[section.to_s] = value end |
#clone ⇒ Object
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.
241 242 243 244 245 |
# File 'lib/inifile.rb', line 241 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.
142 143 144 |
# File 'lib/inifile.rb', line 142 def delete_section( section ) @ini.delete section.to_s end |
#dup ⇒ Object
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.
224 225 226 227 228 229 230 |
# File 'lib/inifile.rb', line 224 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 ⇒ Object
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.
111 112 113 114 115 116 117 118 119 |
# File 'lib/inifile.rb', line 111 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 ⇒ Object
call-seq:
each_section {|section| block}
Yield each section in turn to the given block. The method returns immediately if no block is supplied.
128 129 130 131 132 |
# File 'lib/inifile.rb', line 128 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.
255 256 257 258 259 |
# File 'lib/inifile.rb', line 255 def eql?( other ) return true if equal? other return false unless other.instance_of? self.class @ini == other.instance_variable_get(:@ini) end |
#freeze ⇒ Object
call-seq:
freeze
Freeze the state of the IniFile object. Any attempts to change the object will raise an error.
195 196 197 198 199 200 |
# File 'lib/inifile.rb', line 195 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.
174 175 176 |
# File 'lib/inifile.rb', line 174 def has_section?( section ) @ini.has_key? section.to_s end |
#restore ⇒ Object
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.
269 270 271 |
# File 'lib/inifile.rb', line 269 def restore parse end |
#sections ⇒ Object
call-seq:
sections
Returns an array of the section names.
184 185 186 |
# File 'lib/inifile.rb', line 184 def sections @ini.keys end |
#taint ⇒ Object
call-seq:
taint
Marks the INI file as tainted – this will traverse each section marking each section as tainted as well.
209 210 211 212 213 214 |
# File 'lib/inifile.rb', line 209 def taint super @ini.each_value {|h| h.taint} @ini.taint self end |
#to_h ⇒ Object
call-seq:
to_h
Convert IniFile to hash format.
100 101 102 |
# File 'lib/inifile.rb', line 100 def to_h @ini.dup end |
#to_s ⇒ Object
call-seq:
to_s
Convert IniFile to text format.
84 85 86 87 88 89 90 91 92 |
# File 'lib/inifile.rb', line 84 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) ⇒ 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.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/inifile.rb', line 64 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 |