Module: TopModel::Redis::ClassMethods
- Defined in:
- lib/topmodel/redis.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #delete_all ⇒ Object
- #exists?(id) ⇒ Boolean
- #find(id) ⇒ Object
- #find_all_by_attribute(key, value) ⇒ Object
- #find_by_attribute(key, value) ⇒ Object
- #first ⇒ Object
- #indexes(*indexes) ⇒ Object
- #last ⇒ Object
- #namespace ⇒ Object
- #namespace=(namespace) ⇒ Object
- #redis ⇒ Object
- #redis_key(*args) ⇒ Object
- #select ⇒ Object
Class Method Details
.extended(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/topmodel/redis.rb', line 4 def self.extended(base) base.class_eval do #class_inheritable_array :indexed_attributes class_attribute :indexed_attributes self.indexed_attributes = [] #class_inheritable_hash :redis_options class_attribute :redis_options self. = {} end end |
Instance Method Details
#all ⇒ Object
65 66 67 |
# File 'lib/topmodel/redis.rb', line 65 def all from_ids(redis.sort(redis_key)) end |
#count ⇒ Object
61 62 63 |
# File 'lib/topmodel/redis.rb', line 61 def count redis.scard(redis_key) end |
#delete_all ⇒ Object
73 74 75 |
# File 'lib/topmodel/redis.rb', line 73 def delete_all raise "Not implemented" end |
#exists?(id) ⇒ Boolean
57 58 59 |
# File 'lib/topmodel/redis.rb', line 57 def exists?(id) redis.sismember(redis_key, id.to_s) end |
#find(id) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/topmodel/redis.rb', line 37 def find(id) if redis.sismember(redis_key, id.to_s) existing(:id => id) else raise UnknownRecord, "Couldn't find #{self.name} with ID=#{id}" end end |
#find_all_by_attribute(key, value) ⇒ Object
83 84 85 |
# File 'lib/topmodel/redis.rb', line 83 def find_all_by_attribute(key, value) from_ids(redis.sort(redis_key(key, value.to_s))) end |
#find_by_attribute(key, value) ⇒ Object
77 78 79 80 81 |
# File 'lib/topmodel/redis.rb', line 77 def find_by_attribute(key, value) item_ids = redis.sort(redis_key(key, value.to_s)) item_id = item_ids.first item_id && existing(:id => item_id) end |
#first ⇒ Object
45 46 47 48 49 |
# File 'lib/topmodel/redis.rb', line 45 def first item_ids = redis.sort(redis_key, :order => "ASC", :limit => [0, 1]) item_id = item_ids.first item_id && existing(:id => item_id) end |
#indexes(*indexes) ⇒ Object
28 29 30 |
# File 'lib/topmodel/redis.rb', line 28 def indexes(*indexes) self.indexed_attributes += indexes.map(&:to_s) end |
#last ⇒ Object
51 52 53 54 55 |
# File 'lib/topmodel/redis.rb', line 51 def last item_ids = redis.sort(redis_key, :order => "DESC", :limit => [0, 1]) item_id = item_ids.first item_id && existing(:id => item_id) end |
#namespace ⇒ Object
16 17 18 |
# File 'lib/topmodel/redis.rb', line 16 def namespace @namespace ||= self.name.downcase end |
#namespace=(namespace) ⇒ Object
20 21 22 |
# File 'lib/topmodel/redis.rb', line 20 def namespace=(namespace) @namespace = namespace end |
#redis ⇒ Object
24 25 26 |
# File 'lib/topmodel/redis.rb', line 24 def redis @redis ||= ::Redis.connect() end |
#redis_key(*args) ⇒ Object
32 33 34 35 |
# File 'lib/topmodel/redis.rb', line 32 def redis_key(*args) args.unshift(self.namespace) args.join(":") end |
#select ⇒ Object
69 70 71 |
# File 'lib/topmodel/redis.rb', line 69 def select raise "Not implemented" end |