Sha256: a91e1f55e06c40dd948fd83c51b58d844c3bdf0a5cf7915f1ac04d99a6a2e075

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 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")
            @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

5 entries across 5 versions & 1 rubygems

Version Path
appydave-tools-0.9.2 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.9.1 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.9.0 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.8.0 lib/appydave/tools/configuration/models/config_base.rb
appydave-tools-0.7.0 lib/appydave/tools/configuration/models/config_base.rb