0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A Ruby wrapper for Volatile, a key-value pair API that everyone can use.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
~> 10.0
~> 3.0
 Project Readme

VolatileWTF

Volatile.wtf was closed :(

This repository is archived

Gem Version

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"
}