Sha256: 281f90f732ee870262fcbd8816b0a0026710e884052cf7092ce2beda9f1ea2ae

Contents?: true

Size: 1.39 KB

Versions: 87

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string'
require 'yaml'
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
    class File
      include ::EacRubyUtils::SimpleCache

      attr_reader :path, :options

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

      def clear
        self.data = ::EacRubyUtils::PathsHash.new({})
      end

      def save
        ::FileUtils.mkdir_p(::File.dirname(path))
        ::File.write(path, data.to_h.to_yaml)
      end

      def load
        self.data = if ::File.exist?(path) && ::File.size(path).positive?
                      ::EacRubyUtils::PathsHash.new(YAML.load_file(path))
                    else
                      {}
                    end
      end

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

      def write(entry_key, entry_value)
        data[entry_key] = entry_value
        save if autosave?
      end

      def [](entry_key)
        read(entry_key)
      end

      def read(entry_key)
        data[entry_key]
      end

      def autosave?
        options[:autosave] ? true : false
      end

      private

      attr_accessor :data
    end
  end
end

Version data entries

87 entries across 87 versions & 3 rubygems

Version Path
avm-tools-0.72.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.47.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.71.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.70.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.15.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.14.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.70.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.70.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.69.3 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.14.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.69.2 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.69.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.46.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.69.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.45.2 lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.45.1 lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.45.0 lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.13.1 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.68.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.44.0 lib/eac_ruby_utils/configs/file.rb