Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use with Ohm 2.1 (and redic) #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions lib/ohm/sorted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def count
end

def size
execute { |key| fix_size(db.zcard(key)) }
execute { |key| fix_size(redis.call('zcard', key)) }
end

def between(first, last)
Expand Down Expand Up @@ -69,9 +69,9 @@ def first

def ids
if reversed?
execute { |key| db.zrevrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
execute { |key| redis.call('zrevrangebyscore', key, @range.first, @range.last, 'limit', offset, count) }
else
execute { |key| db.zrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
execute { |key| redis.call('zrangebyscore', key, @range.first, @range.last, 'limit', offset, count) }
end
end

Expand All @@ -91,23 +91,23 @@ class SortedSet < BasicSet

private
def exists?(id)
execute { |key| !!db.zscore(key, id) }
execute { |key| !!redis.call('zscore', key, id) }
end

def execute
yield key
end

def db
model.db
def redis
model.redis
end
end
else
class SortedSet < Model::Collection
include Ohm::SortedMethods

def db
model.db
def redis
model.redis
end

def each(&block)
Expand All @@ -116,7 +116,7 @@ def each(&block)
end

def [](id)
model[id] if !!db.zrank(key, id)
model[id] if !!redis.call('zrank', key, id)
end

def empty?
Expand All @@ -128,7 +128,7 @@ def all
end

def include?(model)
!!db.zrank(key, model.id)
!!redis.call('zrank', key, model.id)
end

private
Expand All @@ -140,7 +140,7 @@ def execute

class RangedSortedSet < SortedSet
def size
execute { |key| fix_size(db.zcount(key, @range.first, @range.last)) }
execute { |key| fix_size(redis.call('zcount', key, @range.first, @range.last)) }
end
end

Expand Down Expand Up @@ -222,16 +222,16 @@ def add_sorted_indices
attr = send(attribute)
if attr
score = attr.to_f
db.zadd(key, score, id)
redis.call('zadd', key, score, id)
else
db.zrem(key, id)
redis.call('zrem', key, id)
end
end
end

def remove_sorted_indices
update_sorted_indices do |key, attribute, options|
db.zrem(key, id)
redis.call('zrem', key, id)
end
end

Expand All @@ -240,13 +240,13 @@ def prune_sorted_indices
update_sorted_indices do |key, attribute, options|
return unless options.include?(:group_by)

old_value = db.hget(self.key, options[:group_by])
old_value = redis.call('hget', self.key, options[:group_by])
new_value = send(options[:group_by])

if old_value != new_value
opts = {options[:group_by] => old_value}
key = self.class.sorted_index_key(attribute, opts)
db.zrem(key, id)
redis.call('zrem', key, id)
end
end
end
Expand Down