Class: ROC::SortedSet

Inherits:
Base
  • Object
show all
Extended by:
Types::MethodGenerators
Includes:
Types::ArrayType, Types::SortableType
Defined in:
lib/roc/objects/sorted_set.rb

Instance Attribute Summary

Attributes inherited from Base

#key, #options

Instance Method Summary collapse

Methods included from Types::MethodGenerators

deserializing_method, nonserializing_method, serializing_and_deserializing_method, serializing_method, zero_arg_method

Methods included from Types::SortableType

#sort, #sort!

Methods included from Types::ArrayType

#clear, #delete_at, #delete_if, #fill, included, #insert, #inspect, #keep_if, #pop, #replace, #shift, #to_a, #to_array, #to_ary, #unshift, #values=

Methods inherited from Base

delegate_methods, #initialize, #method_missing, #respond_to?, #seed

Methods included from Types::AllTypes

#eval

Constructor Details

This class inherits a constructor from ROC::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ROC::Base

Instance Method Details

#<<(val_and_score) ⇒ Object



176
177
178
179
180
181
182
183
184
185
# File 'lib/roc/objects/sorted_set.rb', line 176

def <<(val_and_score)
  if val_and_score.is_a?(Array)
    self.zadd *val_and_score
  elsif val_and_score.is_a?(::Hash) && val_and_score.has_key?(:value) && val_and_score.has_key?(:score)
    self.zadd(val_and_score[:score], val_and_score[:value])
  else
    raise ArgumentError, 'an Array or a Hash required'
  end
  self
end

#[](range_or_num, num = nil) ⇒ Object Also known as: slice

shortcut methods



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roc/objects/sorted_set.rb', line 98

def [](range_or_num, num=nil)
  if range_or_num.is_a?(::Integer)
    if num.nil?
      self.zrange(range_or_num, range_or_num)[0]
    elsif num >= 0
      self.zrange(range_or_num, range_or_num + num - 1)
    else
      raise ArgumentError, 'second arg to [] must be a non-neg integer'
    end
  elsif range_or_num.is_a?(Range)
    self.zrange(range_or_num.first, (range_or_num.exclude_end? ? range_or_num.last - 1 : range_or_num.last))
  else
    if num.nil?
      self.values.slice(range_or_num)
    else
      self.values.slice(range_or_num, num)
    end
  end
end

#clobber(vals) ⇒ Object

implementing ArrayType ##



202
203
204
205
206
207
# File 'lib/roc/objects/sorted_set.rb', line 202

def clobber(vals)
  self.storage.multi do 
    self.forget
    vals.each{|v| self << v}
  end
end

#decrby(by, val) ⇒ Object

helpers



141
142
143
# File 'lib/roc/objects/sorted_set.rb', line 141

def decrby(by, val)
  self.zincrby -by, val
end

#decrement(val, by = nil) ⇒ Object



149
150
151
# File 'lib/roc/objects/sorted_set.rb', line 149

def decrement(val, by=nil)
  self.zincrby( -(by || 1), val )
end

#delete(val) ⇒ Object

implement (if posible) destructive methods that would otherwise raise



168
169
170
171
172
173
174
# File 'lib/roc/objects/sorted_set.rb', line 168

def delete(val)
  if self.zrem(val)
    val
  else
    nil
  end
end

#firstObject



119
120
121
# File 'lib/roc/objects/sorted_set.rb', line 119

def first
  self.zrange(0, 0)[0]
end

#include?(val) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/roc/objects/sorted_set.rb', line 127

def include?(val)
  !self.zrank(val).nil?
end

#increment(val, by = nil) ⇒ Object



145
146
147
# File 'lib/roc/objects/sorted_set.rb', line 145

def increment(val, by=nil)
  self.zincrby( (by || 1), val )
end

#index(val) ⇒ Object



131
132
133
# File 'lib/roc/objects/sorted_set.rb', line 131

def index(val)
  self.zrank(val)
end

#lastObject



123
124
125
# File 'lib/roc/objects/sorted_set.rb', line 123

def last
  self.zrange(-1, -1)[0]
end

#push(*objs) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/roc/objects/sorted_set.rb', line 187

def push(*objs)
  if 1 == objs.size
    self << objs[0]
  elsif objs.size > 1
    self.storage.multi do 
      objs.each do |obj|
        self << obj
      end
    end
  end
  self
end

#reverseObject



135
136
137
# File 'lib/roc/objects/sorted_set.rb', line 135

def reverse
  self.zrevrange(0, -1)
end

#to_hashObject Also known as: to_h



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/roc/objects/sorted_set.rb', line 153

def to_hash
  hsh = {}
  vals_with_scores = self.zrangebyscore('-inf', '+inf', :with_scores => true)
  i = 0
  l = vals_with_scores.size
  while i < l
    hsh[vals_with_scores[i]] = vals_with_scores[i+1]
    i += 2
  end
  hsh
end

#values(opts = {}) ⇒ Object



209
210
211
# File 'lib/roc/objects/sorted_set.rb', line 209

def values(opts = {})
  self.zrange(0, -1, opts)
end

#zadd(score, val) ⇒ Object Also known as: add



11
12
13
# File 'lib/roc/objects/sorted_set.rb', line 11

def zadd(score, val)
  self.call :zadd, score, val
end

#zcount(min, max) ⇒ Object Also known as: count



39
40
41
# File 'lib/roc/objects/sorted_set.rb', line 39

def zcount (min, max)
  self.call :zcount, min, max
end

#zincrby(by, val) ⇒ Object Also known as: incrby



53
54
55
# File 'lib/roc/objects/sorted_set.rb', line 53

def zincrby(by, val)
  self.call :zincrby, by, val
end

#zinterstore(*other_sorted_sets) ⇒ Object Also known as: interstore, inter_store, set_as_intersect_of



71
72
73
74
75
76
77
78
79
# File 'lib/roc/objects/sorted_set.rb', line 71

def zinterstore(*other_sorted_sets)
  opts = if other_sorted_sets.last.is_a?(::Hash)
           other_sorted_sets.pop
         else
           {}
         end
    
  self.call :zinterstore, [*other_sorted_sets].map{|s| s.key}, opts
end

#zrange(start_index, stop_index, opts = {}) ⇒ Object Also known as: range



19
20
21
# File 'lib/roc/objects/sorted_set.rb', line 19

def zrange(start_index, stop_index, opts={})
  self.call :zrange, start_index, stop_index, opts
end

#zrangebyscore(min, max, opts = {}) ⇒ Object Also known as: rangebyscore



29
30
31
# File 'lib/roc/objects/sorted_set.rb', line 29

def zrangebyscore(min, max, opts={})
  self.call :zrangebyscore, min, max, opts
end

#zremrangebyrank(start, stop) ⇒ Object Also known as: remrangebyrank



66
67
68
# File 'lib/roc/objects/sorted_set.rb', line 66

def zremrangebyrank(start, stop)
  self.call :zremrangebyrank, start, stop
end

#zremrangebyscore(min, max) ⇒ Object Also known as: remrangebyscore



61
62
63
# File 'lib/roc/objects/sorted_set.rb', line 61

def zremrangebyscore(min, max)
  self.call :zremrangebyscore, min, max
end

#zrevrange(start_index, stop_index, opts = {}) ⇒ Object Also known as: revrange



24
25
26
# File 'lib/roc/objects/sorted_set.rb', line 24

def zrevrange(start_index, stop_index, opts={})
  self.call :zrevrange, start_index, stop_index, opts
end

#zrevrangebyscore(max, min, opts = {}) ⇒ Object Also known as: revrangebyscore



34
35
36
# File 'lib/roc/objects/sorted_set.rb', line 34

def zrevrangebyscore(max, min, opts={})
  self.call :zrevrangebyscore, max, min, opts
end

#zunionstore(*other_sorted_sets) ⇒ Object Also known as: unionstore, union_store, set_as_union_of



84
85
86
87
88
89
90
91
# File 'lib/roc/objects/sorted_set.rb', line 84

def zunionstore(*other_sorted_sets)
  opts = if other_sorted_sets.last.is_a?(::Hash)
           other_sorted_sets.pop
         else
           {}
         end
  self.call :zunionstore, [*other_sorted_sets].map{|s| s.key}, opts
end