Class: Adalog::PStoreRepo

Inherits:
Object
  • Object
show all
Defined in:
lib/adalog/pstore_repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, **repo_options) ⇒ PStoreRepo

Returns a new instance of PStoreRepo.



8
9
10
# File 'lib/adalog/pstore_repo.rb', line 8

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

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



6
7
8
# File 'lib/adalog/pstore_repo.rb', line 6

def storage
  @storage
end

Instance Method Details

#allObject



18
19
20
21
22
23
24
# File 'lib/adalog/pstore_repo.rb', line 18

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

#clear!Object



38
39
40
41
42
43
44
45
46
# File 'lib/adalog/pstore_repo.rb', line 38

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

#fetch(**options) ⇒ Object



13
14
15
# File 'lib/adalog/pstore_repo.rb', line 13

def fetch(**options)
  all
end

#insert(entry = nil, **options) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/adalog/pstore_repo.rb', line 27

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