class CLI::Kit::Ini
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
Attributes
ini[RW]
Public Class Methods
new(path = nil, config: nil, default_section: nil, convert_types: true)
click to toggle source
# 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
Public Instance Methods
git_format()
click to toggle source
# File lib/cli/kit/ini.rb, line 53 def git_format to_ini(@ini, git_format: true).flatten.join("\n") end
parse()
click to toggle source
# 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()
click to toggle source
# File lib/cli/kit/ini.rb, line 57 def to_s to_ini(@ini).flatten.join("\n") end
Private Instance Methods
section_designator?(k)
click to toggle source
# File lib/cli/kit/ini.rb, line 97 def section_designator?(k) k.start_with?('[') && k.end_with?(']') end
set_val(key, val)
click to toggle source
# File lib/cli/kit/ini.rb, line 78 def set_val(key, val) return if key.nil? && val.nil? current_key = @current_key || @default_section if current_key @ini[current_key] ||= {} @ini[current_key][key] = typed_val(val) else @ini[key] = typed_val(val) end end
to_ini(h, git_format: false)
click to toggle source
# File lib/cli/kit/ini.rb, line 63 def to_ini(h, git_format: false) optional_tab = git_format ? "\t" : "" str = [] h.each do |k, v| if section_designator?(k) str << "" unless str.empty? || git_format str << k str << to_ini(v, git_format: git_format) else str << "#{optional_tab}#{k} = #{v}" end end str end
typed_val(val)
click to toggle source
# File lib/cli/kit/ini.rb, line 90 def typed_val(val) return val.to_s unless @convert_types return val.to_i if val =~ /^-?[0-9]+$/ return val.to_f if val =~ /^-?[0-9]+\.[0-9]*$/ val.to_s end