Metastore
Store and retrieve meta information with ease. Currently YAML and JSON are the supported storage backends.
Installation
Add this line to your application's Gemfile:
gem 'metastore'And then execute:
$ bundle
Or install it yourself as:
$ gem install metastore
Usage
There are four public methods hanging off Metastore::Cabinet:
-
#get('key')or<Metastore::Cabinet instance>['key'] -
#set('key', 'value')or<Metastore::Cabinet instance>['key'] = 'value' #clear!#contents
When calling #get() or #set():
key can be expressed as follows:
-
key- basic string key -
:key- basic symbol key (will be converted into string) -
key1.key2- using a.here allows nested keys to be defined.
When calling #set():
key can be expressed as described above. The YAML file is also immediately saved.
Setup
require 'metastore'
file = File.join(ENV['HOME'], '.metastore.yaml')
# YAML (default)
store = Metastore::Cabinet.new(file, storage_type: :yaml)
# JSON
store = Metastore::Cabinet.new(file, storage_type: :json)Basic example
store.clear!
=> true
store.contents
=> {}
store.get('key')
=> nil
store.set('key', 'value')
=> true
store.get('key')
=> "value"
store.contents
=> {"key"=>"value"}
store.get('key')
=> "value"Advanced examples
When setting values, you can nest both keys and values:
store.clear!
=> true
store.contents
=> {}
store.get('key1.key2')
=> nil
store.set('key1.key2', 'key.key2.value')
=> true
store.contents
=> {"key1"=>{"key2"=>"key.key2.value"}}
store.set('key3.key4', { 'key' => 'value' })
=> true
store.get('key1.key2')
=> "key.key2.value"
store.contents
=> {"key1"=>{"key2"=>"key.key2.value"}, "key3"=>{"key4"=>{"key"=>"value"}}}You can also use Hash notation:
store.clear!
=> true
store.contents
=> {}
store['key1.key2']
=> nil
store['key1.key2'] = 'key.key2.value'
=> "key.key2.value"
store['key1.key2']
=> "key.key2.value"
store.contents
=> {"key1"=>{"key2"=>"key.key2.value"}}
store['key3.key4'] = { 'key' => 'value' }
=> {"key"=>"value"}
store['key3.key4']
=> {"key"=>"value"}
store.contents
=> {"key1"=>{"key2"=>"key.key2.value"}, "key3"=>{"key4"=>{"key"=>"value"}}}Development
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
- Fork it ( https://github.com/ashmckenzie/metastore/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request