Class: CLI::Kit::Ini
- Inherits:
-
Object
- Object
- CLI::Kit::Ini
- Defined in:
- lib/cli/kit/ini.rb
Overview
INI is a language similar to JSON or YAML, but simplied The spec is here: en.wikipedia.org/wiki/INI_file This parser includes supports for 2 very basic uses
-
Sections
-
Key Value Pairs (within and outside of the sections)
- global
-
key = val
Nothing else is supported right now See the ini_test.rb file for more examples
Instance Attribute Summary collapse
-
#ini ⇒ Object
Returns the value of attribute ini.
Instance Method Summary collapse
- #git_format ⇒ Object
-
#initialize(path = nil, config: nil, default_section: nil, convert_types: true) ⇒ Ini
constructor
A new instance of Ini.
- #parse ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path = nil, config: nil, default_section: nil, convert_types: true) ⇒ Ini
Returns a new instance of Ini.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cli/kit/ini.rb', line 18 def initialize(path = nil, config: nil, default_section: nil, convert_types: true) @config = if path && File.exist?(path) File.readlines(path) elsif config config.lines end @ini = {} @current_key = nil @default_section = default_section @convert_types = convert_types end |
Instance Attribute Details
#ini ⇒ Object
Returns the value of attribute ini.
16 17 18 |
# File 'lib/cli/kit/ini.rb', line 16 def ini @ini end |
Instance Method Details
#git_format ⇒ Object
53 54 55 |
# File 'lib/cli/kit/ini.rb', line 53 def git_format to_ini(@ini, git_format: true).flatten.join("\n") end |
#parse ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cli/kit/ini.rb', line 30 def parse return @ini if @config.nil? @config.each do |l| l.strip! # If section, then set current key, this will nest the setting if section_designator?(l) @current_key = l # A new line will reset the current key elsif l.strip.empty? @current_key = nil # Otherwise set the values else k, v = l.split('=', 2).map(&:strip) set_val(k, v) end end @ini end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/cli/kit/ini.rb', line 57 def to_s to_ini(@ini).flatten.join("\n") end |