Sha256: 75f25021cf2f900d0e6056e334f957a06d745b25bb2f18a542c8eb15b57bb361
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
require 'fileutils' require 'pathname' module Metastore class Cabinet def initialize(file, storage_type: :yaml) @file = Pathname.new(file).expand_path @storage_type = storage_type end def get(key) split_key(key).inject(contents) { |c, k| c.is_a?(Hash) ? c[k] : nil } end def set(key, value) current_contents = contents set_key_and_value(current_contents, split_key(key), value) save!(current_contents) end def clear! save!({}) end def contents storage.contents || {} end alias_method :[], :get alias_method :[]=, :set private attr_reader :file, :storage_type def storage @store || StorageFactory.from_sym(storage_type).new(file) end def split_key(key) key.to_s.split('.') end def set_key_and_value(input, key, value) current_key = key.shift.to_s input[current_key] = {} unless input[current_key] if key.empty? input[current_key] = stringify_keys(value) input else input[current_key] = {} unless input[current_key].is_a?(Hash) set_key_and_value(input[current_key], key, value) end end def stringify_keys(input) case input when Hash Hash[ input.map do |k, v| [ k.respond_to?(:to_s) ? k.to_s : k, stringify_keys(v) ] end ] when Enumerable input.map { |v| stringify_keys(v) } else input.is_a?(Symbol) ? input.to_s : input end end def save!(new_values) FileUtils.mkdir_p(file.dirname) unless file.exist? storage.save!(new_values) true rescue => e raise Errors::CabinetCannotSet.new(e.message) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
metastore-0.3.0 | lib/metastore/cabinet.rb |