Class: CLI::Kit::Ini

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#iniObject

: Hash[String, Hash[String, String]]



21
22
23
# File 'lib/cli/kit/ini.rb', line 21

def ini
  @ini
end

Instance Method Details

#git_formatObject

: -> String



53
54
55
# File 'lib/cli/kit/ini.rb', line 53

def git_format
  to_ini(git_format: true)
end

#parseObject

: -> 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_sObject

: -> String



58
59
60
# File 'lib/cli/kit/ini.rb', line 58

def to_s
  to_ini
end