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