Class: NumberStation::ConfigReader
- Inherits:
-
Object
- Object
- NumberStation::ConfigReader
- Defined in:
- lib/number_station/config_reader.rb
Class Method Summary collapse
- .default_config_path ⇒ Object
- .load_config(config_path) ⇒ Object
- .read_config ⇒ Object
- .sanitize_for_yaml(data) ⇒ Object
- .save_config(config_data = nil) ⇒ Object
- .setup_logger(level) ⇒ Object
- .user_config_path ⇒ Object
Class Method Details
.default_config_path ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/number_station/config_reader.rb', line 43 def self.default_config_path yaml_path = File.join(File.dirname(__FILE__), "../../resources/conf.yaml") json_path = File.join(File.dirname(__FILE__), "../../resources/conf.json") # Prefer YAML, but support JSON for backward compatibility return yaml_path if File.exist?(yaml_path) return json_path if File.exist?(json_path) yaml_path # Default to YAML path end |
.load_config(config_path) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/number_station/config_reader.rb', line 53 def self.load_config(config_path) config_data = if config_path.end_with?('.yaml') || config_path.end_with?('.yml') # Use safe_load with permitted classes to handle Date objects YAML.safe_load(File.read(config_path), permitted_classes: [Date, Time], aliases: true) else require 'json' JSON.parse(File.read(config_path)) end NumberStation.set_data(config_data) setup_logger(config_data["logging"]["level"]) NumberStation.log.debug "Reading in config file: #{config_path}" rescue StandardError => e # Ensure logger exists before trying to log unless NumberStation.log setup_logger(Logger::WARN) end NumberStation.log.error "Failed to load config: #{e.}" raise end |
.read_config ⇒ Object
28 29 30 31 |
# File 'lib/number_station/config_reader.rb', line 28 def self.read_config config_path = user_config_path || default_config_path load_config(config_path) end |
.sanitize_for_yaml(data) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/number_station/config_reader.rb', line 103 def self.sanitize_for_yaml(data) case data when Hash data.each_with_object({}) do |(key, value), result| result[key] = sanitize_for_yaml(value) end when Array data.map { |item| sanitize_for_yaml(item) } when Date, Time data.to_s else data end end |
.save_config(config_data = nil) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/number_station/config_reader.rb', line 79 def self.save_config(config_data = nil) config_data ||= NumberStation.data config_path = user_config_path || File.join(Dir.home, "number_station", "conf.yaml") # Ensure directory exists FileUtils.mkdir_p(File.dirname(config_path)) # Convert Date/Time objects to strings before saving sanitized_data = sanitize_for_yaml(config_data.dup) # Save as YAML File.write(config_path, sanitized_data.to_yaml) NumberStation.set_data(config_data) # Update in-memory data (keep original format) NumberStation.log.debug "Saved config file: #{config_path}" config_path rescue StandardError => e # Ensure logger exists before trying to log unless NumberStation.log setup_logger(Logger::WARN) end NumberStation.log.error "Failed to save config: #{e.}" raise end |
.setup_logger(level) ⇒ Object
74 75 76 77 |
# File 'lib/number_station/config_reader.rb', line 74 def self.setup_logger(level) NumberStation.set_log(Logger.new(STDOUT)) NumberStation.log.level = level end |
.user_config_path ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/number_station/config_reader.rb', line 33 def self.user_config_path yaml_path = File.join(Dir.home, "number_station", "conf.yaml") json_path = File.join(Dir.home, "number_station", "conf.json") # Prefer YAML, but support JSON for backward compatibility return yaml_path if File.exist?(yaml_path) return json_path if File.exist?(json_path) nil end |