Created
February 21, 2010 18:01
-
-
Save barinek/310435 to your computer and use it in GitHub Desktop.
ohm models with zset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ohm | |
class Model | |
def self.zset(name, model = nil) | |
attr_zset_reader(name, model) | |
collections << name unless collections.include?(name) | |
end | |
def self.attr_zset_reader(name, model) | |
define_method(name) do | |
instance_variable_get("@#{name}") || | |
instance_variable_set("@#{name}", Attributes::ZSet.new(db, key(name), model)) | |
end | |
end | |
end | |
end | |
module Ohm | |
module Attributes | |
class ZSet < Collection | |
def zadd(score, value) | |
db.zadd(key, score, value.id) | |
end | |
def delete(value) | |
db.zrem(key, value) | |
end | |
def find(min, max) | |
db.zrangebyscore(key, min, max).each.collect do |key| | |
model[key] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment