Class: MockRedis::Zset

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mock_redis/zset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZset

Returns a new instance of Zset.



13
14
15
16
# File 'lib/mock_redis/zset.rb', line 13

def initialize
  @members = Set.new
  @scores  = Hash.new
end

Instance Attribute Details

#membersObject (readonly)

Returns the value of attribute members.



9
10
11
# File 'lib/mock_redis/zset.rb', line 9

def members
  @members
end

#scoresObject (readonly)

Returns the value of attribute scores.



9
10
11
# File 'lib/mock_redis/zset.rb', line 9

def scores
  @scores
end

Instance Method Details

#add(score, member) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mock_redis/zset.rb', line 24

def add(score, member)
  members.add(member)
  if score.to_f.to_i == score.to_f
    scores[member] = score.to_f.to_i
  else
    scores[member] = score.to_f
  end
  self
end

#delete?(member) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/mock_redis/zset.rb', line 34

def delete?(member)
  scores.delete(member)
  members.delete?(member) and self
end

#eachObject



39
40
41
# File 'lib/mock_redis/zset.rb', line 39

def each
  members.each {|m| yield score(m), m}
end

#in_range(min, max) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mock_redis/zset.rb', line 43

def in_range(min, max)
  in_from_the_left = case min
                     when "-inf"
                         lambda {|_| true }
                     when "+inf"
                         lambda {|_| false }
                     when /\((.*)$/
                         val = $1.to_f
                       lambda {|x| x.to_f > val }
                     else
                       lambda {|x| x.to_f >= min.to_f }
                     end

  in_from_the_right = case max
                      when "-inf"
                          lambda {|_| false }
                      when "+inf"
                          lambda {|_| true }
                      when /\((.*)$/
                          val = $1.to_f
                        lambda {|x| x.to_f < val }
                      else
                        lambda {|x| x.to_f <= max.to_f }
                      end

  sorted.find_all do |(score, member)|
    in_from_the_left[score] && in_from_the_right[score]
  end
end

#initialize_copy(source) ⇒ Object



18
19
20
21
22
# File 'lib/mock_redis/zset.rb', line 18

def initialize_copy(source)
  super
  @members = @members.clone
  @scores  = @scores.clone
end

#intersection(other) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/mock_redis/zset.rb', line 73

def intersection(other)
  if !block_given?
    intersection(other, &:+)
  else
    self.members.intersection(other.members).reduce(self.class.new) do |acc, m|
      new_score = yield(self.score(m), other.score(m))
      acc.add(new_score, m)
    end
  end
end

#score(member) ⇒ Object



84
85
86
# File 'lib/mock_redis/zset.rb', line 84

def score(member)
  scores[member]
end

#sortedObject



88
89
90
91
92
# File 'lib/mock_redis/zset.rb', line 88

def sorted
  members.map do |m|
    [score(m), m]
  end.sort_by(&:first)
end

#sorted_membersObject



94
95
96
# File 'lib/mock_redis/zset.rb', line 94

def sorted_members
  sorted.map(&:last)
end

#union(other) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/mock_redis/zset.rb', line 98

def union(other)
  if !block_given?
    union(other, &:+)
  else
    self.members.union(other.members).reduce(self.class.new) do |acc, m|
      new_score = yield(self.score(m), other.score(m))
      acc.add(new_score, m)
    end
  end
end