entity_storage
github.com/eatenbyagrue/entity_storage
DESCRIPTION:
An easy to use Key/Value store for any Ruby on Rails project. Like Memcache, only persistent. Stores config values & application wide state in the database in order to survive server restarts.
Designed to allow you to add persistent value storage to any Rails project in about 5 minutes.
Additionally, allows users to set a list of default keys that auto-initializes baseline key/value pairs in the database for easy initialization.
SYNOPSIS:
You can use the entity store like so:
# Get key value.
e = EntityStore["testkey"]
e = EntityStore.testkey
e = EntityStore[:testkey]
# sets key named 'happened' to a Time object of now
EntityStore[:happened] = Time.now
EntityStore["happened"] = Time.now
EntityStore.happened = Time.now
# find out it's default, even if it's been changed
e = EntityStore.default(:testkey)
# or
e = EntityStore.defaults[:testkey]
# set it back to default
EntityStore.default!(:testkey)
# delete an item
EntityStore.delete(key)
All EntityStorage operations sync immediately with the database, so a server shutdown will not impact stored values.
If you access a key that doesn’t exist, and is specified in default list, will be initialized and returned. If not in default list, will return nil.
Keys can be up to 512 characters in length. Values can be practically any size, and consist of any object. Objects are marshalled back and forth between database.
REQUIREMENTS:
Requires ActiveRecord 2.2.3 or above (probably works with earlier versions, but has not been tested.)
INSTALL:
To install from github:
Run the following if you haven't already:
gem sources -a http://gems.github.com
Install the gem(s):
sudo gem install eatenbyagrue-entity_storage
Or download the souce above and run, customized for version and environment:
sudo gem install PATH/entity_storage.#.#.#.gem
Put the following at the bottom of your environment.rb file, or in an initializer.
require 'entity_storage'
DEFAULT_KEYS = { "testkey" => DateTime.parse("1-1-900"), "also test" => 2, "long ass key that I probably wouldn't use" => false }
EntityStore = EntityStorage::Storage.new(DEFAULT_KEYS)
On initialization, if the table “entity_storage” doesn’t exist, it will be created. If you already have a table with that name, it must be in the correct format (previously created by this gem.)
You can pass a hash full of default key/value pairs. If the application accesses one of the keys, and it doesn’t already exist, it will initiliaze that key with the default value. Good for getting your app to an intial starting state.
LICENSE:
(The MIT License)
Copyright © 2009 Joshua Siler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.