Module: RedisStorageMethods
- Defined in:
- lib/redis_storage_methods.rb,
lib/redis_storage_methods/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
"0.0.3"
Class Method Summary collapse
Instance Method Summary collapse
-
#add_key_value_pairs_of_associations_for_redis!(array) ⇒ Object
No nesting in Redis, no methods for listing associations on DataMapper, This is custom, returns nested objects for use in storage with redis.
- #after_redis_create_do ⇒ Object
- #assemble_key_value_array_for_redis ⇒ Object
-
#construct_low_level_model_from_redis_hash(redis_hash, association) ⇒ Object
this is a helper method that will construct objects from redis that have no nested associations themselves.
-
#create_associations_from_params(params) ⇒ Object
accepts the part of the form hash with nested atributes(but with attributes taken off keys), like { “videos” => => {field => value}} and then creates models.
-
#create_custom_redis_fields ⇒ Object
create the custom fields you need like lists for arrays etc, artist:name => id references, stuff like that.
- #destroy_with_redis ⇒ Object
-
#populate_associations_from_redis(redis_hash) ⇒ Object
expects to get hash of things like video0video_id => “555” from redis, needs to create associations from it.
-
#populate_custom_fields_from_extraneous_redis_calls ⇒ Object
if you have sets stored outside the redis hash, need to make a call to get them, haven’t you? you can do that here, or leave this a nil method.
-
#put_in_redis ⇒ Object
if the db object exists but needs to be placed in redis.
-
#redis ⇒ Object
must also define redis down here so it gets set, there is an instance on the class object and instances of it then.
-
#sync_with_redis ⇒ Object
method to sync object in db with redis.
Class Method Details
.included(base) ⇒ Object
128 129 130 |
# File 'lib/redis_storage_methods.rb', line 128 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#add_key_value_pairs_of_associations_for_redis!(array) ⇒ Object
No nesting in Redis, no methods for listing associations on DataMapper, This is custom, returns nested objects for use in storage with redis. Because I dont want to write metacode that detects nested objects within my own nested objects, I’m just leaving it to each model to implement this in a custom fashion.
197 198 199 |
# File 'lib/redis_storage_methods.rb', line 197 def add_key_value_pairs_of_associations_for_redis!(array) raise 'Must be implemented by object class!' end |
#after_redis_create_do ⇒ Object
221 222 |
# File 'lib/redis_storage_methods.rb', line 221 def after_redis_create_do end |
#assemble_key_value_array_for_redis ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/redis_storage_methods.rb', line 140 def assemble_key_value_array_for_redis a = Array.new self.class.properties.map { |prop| prop.name.to_s }.each do |p| if(self.send(p)) #this if statement is so nil values wont be stored as "" in redis #and come back as an empty string, they will come back as nil. a<<p a<<self.send(p) end end self.add_key_value_pairs_of_associations_for_redis!(a) a end |
#construct_low_level_model_from_redis_hash(redis_hash, association) ⇒ Object
this is a helper method that will construct objects from redis that have no nested associations themselves. So we can keep code DRY. If you have nesting, you must do it yourself.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/redis_storage_methods.rb', line 162 def construct_low_level_model_from_redis_hash(redis_hash, association) found = true i = 0 while(found) do if redis_hash["#{association.singularize}#{i}id"] params_hash = {} redis_hash.select {|k, v| k.to_s.match(/#{association.singularize}#{i}/)}.each do |key, value| params_hash[key.to_s.match(/#{association.singularize}#{i}(\w+)/)[1]] = value end self.send(association)<< Kernel.const_get(association.singularize.capitalize).new(params_hash) else found = false and break end i+=1 end end |
#create_associations_from_params(params) ⇒ Object
accepts the part of the form hash with nested atributes(but with attributes taken off keys), like { “videos” => => {field => value}} and then creates models. Because we don’t support nested creation at the meta level, must be handled locally.
190 191 192 |
# File 'lib/redis_storage_methods.rb', line 190 def create_associations_from_params(params) raise 'Must be implemented by the object class!' end |
#create_custom_redis_fields ⇒ Object
create the custom fields you need like lists for arrays etc, artist:name => id references, stuff like that.
218 219 |
# File 'lib/redis_storage_methods.rb', line 218 def create_custom_redis_fields end |
#destroy_with_redis ⇒ Object
154 155 156 157 |
# File 'lib/redis_storage_methods.rb', line 154 def destroy_with_redis self.destroy redis.del("#{self.class.to_s.downcase}:#{self.id}") end |
#populate_associations_from_redis(redis_hash) ⇒ Object
expects to get hash of things like video0video_id => “555” from redis, needs to create associations from it.
202 203 204 |
# File 'lib/redis_storage_methods.rb', line 202 def populate_associations_from_redis(redis_hash) raise 'Must be implemented by object class!' end |
#populate_custom_fields_from_extraneous_redis_calls ⇒ Object
if you have sets stored outside the redis hash, need to make a call to get them, haven’t you? you can do that here, or leave this a nil method. It won’t hurt you.
208 209 |
# File 'lib/redis_storage_methods.rb', line 208 def populate_custom_fields_from_extraneous_redis_calls end |
#put_in_redis ⇒ Object
if the db object exists but needs to be placed in redis
182 183 184 |
# File 'lib/redis_storage_methods.rb', line 182 def put_in_redis self.class.create_with_associations_and_redis(self) end |
#redis ⇒ Object
must also define redis down here so it gets set, there is an instance on the class object and instances of it then.
136 137 138 |
# File 'lib/redis_storage_methods.rb', line 136 def redis @redis ||= self.class.redis end |
#sync_with_redis ⇒ Object
method to sync object in db with redis.
212 213 214 |
# File 'lib/redis_storage_methods.rb', line 212 def sync_with_redis raise 'Must be implemented by object class!' end |