Sha256: 349f95764900ea4b26a446d07e0f744ae54f7747f12e5ffdcb4644e500aa6fe6

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

require 'pstore'

module Adalog
  class PStoreRepo

    attr_reader :storage

    def initialize(path, **repo_options)
      @storage  = PStore.new(path, true)
    end


    def fetch(**options)
      all
    end


    def all
      storage.transaction do
        storage.roots.flat_map do |key|
          storage.fetch(key, [])
        end
      end.reverse
    end


    def insert(entry = nil, **options)
      converted_entry = Adalog::Entry.build(entry, **options)
      if converted_entry.valid?
        insert_into_storage(converted_entry)
        [:ok, converted_entry]
      else
        [:error, converted_entry.errors]
      end
    end


    def clear!
      storage.transaction do
        all_keys = storage.roots
        all_keys.each do |key|
          storage.delete(key)
        end
      end
      :ok
    end


  private ######################################################################


    def insert_into_storage(entry)
      storage.transaction do
        key   = Time.now.to_i
        list  = storage.fetch(key, [])
        list.unshift(entry)
        storage[key] = list
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
adalog-0.5.0 lib/adalog/pstore_repo.rb
adalog-0.4.1 lib/adalog/pstore_repo.rb
adalog-0.4.0 lib/adalog/pstore_repo.rb