Module: Riak::Shim::Persistable::ClassMethods
- Defined in:
- lib/riak-shim/persistable.rb
Instance Method Summary collapse
-
#[](val) ⇒ Riak::Shim::SeconardyIndexQuery
An interface to ducktype index lookups as hash lookups.
-
#bucket ⇒ Riak::Bucket
Your app’s Riak bucket for this class.
-
#bucket_name ⇒ String
Name of the Riak bucket generated from the class name and your configuration.
-
#count ⇒ Integer
WARNING: This is very slow and is intended only for your tests, not for production use.
-
#delete_all ⇒ Object
WARNING: This is very slow and is intended only for your tests, not for production use.
-
#for_index(index, value) ⇒ Array<Persistable>
Locate instances by secondary index.
-
#for_key(key) ⇒ Persistable, #nil
Look up an instance of your class.
-
#gen_key ⇒ String
A UUID which we will use as our key.
-
#store ⇒ Riak::Shim::Store
The store you are connected to.
Instance Method Details
#[](val) ⇒ Riak::Shim::SeconardyIndexQuery
Returns 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 |
#bucket ⇒ Riak::Bucket
Returns 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_name ⇒ String
Returns 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 |
#count ⇒ Integer
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.
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_all ⇒ Object
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.
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
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_key ⇒ String
Returns 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 |
#store ⇒ Riak::Shim::Store
Returns the store you are connected to.
62 63 64 |
# File 'lib/riak-shim/persistable.rb', line 62 def store @store ||= Store.new end |