module Ohm::SortedMethods

Attributes

key[R]
model[R]
namespace[R]

Public Class Methods

new(key, namespace, model, options={}) click to toggle source
# File lib/ohm/sorted.rb, line 16
def initialize(key, namespace, model, options={})
  @key = key
  @namespace = namespace
  @model = model
  @options = options
  @range = options.fetch(:range, ["-inf", "inf"])
end

Public Instance Methods

between(first, last) click to toggle source
# File lib/ohm/sorted.rb, line 36
def between(first, last)
  range = [first.to_f, last.to_f]
  range.reverse! if reversed?

  opts = @options.merge(range: range)
  RangedSortedSet.new(key, namespace, model, opts)
end
count() click to toggle source
# File lib/ohm/sorted.rb, line 28
def count
  @options.fetch(:count, -1)
end
first() click to toggle source
# File lib/ohm/sorted.rb, line 66
def first
  slice(0, 1).to_a.first
end
ids() click to toggle source
# File lib/ohm/sorted.rb, line 70
def ids
  if reversed?
    execute { |key| db.zrevrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
  else
    execute { |key| db.zrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
  end
end
inspect() click to toggle source
# File lib/ohm/sorted.rb, line 78
def inspect
  "#<SortedSet (#{model}): #{ids}>"
end
offset() click to toggle source
# File lib/ohm/sorted.rb, line 24
def offset
  @options.fetch(:offset, 0)
end
reverse() click to toggle source
# File lib/ohm/sorted.rb, line 48
def reverse
  opts = @options.merge(reverse: !reversed?, range: @range.reverse)

  self.class.new(key, namespace, model, opts)
end
reversed?() click to toggle source
# File lib/ohm/sorted.rb, line 44
def reversed?
  @options.fetch(:reverse, false)
end
size() click to toggle source
# File lib/ohm/sorted.rb, line 32
def size
  execute { |key| fix_size(db.zcard(key)) }
end
slice(*args) click to toggle source
# File lib/ohm/sorted.rb, line 54
def slice(*args)
  if args.count == 1
    self[args.first]
  elsif args.count == 2
    offset, count = *args
    opts = @options.merge(offset: offset, count: count)
    self.class.new(key, namespace, model, opts)
  else
    raise ArgumentError
  end
end

Private Instance Methods

fix_size(value) click to toggle source
# File lib/ohm/sorted.rb, line 83
def fix_size(value)
  [[value - offset, 0].max, count >= 0 ? count : Float::INFINITY].min
end