Class: Idcf::Cli::Lib::Util::IniConf

Inherits:
Object
  • Object
show all
Defined in:
lib/idcf/cli/lib/util/ini_conf.rb

Overview

ini conf

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*path) ⇒ IniConf

initialize

Parameters:

  • path (String)

    ini file path



14
15
16
17
18
# 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

Instance Attribute Details

#load_dataObject (readonly)

Returns the value of attribute load_data.



9
10
11
# File 'lib/idcf/cli/lib/util/ini_conf.rb', line 9

def load_data
  @load_data
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



9
10
11
# File 'lib/idcf/cli/lib/util/ini_conf.rb', line 9

def read_only
  @read_only
end

#tmp_dataObject (readonly)

Returns the value of attribute tmp_data.



9
10
11
# File 'lib/idcf/cli/lib/util/ini_conf.rb', line 9

def tmp_data
  @tmp_data
end

Instance Method Details

#[](profile) ⇒ Object



24
25
26
27
# 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) ⇒ Object



29
30
31
32
# 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) ⇒ Object

get config value

Parameters:

  • name (String)
  • profile (String)

Returns:

  • String or Hash

Raises:



40
41
42
43
44
45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/idcf/cli/lib/util/ini_conf.rb', line 20

def load_error?
  @load_data.nil?
end

#merge!(value) ⇒ Object

inifile object merge

Parameters:

Returns:

  • self

Raises:



68
69
70
71
72
73
74
75
# 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) ⇒ Object

write

Parameters:

  • path (String)

Raises:



54
55
56
57
58
59
60
61
62
# 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