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.57.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.13.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.40.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.56.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.39.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.55.0 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.54.2 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.54.1 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.38.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.54.0 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.53.0 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.37.0 lib/eac_ruby_utils/configs/file.rb
avm-tools-0.52.0 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
avm-tools-0.51.0 vendor/gems/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.36.1 lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.12.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
ehbrs-tools-0.11.0 vendor/eac_ruby_utils/lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.36.0 lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.35.0 lib/eac_ruby_utils/configs/file.rb
eac_ruby_utils-0.34.0 lib/eac_ruby_utils/configs/file.rb