Sha256: ad1c03d84fa50184b6a8100c5dd0cbd4399d16d69458a78d25f4d2af2edee805

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'yaml/store'
require_relative 'mapper'
require_relative 'settings'
require_relative 'sig_finder'
require_relative 'tags'

module Signore
  class Repo
    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)
      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
        hashes = store.transaction(true) { store.fetch('signatures', []) }
        hashes.map(&Mapper.method(:from_h))
      end
    end

    private

    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
        YAML::Store.new(path)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
signore-0.6.0 lib/signore/repo.rb
signore-0.5.0 lib/signore/repo.rb