Class: DataCollector::ConfigFile
- Inherits:
-
Object
- Object
- DataCollector::ConfigFile
- Defined in:
- lib/data_collector/config_file.rb
Class Method Summary collapse
- .[](key) ⇒ Object
- .[]=(key, value) ⇒ Object
- .dig(*args) ⇒ Object
- .include?(key) ⇒ Boolean
- .interpret_yaml_with_expressions(yaml_file, variables = {}) ⇒ Object
- .keys ⇒ Object
- .name ⇒ Object
- .name=(config_file_name) ⇒ Object
- .path ⇒ Object
- .path=(config_file_path) ⇒ Object
- .raw ⇒ Object
- .version ⇒ Object
Class Method Details
.[](key) ⇒ Object
33 34 35 36 |
# File 'lib/data_collector/config_file.rb', line 33 def self.[](key) init @config[key] end |
.[]=(key, value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/data_collector/config_file.rb', line 38 def self.[]=(key, value) init @config[key] = value File.open("#{path}/#{@config_file_name}", 'w') do |f| f.puts @config.to_yaml end end |
.dig(*args) ⇒ Object
61 62 63 64 |
# File 'lib/data_collector/config_file.rb', line 61 def self.dig(*args) init @config.dig(*args) end |
.include?(key) ⇒ Boolean
46 47 48 49 |
# File 'lib/data_collector/config_file.rb', line 46 def self.include?(key) init @config.include?(key) end |
.interpret_yaml_with_expressions(yaml_file, variables = {}) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/data_collector/config_file.rb', line 111 def self.interpret_yaml_with_expressions(yaml_file, variables = {}) # Read the YAML file content content = File.read(yaml_file) # Process any :key: "${expression}" patterns processed_content = content.gsub(/:key:\s*"?\$\{([^}]+)\}"?/) do |match| expression = $1.strip # Evaluate the expression using the provided variables if variables.key?(expression) # Replace with the variable value match.gsub("${#{expression}}", variables[expression].to_s) elsif expression.include?('.') # Handle dot notation for nested variables parts = expression.split('.') value = variables parts.each do |part| value = value[part.to_sym] if value.is_a?(Hash) && value.key?(part.to_sym) end value.to_s else # Keep the original if we can't evaluate match end end # Parse the processed YAML content YAML.load(processed_content, aliases: true, permitted_classes: [Time, Symbol]) end |
.keys ⇒ Object
51 52 53 54 |
# File 'lib/data_collector/config_file.rb', line 51 def self.keys init @config.keys end |
.name ⇒ Object
16 17 18 |
# File 'lib/data_collector/config_file.rb', line 16 def self.name @config_file_name end |
.name=(config_file_name) ⇒ Object
20 21 22 |
# File 'lib/data_collector/config_file.rb', line 20 def self.name=(config_file_name) @config_file_name = config_file_name end |
.path ⇒ Object
24 25 26 27 |
# File 'lib/data_collector/config_file.rb', line 24 def self.path init if @config_file_path.empty? @config_file_path end |
.path=(config_file_path) ⇒ Object
29 30 31 |
# File 'lib/data_collector/config_file.rb', line 29 def self.path=(config_file_path) @config_file_path = config_file_path end |
.raw ⇒ Object
56 57 58 59 |
# File 'lib/data_collector/config_file.rb', line 56 def self.raw init @config end |
.version ⇒ Object
12 13 14 |
# File 'lib/data_collector/config_file.rb', line 12 def self.version '0.0.4' end |