Skip to content

[Strong]KeyValueStore

Saumya Kanoria edited this page Sep 19, 2016 · 5 revisions

It is not uncommon to want to persist little bits of random information such as counters, flags etc. For example, you may like to keep a running count of certain occurrences or events in your application. Or you may need to "bookmark" a long running job from time to time, to be able to resume from the last saved bookmark in case of failure.

Using ActiveRecord for such things may very often be overkill since it requires some grunt work to maintain. We provide this functionality much more simply and dynamically, in the form of a global key value store, via:

  • The KeyValueStore class, or,
  • The StrongKeyValueStore concern, that can be included in any plain old Ruby class (PORO) to turn it into a "strong" key value store, with the ability to explicitly define permissible keys, and add validations in the style of ActiveRecord

An example of a concrete StrongKeyValueStore can be found at /app/models/app_settings/outgoing_email.rb.

The documentation in the three files above is more than sufficient to illustrate the usage and implementation of KeyValueStore and StrongKeyValueStore.

Clone this wiki locally