Class: Ini
- Inherits:
-
Object
- Object
- Ini
- Defined in:
- lib/bbcloud/vendor/brightbox-ini/lib/ini.rb
Overview
This class represents the INI file and can be used to parse, modify, and write INI files.
Direct Known Subclasses
Defined Under Namespace
Classes: Error
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.
-
#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 = {}) ⇒ Ini
constructor
call-seq: IniFile.new( filename ) IniFile.new( filename, options ).
-
#sections ⇒ Object
call-seq: sections.
-
#taint ⇒ Object
call-seq: taint.
-
#write(filename = nil) ⇒ Object
(also: #save)
call-seq: write write( filename ).
Constructor Details
#initialize(filename, opts = {}) ⇒ Ini
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
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bbcloud/vendor/brightbox-ini/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 = %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
call-seq:
IniFile.load( filename )
IniFile.load( filename, )
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
24 25 26 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 24 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.
125 126 127 128 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 125 def []( section ) return nil if section.nil? @ini[section.to_s] 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.
203 204 205 206 207 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 203 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.
114 115 116 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 114 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.
186 187 188 189 190 191 192 |
# File 'lib/bbcloud/vendor/brightbox-ini/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 ⇒ 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.
83 84 85 86 87 88 89 90 91 |
# File 'lib/bbcloud/vendor/brightbox-ini/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 ⇒ 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.
100 101 102 103 104 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 100 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.
217 218 219 220 221 |
# File 'lib/bbcloud/vendor/brightbox-ini/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 |
#freeze ⇒ Object
call-seq:
freeze
Freeze the state of the IniFile object. Any attempts to change the object will raise an error.
157 158 159 160 161 162 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 157 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.
136 137 138 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 136 def has_section?( section ) @ini.has_key? section.to_s end |
#sections ⇒ Object
call-seq:
sections
Returns an array of the section names.
146 147 148 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 146 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.
171 172 173 174 175 176 |
# File 'lib/bbcloud/vendor/brightbox-ini/lib/ini.rb', line 171 def taint super @ini.each_value {|h| h.taint} @ini.taint self 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.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bbcloud/vendor/brightbox-ini/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 |