Class: ToolsConfig

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/lib/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ToolsConfig

Returns a new instance of ToolsConfig.



5
6
# File 'lib/lib/config.rb', line 5

def initialize(options = {})
end

Class Method Details

.change_value_in_config(*args) ⇒ Object

Change data in config file in work area

Parameters:

  • args —

    Sequence keys to locate the value in hash



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/lib/config.rb', line 129

def self.change_value_in_config *args
  source = args.extract_first
  value  = args.extract_first
  test_config_type source
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    file = open(source)
    json = file.read
    parsed = JSON.parse(json)
    parsed.nested_set(args, value)
    file.close
    file = open(source, 'w')
    file.write JSON.pretty_generate(parsed)
    file.close
  when :yaml
    file = open(source)
    yaml = file.read
    parsed = YAML.load(yaml)
    parsed.nested_set(args, value)
    file.close
    file = open(source, 'w')
    file.write parsed.to_yaml
    file.close
  end
end

.check_config ⇒ Object



160
161
# File 'lib/lib/config.rb', line 160

def self.check_config
end

.config_backup ⇒ Object



166
167
# File 'lib/lib/config.rb', line 166

def self.config_backup
end

.create_config_file(config_name, config_file, config_type = :json, data = {}) ⇒ Object

Create a Config file in work area

Sample

ToolsConfig.create_config_file 'sample', '/myhome/tools/sample.log' log = ToolsUtil.get_variable "sample_config" => Logger Object

ToolsConfig.create_log_file 'xyko', '/home/francisco/2018/xykotools/tools/xyko.config' config = ToolsConfig.xyko_load ToolsConfig.xyko_add {}

Parameters:

  • config_name
  • config_file
  • config_type (defaults to: :json)
  • data (defaults to: {})

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lib/config.rb', line 26

def self.create_config_file config_name, config_file, config_type = :json, data = {}
  unless File.exists? config_file
    case config_type
    when :json
      config = File.open(config_file, 'w')
      config.write JSON.pretty_generate(data)
      config.close
      ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
      return true
    when :yaml
      config = File.open(config_file, 'w')
      config.write data.to_yaml
      config.close
      ToolsLog.tools_info "Json config file '#{config_file}' was created!'"
      return true
    end
  else
    ToolsLog.tools_warn "The file #{config_file} really exist. leaving operation...."
  end
  ToolsUtil.set_variable "#{config_name}_config_file", config_file
  ToolsUtil.set_variable "#{config_name}_config_type", config_type
end

.daily_backup_config ⇒ Object



169
170
# File 'lib/lib/config.rb', line 169

def self.daily_backup_config
end

.insert_in_config(source, command) ⇒ Object

Merge data in config file in work area

Parameters:

  • source

Returns:

  • content



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lib/config.rb', line 104

def self.insert_in_config source, command
  test_config_type source
  file = open(source)
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    json = file.read
    parsed = JSON.parse(json)
    parsed.rmerge!(command)
    file.close
    file = open(source, 'w')
    file.write JSON.pretty_generate(parsed)
    file.close
  when :yaml
    yaml = file.read
    parsed = YAML.load(yaml)
    parsed.rmerge!(command)
    file.close
    file = open(source, 'w')
    file.write parsed.to_yaml
    file.close
  end
end

.load_config(source) ⇒ Object

Load a content from a config file in work area

Parameters:

  • source

Returns:

  • content



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lib/config.rb', line 84

def self.load_config source
  test_config_type source
  case ToolsUtil.get_variable 'config_file_type'
  when :json
    file    = open(source)
    json    = file.read
    parsed  = JSON.parse(json)
    ToolsUtil.set_variable 'config_data', parsed
    return parsed
  when :yaml
    file    = open(source)
    parsed = YAML.load(file.read)
    ToolsUtil.set_variable 'config_data', parsed
    return parsed
  end
end

.method_missing(method, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/lib/config.rb', line 50

def self.method_missing(method, *args, &block)
  #expected call format =>    STRING_LOGGER_TYPE + '_' + LOGGER_TYPE
  # Ex.:  tools_info
  config_name   = method.to_s.split('_').first
  config_method = method.to_s.split('_').last
  config_file   = ToolsUtil.get_variable "#{config_name}_config_file"
  config_type   = ToolsUtil.get_variable "#{config_name}_config_type"
end

.purge_backup_config ⇒ Object



163
164
# File 'lib/lib/config.rb', line 163

def self.purge_backup_config
end

.test_config_type(source) ⇒ Object

Test and register a config file in work area

Parameters:

  • source

Returns:

  • boolean



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lib/config.rb', line 63

def self.test_config_type source
  file        = open(source)
  content     = file.read
  if ToolsUtil.valid_json? content
    config_type = :json
  else
    if ToolsUtil.valid_yaml? content
      config_type = :yaml
    else
      config_type = :invalid
      ToolsLog.tools_error "Invalid format in config file in '#{source}'.", :light_red
      ToolsLog.tools_exit
    end
  end
  ToolsUtil.set_variable 'config_file_type', config_type
end

.validate_config ⇒ Object



157
158
# File 'lib/lib/config.rb', line 157

def self.validate_config
end