Class: Pgai::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/pgai/store.rb

Constant Summary collapse

STORE_NAME =
"config.enc.pstore"
LEGACY_STORE_NAME =
"config.pstore"

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, config_dir: Pgai.config_dir) ⇒ Store

Returns a new instance of Store.



8
9
10
11
# File 'lib/pgai/store.rb', line 8

def initialize(key: nil, config_dir: Pgai.config_dir)
  @key = key
  @config_dir = config_dir
end

Instance Method Details

#all(record_type) ⇒ Object



13
14
15
16
17
# File 'lib/pgai/store.rb', line 13

def all(record_type)
  backend.transaction(true) do
    backend[record_type]&.values || {}
  end
end

#delete(record_type, id) ⇒ Object



25
26
27
28
29
30
# File 'lib/pgai/store.rb', line 25

def delete(record_type, id)
  backend.transaction do
    backend[record_type] ||= {}
    backend[record_type] = backend[record_type].except(id)
  end
end

#encrypted?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/pgai/store.rb', line 39

def encrypted?
  backend.is_a?(Encryption::Store)
end

#find(record_type, id) ⇒ Object



19
20
21
22
23
# File 'lib/pgai/store.rb', line 19

def find(record_type, id)
  backend.transaction(true) do
    (backend[record_type] || {})[id]
  end
end

#save(record_type, attributes, key: :id) ⇒ Object



32
33
34
35
36
37
# File 'lib/pgai/store.rb', line 32

def save(record_type, attributes, key: :id)
  backend.transaction do
    backend[record_type] ||= {}
    backend[record_type].merge!(attributes[key] => attributes)
  end
end