Class: Dotini::IniFile
- Inherits:
-
Object
- Object
- Dotini::IniFile
- Defined in:
- lib/dotini/ini_file.rb
Overview
Representation of a INI file
Constant Summary collapse
- DEFAULT_KEY_PATTERN =
/[^=\s]+/.freeze
- DEFAULT_VALUE_PATTERN =
/(?:"[^"]*"|[^=\s]*)/.freeze
Instance Attribute Summary collapse
-
#sections ⇒ Object
Returns the value of attribute sections.
Class Method Summary collapse
-
.load(filename, comment_character: ';', key_pattern: DEFAULT_KEY_PATTERN, value_pattern: DEFAULT_VALUE_PATTERN) ⇒ Object
Loads an INI file by name The options are: - comment_character: which character is used for comments - key_pattern: a regexp that matches the property keys - value_pattern: a regexp that matches the property values.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Retrieves an existing section, or creates a new one.
-
#initialize ⇒ IniFile
constructor
Creates a new, empty INI file.
-
#to_h ⇒ Object
Represents the current INI file as a hash.
-
#to_s ⇒ Object
Represents the current INI file as a string.
- #write(io_stream) ⇒ Object
Constructor Details
#initialize ⇒ IniFile
Creates a new, empty INI file
12 13 14 |
# File 'lib/dotini/ini_file.rb', line 12 def initialize @sections = [] end |
Instance Attribute Details
#sections ⇒ Object
Returns the value of attribute sections.
9 10 11 |
# File 'lib/dotini/ini_file.rb', line 9 def sections @sections end |
Class Method Details
.load(filename, comment_character: ';', key_pattern: DEFAULT_KEY_PATTERN, value_pattern: DEFAULT_VALUE_PATTERN) ⇒ Object
Loads an INI file by name The options are:
-
comment_character: which character is used for comments
-
key_pattern: a regexp that matches the property keys
-
value_pattern: a regexp that matches the property values
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/dotini/ini_file.rb', line 54 def load(filename, comment_character: ';', key_pattern: DEFAULT_KEY_PATTERN, value_pattern: DEFAULT_VALUE_PATTERN) line_pattern = /\A(?<key>#{key_pattern}) \s*=\s* (?<value>#{value_pattern}) (?:\s*(?<inline_comment>#{comment_character}.*))?\z/x ini_file = IniFile.new current_section = Section.new(nil) current_key_value_pair = KeyValuePair.new File.open(filename, 'r') do |f| f.each_line(chomp: true) do |line| line.strip! if line.start_with?(comment_character) current_key_value_pair.prepended_comments << line elsif (match = line.match(/\A\[(?<section_name>[^\]]+)\]\z/)) current_section.key_value_pairs << current_key_value_pair ini_file.sections << current_section current_section = Section.new(match['section_name']) current_key_value_pair = KeyValuePair.new elsif (match = line.match(line_pattern)) current_key_value_pair.key = match['key'] current_key_value_pair.value = match['value'] current_key_value_pair.inline_comment = match['inline_comment'] current_section.key_value_pairs << current_key_value_pair current_key_value_pair = KeyValuePair.new end end end current_section.key_value_pairs << current_key_value_pair ini_file.sections << current_section ini_file end |
Instance Method Details
#[](name) ⇒ Object
Retrieves an existing section, or creates a new one
17 18 19 20 |
# File 'lib/dotini/ini_file.rb', line 17 def [](name) sections.find { |section| section.name == name } || Section.new(name).tap { |section| sections << section } end |
#to_h ⇒ Object
Represents the current INI file as a hash
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/dotini/ini_file.rb', line 23 def to_h {}.tap do |hash| sections.each do |section| section.to_h.then do |section_hash| next if section_hash.empty? (hash[section.name] ||= {}).merge! section_hash end end end end |
#to_s ⇒ Object
Represents the current INI file as a string
36 37 38 39 40 41 42 |
# File 'lib/dotini/ini_file.rb', line 36 def to_s buffer = StringIO.new sections.each do |section| buffer << section.to_s end buffer.string end |
#write(io_stream) ⇒ Object
44 45 46 |
# File 'lib/dotini/ini_file.rb', line 44 def write(io_stream) io_stream.write(to_s) end |