lib/signore/repo.rb in signore-0.4.2 vs lib/signore/repo.rb in signore-0.5.0
- old
+ new
@@ -1,41 +1,52 @@
+# frozen_string_literal: true
+
+require 'forwardable'
require 'yaml/store'
require_relative 'mapper'
require_relative 'settings'
require_relative 'sig_finder'
-require_relative 'signature'
require_relative 'tags'
module Signore
class Repo
- def initialize(path: Settings.new.repo_path, sig_finder: SigFinder)
+ extend Forwardable
+
+ def initialize(path: Settings.new.repo_path, sig_finder: SigFinder.new)
@path = path
@sig_finder = sig_finder
+ convert if legacy?
end
def <<(sig)
- sigs << sig
- persist
+ store.transaction { (store['signatures'] ||= []) << Mapper.to_h(sig) }
end
+ delegate empty?: :sigs
+
def find(tags: Tags.new)
sig_finder.find(sigs, tags: tags)
end
def sigs
@sigs ||= begin
- elems = store.transaction(true) { store.fetch('signatures', []) }
- elems.map { |elem| elem.is_a?(Signature) ? elem : Mapper.from_h(elem) }
+ hashes = store.transaction(true) { store.fetch('signatures', []) }
+ hashes.map(&Mapper.method(:from_h))
end
end
- private_attr_reader :path, :sig_finder
-
private
- def persist
- hashes = sigs.map { |sig| Mapper.to_h(sig) }
- store.transaction { store['signatures'] = hashes }
+ attr_reader :path, :sig_finder
+
+ def convert
+ store.transaction do
+ store['signatures'] = store.fetch('signatures', []).map(&:to_h)
+ end
+ end
+
+ def legacy?
+ path.exist? and path.read.include?('Signore::Signature')
end
def store
@store ||= begin
path.dirname.mkpath