class Idcf::Cli::Lib::Util::IniConf

ini conf

Attributes

load_data[R]
read_only[R]
tmp_data[R]

Public Class Methods

new(*path) click to toggle source

initialize

@param path [String] ini file path

# File lib/idcf/cli/lib/util/ini_conf.rb, line 14
def initialize(*path)
  @tmp_data  = {}
  @load_data = IniFile.load(path[0])
  @read_only = false
end

Public Instance Methods

[](profile) click to toggle source
# File lib/idcf/cli/lib/util/ini_conf.rb, line 24
def [](profile)
  return nil if load_error?
  @load_data[profile.to_s]
end
[]=(profile, value) click to toggle source
# File lib/idcf/cli/lib/util/ini_conf.rb, line 29
def []=(profile, value)
  data               = load_error? ? @tmp_data : @load_data
  data[profile.to_s] = value
end
find(name, profile) click to toggle source

get config value

@param name [String] @param profile [String] @return String or Hash @raise

# File lib/idcf/cli/lib/util/ini_conf.rb, line 40
def find(name, profile)
  begin
    @load_data[profile].fetch(name)
  rescue StandardError => _e
    @load_data['default'].fetch(name)
  end
rescue StandardError => _e
  msg = "Error: could not read #{profile}:#{name}"
  raise Idcf::Cli::Error::CliError, msg
end
load_error?() click to toggle source
# File lib/idcf/cli/lib/util/ini_conf.rb, line 20
def load_error?
  @load_data.nil?
end
merge!(value) click to toggle source

inifile object merge

@param value [Idcf::Cli::Lib::Util::IniConf] @return self

# File lib/idcf/cli/lib/util/ini_conf.rb, line 68
def merge!(value)
  raise Idcf::Cli::Error::CliError, 'merge error' unless value.class == self.class
  v_data = value.load_data
  return self if v_data.nil?
  @load_data.merge!(v_data)
  @read_only = true
  self
end
write(path) click to toggle source

write

@param path [String]

# File lib/idcf/cli/lib/util/ini_conf.rb, line 54
def write(path)
  raise Idcf::Cli::Error::CliError, 'read only object' if @read_only
  @load_data = load_create_file(path) unless File.exist?(path)

  @tmp_data.each do |k, v|
    @load_data[k] = v
  end
  @load_data.write(filename: path)
end

Protected Instance Methods

load_create_file(path) click to toggle source
# File lib/idcf/cli/lib/util/ini_conf.rb, line 79
def load_create_file(path)
  dir_path = File.dirname(path)
  FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)
  File.open(path, 'w').close
  IniFile.load(path)
end