Module: Riak::Shim::Persistable::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#[](val) ⇒ Riak::Shim::SeconardyIndexQuery

Returns an interface to ducktype index lookups as hash lookups. i.e. Class[‘KEY’].

Returns:

  • (Riak::Shim::SeconardyIndexQuery)

    an interface to ducktype index lookups as hash lookups. i.e. Class[‘KEY’]



105
106
107
# File 'lib/riak-shim/persistable.rb', line 105

def [](val)
  SecondaryIndexQuery.new(self, val)
end

#bucketRiak::Bucket

Returns your app’s Riak bucket for this class.

Returns:

  • (Riak::Bucket)

    your app’s Riak bucket for this class



74
75
76
# File 'lib/riak-shim/persistable.rb', line 74

def bucket
  return 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



68
69
70
71
# File 'lib/riak-shim/persistable.rb', line 68

def bucket_name
  myclass = de_camel(self.to_s)
  "#{store.bucket_prefix}#{myclass}"
end

#countInteger

WARNING: This is very slow and is intended only for your tests, not for production use.

Counts instances in the database which really means items in the bucket.

Returns:

  • (Integer)

    the number of instances of your class stored in Riak



122
123
124
125
126
# File 'lib/riak-shim/persistable.rb', line 122

def count
  counter = 0
  bucket.keys {|keys| counter += keys.count }
  return counter
end

#delete_allObject

WARNING: This is very slow and is intended only for your tests, not for production use.

Remove all instances of this class from Riak.



53
54
55
56
57
58
59
# File 'lib/riak-shim/persistable.rb', line 53

def delete_all
  bucket.keys do |streamed_keys|
    streamed_keys.each do |key|
      bucket.delete(key)
    end
  end
end

#for_index(index, value) ⇒ Array<Persistable>

Locate instances by secondary index.

NOTE: This interface is ugly as sin, so expect it to change in a future release.

Returns:

  • (Array<Persistable>)

    any instances of your class whose indexed field matches value



98
99
100
101
102
# File 'lib/riak-shim/persistable.rb', line 98

def for_index(index, value)
  bucket.get_index(index, value).map do |key|
    for_key(key)
  end
end

#for_key(key) ⇒ Persistable, #nil

Look up an instance of your class

Returns:

  • (Persistable, #nil)

    an instance corresponding to key



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/riak-shim/persistable.rb', line 80

def for_key(key)
  begin
    raw = bucket.get(key)
    data = raw.data
    result = from_hash(data)
    result.key = key
    result
  rescue Riak::HTTPFailedRequest
    return nil
  end
end

#gen_keyString

Returns a UUID which we will use as our key.

Returns:

  • (String)

    a UUID which we will use as our key



110
111
112
# File 'lib/riak-shim/persistable.rb', line 110

def gen_key
  UUIDTools::UUID.random_create.to_s
end

#storeRiak::Shim::Store

Returns the store you are connected to.

Returns:



62
63
64
# File 'lib/riak-shim/persistable.rb', line 62

def store
  @store ||= Store.new
end