class KeyValueModel

A data model representing a basic Key/Value store.

This has the following structure on disk:

key-value/            : ModelItem class for Key/Value pairs
key-value/$KEY/value  : Property file containing value of key

Public Class Methods

new(db) click to toggle source
Calls superclass method GitDS::Model::new
# File doc/examples/key_value/model.rb, line 65
def initialize(db)
  super db, 'key/value model'
end

Public Instance Methods

[](key) click to toggle source

Return value for key if it exists in model, nil otherwise.

# File doc/examples/key_value/model.rb, line 89
def [](key)
  return nil if not exist? key
  p = pair(key)
  p ? p.value : nil
end
[]=(key, val) click to toggle source

Set value for key to ‘val’. The key/value pair is created if it does not already exist.

# File doc/examples/key_value/model.rb, line 99
def []=(key, val)
  if self.exist? key
    p = pair(key)
    p.value = val if p
  else
    KeyValueModelItem.create self.root, {:key => key, :value => val}
  end
end
each() { |k, self| ... } click to toggle source

Yield each key, value pair in database.

# File doc/examples/key_value/model.rb, line 111
def each
  KeyValueModelItem.list(self.root).each do |k|
    yield [k, self[k]]
  end
end
exist?(key)
Alias for: include?
include?(key) click to toggle source

Return true if key exists in Model.

Calls superclass method GitDS::ModelObject#include?
# File doc/examples/key_value/model.rb, line 80
def include?(key)
  super KeyValueModelItem.instance_path(self.root.path, key)
end
Also aliased as: exist?
pair(key) click to toggle source

Return KeyValudeModelItem for key.

# File doc/examples/key_value/model.rb, line 72
def pair(key)
  path = KeyValueModelItem.instance_path(self.root.path, key)
  KeyValueModelItem.new(self, path)
end
remove(key) click to toggle source

Remove a key:value pair from the database.

# File doc/examples/key_value/model.rb, line 120
def remove(key)
  pair(key).delete
end