lib/signore/repo.rb in signore-0.6.0 vs lib/signore/repo.rb in signore-0.7.0

- old
+ new

@@ -1,52 +1,54 @@ -# frozen_string_literal: true - require 'forwardable' +require 'pathname' require 'yaml/store' -require_relative 'mapper' -require_relative 'settings' -require_relative 'sig_finder' +require_relative 'signature' require_relative 'tags' module Signore class Repo + class << self + def default_path + dir = ENV.fetch('XDG_DATA_HOME') { File.expand_path('~/.local/share') } + Pathname.new(dir) / 'signore' / 'signatures.yml' + end + end + extend Forwardable - def initialize(path: Settings.new.repo_path, sig_finder: SigFinder.new) - @path = path - @sig_finder = sig_finder + def initialize(path: self.class.default_path) + @path = path convert if legacy? end - def <<(sig) - store.transaction { (store['signatures'] ||= []) << Mapper.to_h(sig) } + def <<(signature) + self.hashes = hashes + [signature.to_h] end - delegate empty?: :sigs + delegate empty?: :hashes - def find(tags: Tags.new) - sig_finder.find(sigs, tags: tags) - end - def sigs - @sigs ||= begin - hashes = store.transaction(true) { store.fetch('signatures', []) } - hashes.map(&Mapper.method(:from_h)) - end + hashes.map(&Signature.method(:from_h)) end private - attr_reader :path, :sig_finder + attr_reader :path def convert - store.transaction do - store['signatures'] = store.fetch('signatures', []).map(&:to_h) - end + self.hashes = hashes.map(&:to_h) end def legacy? path.exist? and path.read.include?('Signore::Signature') + end + + def hashes + store.transaction(true) { store.fetch('signatures', []) } + end + + def hashes=(hashes) + store.transaction { store['signatures'] = hashes } end def store @store ||= begin path.dirname.mkpath