Class: ToolsConfig
Class Method Summary collapse
-
.change_value_in_config(*args) ⇒ Object
Change data in config file in work area.
- .check_config ⇒ Object
- .config_backup ⇒ Object
-
.create_config_file(config_name, config_file, config_type = :json, data = {}) ⇒ Object
Create a Config file in work area.
- .daily_backup_config ⇒ Object
-
.insert_in_config(source, command) ⇒ Object
Merge data in config file in work area.
-
.load_config(source) ⇒ Object
Load a content from a config file in work area.
- .method_missing(method, *_args) ⇒ Object
- .purge_backup_config ⇒ Object
-
.test_config_type(source) ⇒ Object
Test and register a config file in work area.
- .validate_config ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ToolsConfig
constructor
A new instance of ToolsConfig.
Constructor Details
#initialize(options = {}) ⇒ ToolsConfig
Returns a new instance of ToolsConfig.
5 |
# File 'lib/lib/config.rb', line 5 def initialize( = {}); end |
Class Method Details
.change_value_in_config(*args) ⇒ Object
Change data in config file in work area
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/lib/config.rb', line 110 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 = File.open(source) json = file.read parsed = JSON.parse(json) parsed.nested_set(args, value) file.close file = File.open(source, 'w') file.write JSON.pretty_generate(parsed) file.close when :yaml file = File.open(source) yaml = file.read parsed = YAML.safe_load(yaml, [Symbol]) parsed.nested_set(args, value) file.close file = File.open(source, 'w') file.write parsed.to_yaml file.close end end |
.check_config ⇒ Object
138 |
# File 'lib/lib/config.rb', line 138 def self.check_config; end |
.config_backup ⇒ Object
142 |
# File 'lib/lib/config.rb', line 142 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 {}
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/lib/config.rb', line 23 def self.create_config_file(config_name, config_file, config_type = :json, data = {}) return false if File.exist? config_file case config_type when :json config = File.open(config_file, 'w') config.write JSON.pretty_generate(data) config.close when :yaml config = File.open(config_file, 'w') config.write data.to_yaml config.close end ToolsUtil.set_variable "#{config_name}_config_file", config_file ToolsUtil.set_variable "#{config_name}_config_type", config_type end |
.daily_backup_config ⇒ Object
144 |
# File 'lib/lib/config.rb', line 144 def self.daily_backup_config; end |
.insert_in_config(source, command) ⇒ Object
Merge data in config file in work area
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lib/config.rb', line 85 def self.insert_in_config(source, command) test_config_type source file = 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 = File.open(source, 'w') file.write JSON.pretty_generate(parsed) file.close when :yaml yaml = file.read parsed = YAML.safe_load(yaml, [Symbol]) parsed.rmerge!(command) file.close file = 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lib/config.rb', line 65 def self.load_config(source) test_config_type source case ToolsUtil.get_variable 'config_file_type' when :json file = File.open(source) json = file.read parsed = JSON.parse(json) ToolsUtil.set_variable 'config_data', parsed return parsed when :yaml file = File.open(source) parsed = YAML.safe_load(file.read, [Symbol]) ToolsUtil.set_variable 'config_data', parsed return parsed end end |
.method_missing(method, *_args) ⇒ Object
40 41 42 43 44 |
# File 'lib/lib/config.rb', line 40 def self.method_missing(method, *_args) config_name = method.to_s.split('_').first ToolsUtil.get_variable "#{config_name}_config_file" ToolsUtil.get_variable "#{config_name}_config_type" end |
.purge_backup_config ⇒ Object
140 |
# File 'lib/lib/config.rb', line 140 def self.purge_backup_config; end |
.test_config_type(source) ⇒ Object
Test and register a config file in work area
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lib/config.rb', line 49 def self.test_config_type(source) file = File.open(source) content = file.read config_type = if ToolsUtil.valid_json? content :json elsif ToolsUtil.valid_yaml? content :yaml else :invalid end ToolsUtil.set_variable 'config_file_type', config_type end |
.validate_config ⇒ Object
136 |
# File 'lib/lib/config.rb', line 136 def self.validate_config; end |