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



39
40
41
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 39

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

#debugObject



43
44
45
46
47
48
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 43

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

  log.json data
end

#loadObject



26
27
28
29
30
31
32
33
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 26

def load
  return JSON.parse(File.read(config_path)) if File.exist?(config_path)

  default_data
rescue JSON::ParserError
  # log.exception e
  default_data
end

#nameObject



35
36
37
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 35

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

#saveObject



22
23
24
# File 'lib/appydave/tools/configuration/models/config_base.rb', line 22

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