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
: Hash[String, Hash[String, String]].
Instance Method Summary collapse
-
#git_format ⇒ Object
: -> String.
-
#initialize(path = nil, config: nil, default_section: '[global]') ⇒ Ini
constructor
: (?String? path, ?config: String?, ?default_section: String) -> void.
-
#parse ⇒ Object
: -> Hash[String, Hash[String, String]].
-
#to_s ⇒ Object
: -> String.
Constructor Details
#initialize(path = nil, config: nil, default_section: '[global]') ⇒ Ini
: (?String? path, ?config: String?, ?default_section: String) -> void
24 25 26 27 28 29 30 31 32 |
# File 'lib/cli/kit/ini.rb', line 24 def initialize(path = nil, config: nil, default_section: '[global]') @config = if path && File.exist?(path) File.readlines(path) elsif config config.lines end @ini = {} @current_key = default_section end |
Instance Attribute Details
#ini ⇒ Object
: Hash[String, Hash[String, String]]
21 22 23 |
# File 'lib/cli/kit/ini.rb', line 21 def ini @ini end |
Instance Method Details
#git_format ⇒ Object
: -> String
53 54 55 |
# File 'lib/cli/kit/ini.rb', line 53 def git_format to_ini(git_format: true) end |
#parse ⇒ Object
: -> Hash[String, Hash[String, String]]
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cli/kit/ini.rb', line 35 def parse return @ini if @config.nil? @config.each do |l| l.strip! if section_designator?(l) @current_key = l else k, v = l.split('=', 2).map(&:strip) set_val(k, v) if k && v end end @ini end |
#to_s ⇒ Object
: -> String
58 59 60 |
# File 'lib/cli/kit/ini.rb', line 58 def to_s to_ini end |