Module: SuperModel::Redis::ClassMethods
- Defined in:
- lib/supermodel/redis.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #delete_all ⇒ Object
- #find(id) ⇒ 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
- #serialize(*attributes) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/supermodel/redis.rb', line 4 def self.extended(base) base.class_eval do class_inheritable_array :indexed_attributes self.indexed_attributes = [] class_inheritable_array :serialized_attributes self.serialized_attributes = [] end end |
Instance Method Details
#all ⇒ Object
58 59 60 |
# File 'lib/supermodel/redis.rb', line 58 def all from_ids(redis.set_members(redis_key)) end |
#count ⇒ Object
54 55 56 |
# File 'lib/supermodel/redis.rb', line 54 def count redis.set_count(redis_key) end |
#delete_all ⇒ Object
62 63 64 |
# File 'lib/supermodel/redis.rb', line 62 def delete_all raise "Not implemented" end |
#find(id) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/supermodel/redis.rb', line 38 def find(id) if redis.set_member?(redis_key, id) existing(:id => id) else raise(UnknownRecord) end end |
#find_by_attribute(key, value) ⇒ Object
66 67 68 69 70 |
# File 'lib/supermodel/redis.rb', line 66 def find_by_attribute(key, value) item_ids = redis.set_members(redis_key(key, value)) return if item_ids.empty? existing(:id => item_ids.first) end |
#first ⇒ Object
46 47 48 |
# File 'lib/supermodel/redis.rb', line 46 def first all.first end |
#indexes(*indexes) ⇒ Object
25 26 27 |
# File 'lib/supermodel/redis.rb', line 25 def indexes(*indexes) self.indexed_attributes += indexes.map(&:to_s) end |
#last ⇒ Object
50 51 52 |
# File 'lib/supermodel/redis.rb', line 50 def last all.last end |
#namespace ⇒ Object
13 14 15 |
# File 'lib/supermodel/redis.rb', line 13 def namespace @namespace ||= self.name.downcase end |
#namespace=(namespace) ⇒ Object
17 18 19 |
# File 'lib/supermodel/redis.rb', line 17 def namespace=(namespace) @namespace = namespace end |
#redis ⇒ Object
21 22 23 |
# File 'lib/supermodel/redis.rb', line 21 def redis @redis ||= ::Redis.new end |
#redis_key(*args) ⇒ Object
33 34 35 36 |
# File 'lib/supermodel/redis.rb', line 33 def redis_key(*args) args.unshift(self.namespace) args.join(":") end |
#serialize(*attributes) ⇒ Object
29 30 31 |
# File 'lib/supermodel/redis.rb', line 29 def serialize(*attributes) self.serialized_attributes += attributes.map(&:to_s) end |