Class: IniFile
Overview
and write INI files.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'2.0.2'
Instance Attribute Summary collapse
-
#encoding ⇒ Object
Get and set the encoding (Ruby 1.9).
-
#escape ⇒ Object
Enable or disable character escaping.
-
#filename ⇒ Object
Get and set the filename.
Class Method Summary collapse
-
.load(filename, opts = {}) ⇒ Object
Public: Open an INI file and load the contents.
Instance Method Summary collapse
-
#[](section) ⇒ Object
Public: Get the section Hash by name.
-
#[]=(section, value) ⇒ Object
Public: Set the section to a hash of parameter/value pairs.
-
#clone ⇒ Object
Public: Produces a duplicate of this IniFile.
-
#delete_section(section) ⇒ Object
Public: Remove a section identified by name from the IniFile.
-
#dup ⇒ Object
Public: Produces a duplicate of this IniFile.
-
#each ⇒ Object
Public: Yield each INI file section, parameter, and value in turn to the given block.
-
#each_section ⇒ Object
Public: Yield each section in turn to the given block.
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Compare this IniFile to some other IniFile.
-
#freeze ⇒ Object
Public: Freeze the state of this IniFile object.
-
#has_section?(section) ⇒ Boolean
Public: Check to see if the IniFile contains the section.
-
#initialize(content = nil, opts = {}) ⇒ IniFile
constructor
Public: Create a new INI file from the given content String which contains the INI file lines.
-
#match(regex) ⇒ Object
Public: Create a Hash containing only those INI file sections whose names match the given regular expression.
-
#merge(other) ⇒ Object
Public: Creates a copy of this inifile with the entries from the other_inifile merged into the copy.
-
#merge!(other) ⇒ Object
Public: Merges other_inifile into this inifile, overwriting existing entries.
-
#read(opts = {}) ⇒ Object
(also: #restore)
Public: Read the contents of the INI file from the file system and replace and set the state of this IniFile instance.
-
#sections ⇒ Object
Returns an Array of section names contained in this IniFile.
-
#taint ⇒ Object
Public: Mark this IniFile as tainted – this will traverse each section marking each as tainted.
-
#to_h ⇒ Object
Returns this IniFile converted to a Hash.
-
#to_s ⇒ Object
Returns this IniFile converted to a String.
-
#write(opts = {}) ⇒ Object
(also: #save)
Public: Write the contents of this IniFile to the file system.
Constructor Details
#initialize(content = nil, opts = {}) ⇒ IniFile
Public: Create a new INI file from the given content String which contains the INI file lines. If the content are omitted, then the :filename option is used to read in the content of the INI file. If neither the content for a filename is provided then an empty INI file is created.
content - The String containing the INI file contents opts - The Hash of options (default: {})
:comment - String containing the comment character(s)
:parameter - String used to separate parameter and value
:encoding - Encoding String for reading / writing (Ruby 1.9)
:escape - Boolean used to control character escaping
:default - The String name of the default global section
:filename - The filename as a String
Examples
IniFile.new
#=> an empty IniFile instance
IniFile.new( "[global]\nfoo=bar" )
#=> an IniFile instance
IniFile.new( :filename => 'file.ini', :encoding => 'UTF-8' )
#=> an IniFile instance
IniFile.new( "[global]\nfoo=bar", :comment => '#' )
#=> an IniFile instance
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/inifile.rb', line 78 def initialize( content = nil, opts = {} ) opts, content = content, nil if Hash === content @content = content @comment = opts.fetch(:comment, ';#') @param = opts.fetch(:parameter, '=') @encoding = opts.fetch(:encoding, nil) @escape = opts.fetch(:escape, true) @default = opts.fetch(:default, 'global') @filename = opts.fetch(:filename, nil) @ini = Hash.new {|h,k| h[k] = Hash.new} if @content then parse! elsif @filename then read end end |
Instance Attribute Details
#encoding ⇒ Object
Get and set the encoding (Ruby 1.9)
44 45 46 |
# File 'lib/inifile.rb', line 44 def encoding @encoding end |
#escape ⇒ Object
Enable or disable character escaping
47 48 49 |
# File 'lib/inifile.rb', line 47 def escape @escape end |
#filename ⇒ Object
Get and set the filename
41 42 43 |
# File 'lib/inifile.rb', line 41 def filename @filename end |
Class Method Details
.load(filename, opts = {}) ⇒ Object
Public: Open an INI file and load the contents.
filename - The name of the fiel as a String opts - The Hash of options (default: {})
:comment - String containing the comment character(s)
:parameter - String used to separate parameter and value
:encoding - Encoding String for reading / writing (Ruby 1.9)
:escape - Boolean used to control character escaping
:default - The String name of the default global section
Examples
IniFile.load('file.ini')
#=> IniFile instance
IniFile.load('does/not/exist.ini')
#=> nil
Returns an IniFile intsnace or nil if the file could not be opened.
35 36 37 38 |
# File 'lib/inifile.rb', line 35 def self.load( filename, opts = {} ) return unless File.file? filename new(opts.merge(:filename => filename)) end |
Instance Method Details
#[](section) ⇒ Object
Public: Get the section Hash by name. If the section does not exist, then it will be created.
section - The section name as a String.
Examples
inifile['global']
#=> global section Hash
Returns the Hash of parameter/value pairs for this section.
278 279 280 281 |
# File 'lib/inifile.rb', line 278 def []( section ) return nil if section.nil? @ini[section.to_s] end |
#[]=(section, value) ⇒ Object
Public: Set the section to a hash of parameter/value pairs.
section - The section name as a String. value - The Hash of parameter/value pairs.
Examples
inifile['tenderloin'] = { 'gritty' => 'yes' }
#=> { 'gritty' => 'yes' }
Returns the value Hash.
295 296 297 |
# File 'lib/inifile.rb', line 295 def []=( section, value ) @ini[section.to_s] = value end |
#clone ⇒ Object
Public: Produces a duplicate of this IniFile. The duplicate is independent of the original – i.e. the duplicate can be modified without changing the original. The tainted state and the frozen state of the original is copied to the duplicate.
Returns a new IniFile.
377 378 379 380 381 |
# File 'lib/inifile.rb', line 377 def clone other = dup other.freeze if self.frozen? other end |
#delete_section(section) ⇒ Object
Public: Remove a section identified by name from the IniFile.
section - The section name as a String.
Returns the deleted section Hash.
262 263 264 |
# File 'lib/inifile.rb', line 262 def delete_section( section ) @ini.delete section.to_s end |
#dup ⇒ Object
Public: Produces a duplicate of this IniFile. The duplicate is independent of the original – i.e. the duplicate can be modified without changing the original. The tainted state of the original is copied to the duplicate.
Returns a new IniFile.
362 363 364 365 366 367 368 |
# File 'lib/inifile.rb', line 362 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
Public: Yield each INI file section, parameter, and value in turn to the given block.
block - The block that will be iterated by the each method. The block will
be passed the current section and the parameter / value pair.
Examples
inifile.each do |section, parameter, value|
puts "#{parameter} = #{value} [in section - #{section}]"
end
Returns this IniFile.
227 228 229 230 231 232 233 234 235 |
# File 'lib/inifile.rb', line 227 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
Public: Yield each section in turn to the given block.
block - The block that will be iterated by the each method. The block will
be passed the current section as a Hash.
Examples
inifile.each_section do |section|
puts section.inspect
end
Returns this IniFile.
250 251 252 253 254 |
# File 'lib/inifile.rb', line 250 def each_section return unless block_given? @ini.each_key {|section| yield section} self end |
#eql?(other) ⇒ Boolean Also known as: ==
Public: Compare this IniFile to some other IniFile. For two INI files to be equivalent, they must have the same sections with the same parameter / value pairs in each section.
other - The other IniFile.
Returns true if the INI files are equivalent and false if they differ.
391 392 393 394 395 |
# File 'lib/inifile.rb', line 391 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
Public: Freeze the state of this IniFile object. Any attempts to change the object will raise an error.
Returns this IniFile.
337 338 339 340 341 342 |
# File 'lib/inifile.rb', line 337 def freeze super @ini.each_value {|h| h.freeze} @ini.freeze self end |
#has_section?(section) ⇒ Boolean
Public: Check to see if the IniFile contains the section.
section - The section name as a String.
Returns true if the section exists in the IniFile.
322 323 324 |
# File 'lib/inifile.rb', line 322 def has_section?( section ) @ini.has_key? section.to_s end |
#match(regex) ⇒ Object
Public: Create a Hash containing only those INI file sections whose names match the given regular expression.
regex - The Regexp used to match section names.
Examples
inifile.match(/^tree_/)
#=> Hash of matching sections
Return a Hash containing only those sections that match the given regular expression.
312 313 314 |
# File 'lib/inifile.rb', line 312 def match( regex ) @ini.dup.delete_if { |section, _| section !~ regex } end |
#merge(other) ⇒ Object
Public: Creates a copy of this inifile with the entries from the other_inifile merged into the copy.
other - The other IniFile.
Returns a new IniFile.
182 183 184 |
# File 'lib/inifile.rb', line 182 def merge( other ) self.dup.merge!(other) end |
#merge!(other) ⇒ Object
Public: Merges other_inifile into this inifile, overwriting existing entries. Useful for having a system inifile with user over-ridable settings elsewhere.
other - The other IniFile.
Returns this IniFile.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/inifile.rb', line 194 def merge!( other ) my_keys = @ini.keys other_keys = case other when IniFile; other.instance_variable_get(:@ini).keys when Hash; other.keys else raise "cannot merge contents from '#{other.class.name}'" end (my_keys & other_keys).each do |key| @ini[key].merge!(other[key]) end (other_keys - my_keys).each do |key| @ini[key] = other[key] end self end |
#read(opts = {}) ⇒ Object Also known as: restore
Public: Read the contents of the INI file from the file system and replace and set the state of this IniFile instance. If left unspecified the currently configured filename and encoding will be used when reading from the file system. Otherwise the filename and encoding can be specified in the options hash.
opts - The default options Hash
:filename - The filename as a String
:encoding - The encoding as a String (Ruby 1.9)
Returns this IniFile instance if the read was successful; nil is returned if the file could not be read.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/inifile.rb', line 139 def read( opts = {} ) filename = opts.fetch(:filename, @filename) encoding = opts.fetch(:encoding, @encoding) return unless File.file? filename mode = (RUBY_VERSION >= '1.9' && encoding) ? "r:#{encoding.to_s}" : 'r' fd = File.open(filename, mode) @content = fd.read parse! self ensure fd.close if fd && !fd.closed? end |
#sections ⇒ Object
Returns an Array of section names contained in this IniFile.
328 329 330 |
# File 'lib/inifile.rb', line 328 def sections @ini.keys end |
#taint ⇒ Object
Public: Mark this IniFile as tainted – this will traverse each section marking each as tainted.
Returns this IniFile.
349 350 351 352 353 354 |
# File 'lib/inifile.rb', line 349 def taint super @ini.each_value {|h| h.taint} @ini.taint self end |
#to_h ⇒ Object
Returns this IniFile converted to a Hash.
171 172 173 |
# File 'lib/inifile.rb', line 171 def to_h @ini.dup end |
#to_s ⇒ Object
Returns this IniFile converted to a String.
159 160 161 162 163 164 165 166 167 |
# File 'lib/inifile.rb', line 159 def to_s s = [] @ini.each do |section,hash| s << "[#{section}]" hash.each {|param,val| s << "#{param} #{@param} #{escape_value val}"} s << "" end s.join("\n") end |
#write(opts = {}) ⇒ Object Also known as: save
Public: Write the contents of this IniFile to the file system. If left unspecified, the currently configured filename and encoding will be used. Otherwise the filename and encoding can be specified in the options hash.
opts - The default options Hash
:filename - The filename as a String
:encoding - The encoding as a String (Ruby 1.9)
Returns this IniFile instance.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/inifile.rb', line 107 def write( opts = {} ) filename = opts.fetch(:filename, @filename) encoding = opts.fetch(:encoding, @encoding) mode = (RUBY_VERSION >= '1.9' && encoding) ? "w:#{encoding.to_s}" : 'w' File.open(filename, mode) do |f| @ini.each do |section,hash| f.puts "[#{section}]" hash.each {|param,val| f.puts "#{param}#{@param}#{escape_value val}"} f.puts end end self end |