Module: Ohm::SortedMethods
- Included in:
- SortedSet
- Defined in:
- lib/ohm/sorted.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #between(first, last) ⇒ Object
- #count ⇒ Object
- #first ⇒ Object
- #ids ⇒ Object
- #initialize(key, namespace, model, options = {}) ⇒ Object
- #inspect ⇒ Object
- #offset ⇒ Object
- #reverse ⇒ Object
- #reversed? ⇒ Boolean
- #size ⇒ Object
- #slice(*args) ⇒ Object
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
12 13 14 |
# File 'lib/ohm/sorted.rb', line 12 def key @key end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
14 15 16 |
# File 'lib/ohm/sorted.rb', line 14 def model @model end |
#namespace ⇒ Object (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 |
#count ⇒ Object
28 29 30 |
# File 'lib/ohm/sorted.rb', line 28 def count @options.fetch(:count, -1) end |
#first ⇒ Object
66 67 68 |
# File 'lib/ohm/sorted.rb', line 66 def first slice(0, 1).to_a.first end |
#ids ⇒ Object
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, ={}) @key = key @namespace = namespace @model = model @options = @range = .fetch(:range, ["-inf", "inf"]) end |
#inspect ⇒ Object
78 79 80 |
# File 'lib/ohm/sorted.rb', line 78 def inspect "#<SortedSet (#{model}): #{ids}>" end |
#offset ⇒ Object
24 25 26 |
# File 'lib/ohm/sorted.rb', line 24 def offset @options.fetch(:offset, 0) end |
#reverse ⇒ Object
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
44 45 46 |
# File 'lib/ohm/sorted.rb', line 44 def reversed? @options.fetch(:reverse, false) end |
#size ⇒ Object
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 |