Module: Ohm::SortedMethods

Included in:
SortedSet
Defined in:
lib/ohm/sorted.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/ohm/sorted.rb', line 12

def key
  @key
end

#modelObject (readonly)

Returns the value of attribute model.



14
15
16
# File 'lib/ohm/sorted.rb', line 14

def model
  @model
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



13
14
15
# File 'lib/ohm/sorted.rb', line 13

def namespace
  @namespace
end

Instance Method Details

#between(first, last) ⇒ Object



36
37
38
39
40
41
42
# 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

#countObject



28
29
30
# File 'lib/ohm/sorted.rb', line 28

def count
  @options.fetch(:count, -1)
end

#firstObject



66
67
68
# File 'lib/ohm/sorted.rb', line 66

def first
  slice(0, 1).to_a.first
end

#idsObject



70
71
72
73
74
75
76
# 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

#initialize(key, namespace, model, options = {}) ⇒ Object



16
17
18
19
20
21
22
# 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

#inspectObject



78
79
80
# File 'lib/ohm/sorted.rb', line 78

def inspect
  "#<SortedSet (#{model}): #{ids}>"
end

#offsetObject



24
25
26
# File 'lib/ohm/sorted.rb', line 24

def offset
  @options.fetch(:offset, 0)
end

#reverseObject



48
49
50
51
52
# 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?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ohm/sorted.rb', line 44

def reversed?
  @options.fetch(:reverse, false)
end

#sizeObject



32
33
34
# File 'lib/ohm/sorted.rb', line 32

def size
  execute { |key| fix_size(db.zcard(key)) }
end

#slice(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# 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