lib/signore/repo.rb in signore-0.4.1 vs lib/signore/repo.rb in signore-0.4.2

- old
+ new

@@ -1,6 +1,5 @@ -require 'fileutils' require 'yaml/store' require_relative 'mapper' require_relative 'settings' require_relative 'sig_finder' require_relative 'signature' @@ -9,12 +8,10 @@ module Signore class Repo def initialize(path: Settings.new.repo_path, sig_finder: SigFinder) @path = path @sig_finder = sig_finder - initialise_store if path.zero? or not path.exist? - @store = YAML::Store.new(path) end def <<(sig) sigs << sig persist @@ -23,26 +20,28 @@ def find(tags: Tags.new) sig_finder.find(sigs, tags: tags) end def sigs - @sigs ||= store.transaction(true) { store['signatures'] }.map do |elem| - elem.is_a?(Signature) ? elem : Mapper.from_h(elem) + @sigs ||= begin + elems = store.transaction(true) { store.fetch('signatures', []) } + elems.map { |elem| elem.is_a?(Signature) ? elem : Mapper.from_h(elem) } end end - private_attr_reader :path, :sig_finder, :store + private_attr_reader :path, :sig_finder private - def initialise_store - FileUtils.mkdir_p path.dirname - FileUtils.touch path - YAML::Store.new(path).transaction { |store| store['signatures'] = [] } - end - def persist hashes = sigs.map { |sig| Mapper.to_h(sig) } store.transaction { store['signatures'] = hashes } + end + + def store + @store ||= begin + path.dirname.mkpath + YAML::Store.new(path) + end end end end