VolatileWTF
Volatile.wtf was closed :(
This repository is archived
A Ruby wrapper for Volatile, a key-value pair API that everyone can use.
More documentation at RDoc.
Installation
gem install volatile_wtf
or for a Gemfile:
gem 'volatile_wtf'
bundle install
Usage
Initialize storage object
Storage = Volatile::Storage.new
Create a key-value pair
Storage['user_name'] = 'Alice' # => 'Alice'
If you want to create a pair independent from your Storage:
Storage.set('random_key', 'random_val')
You can use symbols as keys, but it is not recommended.
Retrieve a value by key
Storage['user_name'] # => 'Alice'
If you want to retrieve a value by a custom key independent from your Storage, use Storage.get
:
Storage.get('random_key') # => 'random_val'
created
and modified
timestamps
Volatile allows to get information about when a key-pair was created and modified.
-
Created
Storage.created('user_name') # => 2019-11-12 16:55:45 +0300
-
Modified
Storage.modified('user_name') # => 2019-11-12 17:40:45 +0300
To get modifiers information for non-Storage keys use external: true
parameter:
Storage.modified('user_name', external: true)
Namespaces
Namespace is used for distinguishing keys from different Storages.
Passing your own namespace
By default, Storage is initialized with a namespace equal to SecureRandom.hex[0..5]
and makes keys look like 0123ab_some
instead of just some
. You can pass your own value like this:
Storage = Volatile::Storage.new('my_ns')
Or you can change namespace later:
Storage.namespace = 'my_own_ns'
Warning! You can loose links to previously stored data if you change a namespace.
Generating a namespaced key name
If you want to get a namespaced key name, you should use Storage#namespaced_key
:
Storage.namespaced_key('nice_key') # => '0123ab_nice_key'
Don't forget that every Volatile::Storage.new
has its own namespace.
Hash conversion (#to_h
)
By default, #to_h
generates a hash with friendly keys without a namespace:
{
"user_name" => "Alice",
"email" => "alice@example.com"
}
To generate a hash with namespaced keys, use use_namespace: true
parameter:
Storage.to_h(use_namespace: true)
This will return the following result:
{
"2365fc_user_name" => "Alice",
"2365fc_email" => "alice@example.com"
}