Skip to content

Instantly share code, notes, and snippets.

@barinek
Created February 21, 2010 18:01
Show Gist options
  • Save barinek/310435 to your computer and use it in GitHub Desktop.
Save barinek/310435 to your computer and use it in GitHub Desktop.
ohm models with zset
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