Sha256: 6aa22e7b2834912a3f8af869e2f238400ac17c4dd41accafecb2e16824e371fc

Contents?: true

Size: 1.58 KB

Versions: 29

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string'
require 'yaml'
require 'eac_ruby_utils/configs/file'
require 'eac_ruby_utils/patches/hash/sym_keys_hash'
require 'eac_ruby_utils/paths_hash'
require 'eac_ruby_utils/simple_cache'

module EacRubyUtils
  class Configs
    include ::EacRubyUtils::SimpleCache

    attr_reader :configs_key, :options

    # Valid options: [:storage_path]
    def initialize(configs_key, options = {})
      @configs_key = configs_key
      @options = options.to_sym_keys_hash.freeze
      load
    end

    def clear
      file.clear
    end

    def save
      file.save
    end

    def load
      file.load
    end

    def []=(entry_key, entry_value)
      write_entry(entry_key, entry_value)
    end

    def write_entry(entry_key, entry_value)
      file.write(entry_key, entry_value)
    end

    def [](entry_key)
      read_entry(entry_key)
    end

    def read_entry(entry_key)
      file.read(entry_key)
    end

    def autosave?
      file.autosave?
    end

    private

    attr_accessor :data

    def file_uncached
      ::EacRubyUtils::Configs::File.new(
        storage_path, options
      )
    end

    def storage_path_uncached
      path = options_storage_path || default_storage_path
      return path if ::File.exist?(path) && ::File.size(path).positive?

      ::FileUtils.mkdir_p(::File.dirname(path))
      ::File.write(path, {}.to_yaml)
      path
    end

    def options_storage_path
      options[:storage_path]
    end

    def default_storage_path
      ::File.join(ENV['HOME'], '.config', configs_key, 'settings.yml')
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
ehbrs-tools-0.11.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.34.0 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.33.1 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.33.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.10.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.32.1 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.32.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.9.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.31.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.7.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.30.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.6.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.29.0 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.28.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.5.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.27.0 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.26.0 lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.25.0 lib/eac_ruby_utils/configs.rb
ehbrs-tools-0.3.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb
eac_ruby_utils-0.24.1 lib/eac_ruby_utils/configs.rb