Code Monkey home page Code Monkey logo

volatile-wtf-rb's Introduction

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" => "[email protected]"
}

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" => "[email protected]"
}

volatile-wtf-rb's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.