Class: FluidCLI::DB
- Inherits:
-
Object
- Object
- FluidCLI::DB
- Extended by:
- SingleForwardable
- Defined in:
- lib/fluid_cli/db.rb
Overview
Persists transient data like access tokens that may be cleared when user clears their session
All of the instance methods documented here can be used as class methods. All class methods are forwarded to a new instance of the database, pointing at the default path.
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#clear ⇒ Object
Drops all keys from the database.
-
#del(*args) ⇒ Object
Deletes a value from the local storage.
-
#exists?(key) ⇒ Boolean
Check to see if a key exists in the database, the key will only exist if it has a value so if the key exists then there is also a value.
-
#get(key) ⇒ Object
Gets a value from the DB that is associated with the supplied key.
-
#initialize(path: File.join(FluidCLI.cache_dir, ".db.pstore")) ⇒ DB
constructor
:nodoc:.
-
#keys ⇒ Object
Get all keys that exist in the database.
-
#set(**args) ⇒ Object
Persist a value by key in the local storage.
Constructor Details
Instance Attribute Details
#db ⇒ Object (readonly)
:nodoc:
15 16 17 |
# File 'lib/fluid_cli/db.rb', line 15 def db @db end |
Instance Method Details
#clear ⇒ Object
110 111 112 |
# File 'lib/fluid_cli/db.rb', line 110 def clear del(*keys) end |
#del(*args) ⇒ Object
100 101 102 |
# File 'lib/fluid_cli/db.rb', line 100 def del(*args) db.transaction { args.each { |key| db.delete(key) } } end |
#exists?(key) ⇒ Boolean
Check to see if a key exists in the database, the key will only exist if it has a value so if the key exists then there is also a value.
Parameters
key: a string or a symbol representation of a key that is stored in the DB
Returns
exists: a boolean value if the key exists in the database
Usage
exists = FluidCLI::DB.exists?('fluid_exchange_token')
47 48 49 |
# File 'lib/fluid_cli/db.rb', line 47 def exists?(key) db.transaction(true) { db.root?(key) } end |
#get(key) ⇒ Object
85 86 87 88 89 |
# File 'lib/fluid_cli/db.rb', line 85 def get(key) val = db.transaction(true) { db[key] } val = yield if val.nil? && block_given? val end |
#keys ⇒ Object
30 31 32 |
# File 'lib/fluid_cli/db.rb', line 30 def keys db.transaction(true) { db.roots } end |
#set(**args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fluid_cli/db.rb', line 60 def set(**args) db.transaction do args.each do |key, val| if val.nil? db.delete(key) else db[key] = val end end end end |