Sha256: cc8cb6b0c5958d84870902f580d9abeeb22d56b5d618746f1427b95659950602

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'json'
require 'fileutils'

module Appydave
  module Tools
    module Configuration
      module Models
        # Base class for handling common configuration tasks
        class ConfigBase
          include KLog::Logging

          attr_reader :config_path, :data

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

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

          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

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

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

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

            log.json data
          end

          private

          def default_data
            {}
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
appydave-tools-0.10.2 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.10.1 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.10.0 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.9.5 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.9.4 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.9.3 lib/appydave/tools/configuration/models/config_base.rb