lib/metastore/cabinet.rb in metastore-0.3.0 vs lib/metastore/cabinet.rb in metastore-0.4.0
- old
+ new
@@ -2,17 +2,20 @@
require 'pathname'
module Metastore
class Cabinet
- def initialize(file, storage_type: :yaml)
+ def initialize(file, separators: %w(. /), storage_type: :yaml)
@file = Pathname.new(file).expand_path
+ @separators = separators.map { |x| "\\#{x}" }.join('|')
@storage_type = storage_type
end
def get(key)
- split_key(key).inject(contents) { |c, k| c.is_a?(Hash) ? c[k] : nil }
+ split_key(key).inject(contents) do |c, k|
+ c.is_a?(Hash) ? c[k] : nil
+ end
end
def set(key, value)
current_contents = contents
set_key_and_value(current_contents, split_key(key), value)
@@ -25,23 +28,23 @@
def contents
storage.contents || {}
end
- alias_method :[], :get
- alias_method :[]=, :set
+ alias [] :get
+ alias []= :set
private
- attr_reader :file, :storage_type
+ attr_reader :file, :separators, :storage_type
def storage
@store || StorageFactory.from_sym(storage_type).new(file)
end
def split_key(key)
- key.to_s.split('.')
+ key.to_s.split(Regexp.new(separators))
end
def set_key_and_value(input, key, value)
current_key = key.shift.to_s
input[current_key] = {} unless input[current_key]
@@ -73,10 +76,9 @@
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)
+ raise Errors::CabinetCannotSet, e.message
end
-
end
end