Module: Riak::Shim::Persistable

Defined in:
lib/riak-shim/persistable.rb

Overview

Provides basic persistence features

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyString

Returns the unique key for storing the object in Riak.

Returns:

  • (String)

    the unique key for storing the object in Riak



11
12
13
# File 'lib/riak-shim/persistable.rb', line 11

def key
  @key ||= self.class.gen_key
end

Instance Method Details

#bucketRiak::Bucket

Returns your app’s Riak bucket for this class.

Returns:

  • (Riak::Bucket)

    your app’s Riak bucket for this class



16
17
18
# File 'lib/riak-shim/persistable.rb', line 16

def bucket
  return self.class.store.bucket(bucket_name)
end

#bucket_nameString

Returns name of the Riak bucket generated from the class name and your configuration.

Returns:

  • (String)

    name of the Riak bucket generated from the class name and your configuration



21
22
23
# File 'lib/riak-shim/persistable.rb', line 21

def bucket_name
  self.class.bucket_name
end

#destroyPersistable

Remove this object from Riak

Returns:



42
43
44
45
# File 'lib/riak-shim/persistable.rb', line 42

def destroy
  bucket.delete(key)
  self
end

#savePersistable

Persist this object into Riak

Returns:



32
33
34
35
36
37
38
# File 'lib/riak-shim/persistable.rb', line 32

def save
  doc = bucket.get_or_new(key)
  doc.data = to_hash
  set_indexes(doc.indexes)
  doc.store
  self
end

#storeRiak::Shim::Store

Returns the Riak cluster you are connected to.

Returns:



26
27
28
# File 'lib/riak-shim/persistable.rb', line 26

def store
  self.class.store
end