lib/ohm.rb in ohm-1.1.0 vs lib/ohm.rb in ohm-1.1.1
- old
+ new
@@ -193,11 +193,11 @@
# Note: This is slower compared to just doing `sort`, specifically
# because Redis has to read each individual hash in order to sort
# them.
#
def sort_by(att, options = {})
- sort(options.merge(:by => namespace["*->%s" % att]))
+ sort(options.merge(:by => to_key(att)))
end
# Allows you to sort your models using their IDs. This is much
# faster than `sort_by`. If you simply want to get records in
# ascending or descending order, then this is the best method to
@@ -221,11 +221,11 @@
# User.all.sort(:order => "DESC").map(&:id) == ["2", "1"]
# # => true
#
def sort(options = {})
if options.has_key?(:get)
- options[:get] = namespace["*->%s" % options[:get]]
+ options[:get] = to_key(options[:get])
return execute { |key| db.sort(key, options) }
end
fetch(execute { |key| db.sort(key, options) })
end
@@ -294,10 +294,18 @@
private
def exists?(id)
execute { |key| db.sismember(key, id) }
end
+
+ def to_key(att)
+ if model.counters.include?(att)
+ namespace["*:counters->%s" % att]
+ else
+ namespace["*->%s" % att]
+ end
+ end
end
class List < Struct.new(:key, :namespace, :model)
include PipelinedFetch
include Enumerable
@@ -1018,10 +1026,12 @@
#
# Note: You can't use counters until you save the model. If you
# try to do it, you'll receive an Ohm::MissingID error.
#
def self.counter(name)
+ counters << name unless counters.include?(name)
+
define_method(name) do
return 0 if new?
db.hget(key[:counters], name).to_i
end
@@ -1354,9 +1364,13 @@
@indices ||= []
end
def self.uniques
@uniques ||= []
+ end
+
+ def self.counters
+ @counters ||= []
end
def self.collections
@collections ||= []
end