Class: Appydave::Tools::Configuration::Models::ConfigBase

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/appydave/tools/configuration/models/config_base.rb

Overview

Base class for handling common configuration tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigBase

Returns a new instance of ConfigBase.



16
17
18
19
20
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 16

def initialize
  @config_path = File.join(Config.config_path, "#{config_name}.json")
  # puts "Config path: #{config_path}"
  @data = load
end

Instance Attribute Details

#config_pathObject (readonly)

Returns the value of attribute config_path.



14
15
16
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 14

def config_path
  @config_path
end

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 14

def data
  @data
end

Instance Method Details

#config_nameObject



65
66
67
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 65

def config_name
  name.gsub(/([a-z])([A-Z])/, '\1-\2').downcase
end

#debugObject



69
70
71
72
73
74
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 69

def debug
  log.kv 'Config', name
  log.kv 'Path', config_path

  log.json data
end

#loadObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 32

def load
  if debug_mode?
    log.info "Loading config: #{config_name}"
    log.info "Config path: #{config_path}"
    log.info "File exists: #{File.exist?(config_path)}"
  end

  unless File.exist?(config_path)
    log.warn "Config file not found: #{config_path}" if debug_mode?
    return default_data
  end

  content = File.read(config_path)
  log.info "Config file size: #{content.bytesize} bytes" if debug_mode?

  data = JSON.parse(content)
  log.info "Config loaded successfully: #{config_name}" if debug_mode?
  log.json data if debug_mode?
  data
rescue JSON::ParserError => e
  log.error "JSON parse error in #{config_path}: #{e.message}"
  log.error "File content preview: #{content[0..200]}" if content
  default_data
rescue StandardError => e
  log.error "Error loading #{config_path}: #{e.message}"
  log.error e.backtrace.first(3).join("\n") if debug_mode?
  default_data
end

#nameObject



61
62
63
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 61

def name
  self.class.name.split('::')[-1].gsub(/Config$/, '')
end

#saveObject



22
23
24
25
26
27
28
29
30
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 22

def save
  # Create backup if file exists (silent for self-healing operations)
  if File.exist?(config_path)
    backup_path = "#{config_path}.backup.#{Time.now.strftime('%Y%m%d-%H%M%S')}"
    FileUtils.cp(config_path, backup_path)
  end

  File.write(config_path, JSON.pretty_generate(data))
end