Class: CLI::Kit::Ini

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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

Methods included from T::Sig

sig

Constructor Details

#initialize(path = nil, config: nil, default_section: '[global]') ⇒ Ini

Returns a new instance of Ini.



28
29
30
31
32
33
34
35
36
# File 'lib/cli/kit/ini.rb', line 28

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

Returns the value of attribute ini.



23
24
25
# File 'lib/cli/kit/ini.rb', line 23

def ini
  @ini
end

Instance Method Details

#git_formatObject



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

def git_format
  to_ini(git_format: true)
end

#parseObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cli/kit/ini.rb', line 39

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



62
63
64
# File 'lib/cli/kit/ini.rb', line 62

def to_s
  to_ini
end