0.0
No commit activity in last 3 years
No release in over 3 years
A simple gem that allows active record models to contain a key / value store for configuration
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 3.1.0
 Project Readme

EasyKeyValue

This gem provides a handy key/value store for your ActiveRecord models.

Installation

Add this line to your application's Gemfile:

gem 'easy_key_value'

And then execute:

$ bundle

Or install it yourself as:

$ gem install easy_key_value

Once installed, please run the following commands :

rails generate ekv:migrations

This will generate a new migration used by easy_key_value

Don't forget to run rake db:migrate in order to apply the new migration.

Usage

Because an example is better than a precept :

class MyModel < ActiveRecord::base

  acts_as_key_value_store # Add this line in order to use the key/value store

end

You have now access to methods that will help you manipulate the data stored for objects of this class.

Adding / Updating a key

model = MyModel.find(42)

model.key('foo', 'bar') # The key will be created if it does not exist

model.key('foo', 'baz') # The key will be updated if it exists

Fetching the value of a key

model.key('foo') # => 'baz'

Destroying a key

model.del_key('foo')

model.key('foo') # => nil

Playing with default values

You can specify default values for a given model.

class MyModel < ActiveRecord::base

  acts_as_key_value_store # Add this line in order to use the key/value store

  key_value_store_defaults {
    'author'   => 'Intrepidd',
    'language' => 'ruby'
  }

end

Then, if the key is not set, the default value will be returned.

model = MyModel.new
model.save

model.key('author') # => 'Intrepidd'

model.key('language') # => 'ruby'

model.key('language', 'whatever')

model.key('language') => 'whatever'

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request