ohm-sorted 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmEwYzFiOTIwOGU3OWZhMGM2OTMzY2M4OTM1NTdmZTQ5Yzc4ZmUxMQ==
4
+ YTQwNzhmZDJmMjY0ZDBiNDg1MzdlZDExZTI1ZjE2NjFhMGY3ZDc0NQ==
5
5
  data.tar.gz: !binary |-
6
- MGQ3YzdhZjUwYTQwNzJkZjJjOWEyOTQzYzQxNmJmYjFhNWI3YTc5Ng==
6
+ N2YyOGE2NzQwMmM4ZjZjYjAzYjA4NzU4MzlkMjBiNWI1ODhkYTg3Yg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MzE1Y2VjYzZiYjFkZjU2ZDExZWI5ZDYxMTllZmYyYWQ1ZWUzNDVmYTk5YmVi
10
- NGFmMDExNmU4OTc0ZThjNzE3ZDRiZGZmNDBjYzk3NWQxZGY1MDRhZjNmYWEx
11
- M2Y1YTJjNzBhYjc4Mjk3ZjgxMTVjOWY1ODg1Y2I0ZDkxZDdhN2Y=
9
+ YzY1NTEzOTM3NGM4N2I2MjA1ZTBhNjFlMjZhNTRiYmYxNjNhNmYwMjkwMGVm
10
+ Y2Q2NjE3NTUxZDg1Y2YwNWExOGE1ODc3MjBiZjU0NWViZmU5YjUxZDA3MDYy
11
+ NGU3OWQwODc0ZjMxOGJiMzU2ODJhZjJmOTkzMzczYzFmYzNhNGY=
12
12
  data.tar.gz: !binary |-
13
- NjEzNTQ1M2JkYzk4MmQ3YmVjZmNkZjE0ZTc5NWQwYThiZDUzMDJjNjdmZTI3
14
- Nzg1NDVmNzc2YTRhZGEwMjE0NTM0YjM5ZTEzZGEzMmZiN2FkOWY0ZjczYjZm
15
- YjQ1MDM5ZTVmOWU1ZTQwMjg5MzEzMjY1ZjNlZGIzOTZjZDE4ZTc=
13
+ YjVjMTljNzBlMjMzNGQyYzI0ODBhNWIyMTQ5NDU0ZmQ3ZjgxNDM1M2FjYzBi
14
+ M2I2NTQ1ZDE5YzE0YTUxYWU4YjI4NTVjYWU4NzczMzk5YmFjMDdlMjdjMzU2
15
+ OGQ4MDFjNGE0OWM3NTk2MzA1ODA0ZjZmYjFjNWQwZjgzNTEyODg=
@@ -1,3 +1,11 @@
1
+ ### 0.3.1
2
+
3
+ - Return correct set size for ranged sets
4
+
5
+ ### 0.3.0
6
+
7
+ - Simplify query API
8
+
1
9
  ### 0.2.1
2
10
 
3
11
  - Fix empty? method for SortedSet in Ohm legacy
@@ -15,10 +15,6 @@ module Ohm
15
15
  @options = options
16
16
  end
17
17
 
18
- def range
19
- @options.fetch(:range, "-inf".."inf")
20
- end
21
-
22
18
  def offset
23
19
  @options.fetch(:offset, 0)
24
20
  end
@@ -27,10 +23,14 @@ module Ohm
27
23
  @options.fetch(:count, -1)
28
24
  end
29
25
 
26
+ def size
27
+ execute { |key| db.zcard(key) }
28
+ end
29
+
30
30
  def between(first, last)
31
31
  range = first.to_f..last.to_f
32
32
  opts = @options.merge(range: range)
33
- SortedSet.new(key, namespace, model, opts)
33
+ RangedSortedSet.new(key, namespace, model, opts)
34
34
  end
35
35
 
36
36
  def slice(*args)
@@ -39,17 +39,22 @@ module Ohm
39
39
  elsif args.count == 2
40
40
  offset, count = *args
41
41
  opts = @options.merge(offset: offset, count: count)
42
- SortedSet.new(key, namespace, model, opts)
42
+ self.class.new(key, namespace, model, opts)
43
43
  else
44
44
  raise ArgumentError
45
45
  end
46
46
  end
47
47
 
48
+ def first
49
+ slice(0, 1).to_a.first
50
+ end
51
+
48
52
  def ids
49
- execute do |key|
50
- db.zrangebyscore(key, range.begin, range.end,
51
- limit: [offset, count])
52
- end
53
+ execute { |key| db.zrangebyscore(key, "-inf", "inf", limit: [offset, count]) }
54
+ end
55
+
56
+ def inspect
57
+ "#<SortedSet (#{model}): #{ids}>"
53
58
  end
54
59
  end
55
60
 
@@ -57,14 +62,6 @@ module Ohm
57
62
  class SortedSet < BasicSet
58
63
  include SortedMethods
59
64
 
60
- def size
61
- execute { |key| db.zcard(key) }
62
- end
63
-
64
- def first
65
- fetch(execute { |key| db.zrange(key, 0, 1) }).first
66
- end
67
-
68
65
  private
69
66
  def exists?(id)
70
67
  execute { |key| !!db.zscore(key, id) }
@@ -82,17 +79,6 @@ module Ohm
82
79
  class SortedSet < Model::Collection
83
80
  include Ohm::SortedMethods
84
81
 
85
- attr :key
86
- attr :namespace
87
- attr :model
88
-
89
- def initialize(key, namespace, model, options={})
90
- @key = key
91
- @model = model
92
- @namespace = namespace
93
- @options = options
94
- end
95
-
96
82
  def db
97
83
  model.db
98
84
  end
@@ -105,10 +91,6 @@ module Ohm
105
91
  model[id] if !!db.zrank(key, id)
106
92
  end
107
93
 
108
- def size
109
- db.zcard(key)
110
- end
111
-
112
94
  def empty?
113
95
  size == 0
114
96
  end
@@ -117,19 +99,10 @@ module Ohm
117
99
  ids.map(&model)
118
100
  end
119
101
 
120
- def first
121
- id = db.zrange(key, 0, 1).first
122
- model[id] unless id.empty?
123
- end
124
-
125
102
  def include?(model)
126
103
  !!db.zrank(key, model.id)
127
104
  end
128
105
 
129
- def inspect
130
- "#<SortedSet (#{model}): #{db.zrange(key, 0, -1).inspect}>"
131
- end
132
-
133
106
  private
134
107
  def execute
135
108
  yield key
@@ -137,6 +110,20 @@ module Ohm
137
110
  end
138
111
  end
139
112
 
113
+ class RangedSortedSet < SortedSet
114
+ def range
115
+ @options.fetch(:range)
116
+ end
117
+
118
+ def ids
119
+ execute { |key| db.zrangebyscore(key, range.begin, range.end, limit: [offset, count]) }
120
+ end
121
+
122
+ def size
123
+ execute { |key| db.zcount(key, range.begin, range.end) }
124
+ end
125
+ end
126
+
140
127
  module Sorted
141
128
  def self.included(model)
142
129
  model.extend(ClassMethods)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ohm-sorted'
3
- s.version = '0.3.0'
3
+ s.version = '0.3.1'
4
4
  s.summary = "Sorted indices for Ohm."
5
5
  s.description = "An plugin for Ohm that lets you create sorted indices."
6
6
  s.author = "Federico Bond"
@@ -140,6 +140,15 @@ class SortedTest < Test::Unit::TestCase
140
140
  assert_equal 1, sorted_set.size
141
141
  end
142
142
 
143
+ def test_sorted_set_between_size
144
+ 4.times do |i|
145
+ Post.create(order: i)
146
+ end
147
+
148
+ sorted_set = Post.sorted_find(:order).between(2, 3)
149
+ assert_equal 2, sorted_set.size
150
+ end
151
+
143
152
  def test_sorted_set_empty
144
153
  sorted_set = Post.sorted_find(:order)
145
154
  assert sorted_set.empty?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-sorted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Bond