ohm-sorted 0.3.1 → 0.3.2

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
- YTQwNzhmZDJmMjY0ZDBiNDg1MzdlZDExZTI1ZjE2NjFhMGY3ZDc0NQ==
4
+ NzM0ZTQ4YmIwNjYzYjUzNTlhOTQwYjgxNGVkNWRlYzhjMzJiNTFlMQ==
5
5
  data.tar.gz: !binary |-
6
- N2YyOGE2NzQwMmM4ZjZjYjAzYjA4NzU4MzlkMjBiNWI1ODhkYTg3Yg==
7
- !binary "U0hBNTEy":
6
+ YWU4MmQ1MTU2ZjAwOGU4MTY2YTgwNmEwYmM3MDk5YmQxOTVjNmU2ZQ==
7
+ SHA512:
8
8
  metadata.gz: !binary |-
9
- YzY1NTEzOTM3NGM4N2I2MjA1ZTBhNjFlMjZhNTRiYmYxNjNhNmYwMjkwMGVm
10
- Y2Q2NjE3NTUxZDg1Y2YwNWExOGE1ODc3MjBiZjU0NWViZmU5YjUxZDA3MDYy
11
- NGU3OWQwODc0ZjMxOGJiMzU2ODJhZjJmOTkzMzczYzFmYzNhNGY=
9
+ ZTNmZDhhMzlkNGMzOWZlMmE2ZTA4M2MxYWIwNDlhOWY4YzBhYWVlMzliYzlj
10
+ ZjMwMGZiNTdjZDQ0ZjViYmQxOWQ4YmI1OWZiYmI5OTc2MTE4NGVlMzU2NzMx
11
+ Y2JmMzNmYmJkMzY0MzBlNmQ2MDgxMjU5YjJkZjFlODdmOTQ2MGM=
12
12
  data.tar.gz: !binary |-
13
- YjVjMTljNzBlMjMzNGQyYzI0ODBhNWIyMTQ5NDU0ZmQ3ZjgxNDM1M2FjYzBi
14
- M2I2NTQ1ZDE5YzE0YTUxYWU4YjI4NTVjYWU4NzczMzk5YmFjMDdlMjdjMzU2
15
- OGQ4MDFjNGE0OWM3NTk2MzA1ODA0ZjZmYjFjNWQwZjgzNTEyODg=
13
+ MjM5NTYxMGQ0MjRmMjNkOGZlZjA2ODdlMWJiMzk2MTcyMjRhYmI0NDZlZThj
14
+ NTI2MjY2YThjZThkMzdmNjQyNmM1MjEzOGMzMjIyZTQ4OTIzMzgzZDkwMGFm
15
+ NDk4MDAzZjE3MmMwOTFlNzNjNDBhOWZlYzVhNDFkYzliYWZiNWM=
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ### 0.3.2
2
+
3
+ - You can now use `SortedSet#reverse` to query in reverse order.
4
+
5
+ For instance:
6
+
7
+ ```
8
+ Post.sorted_find(:created_at).reverse.between(Time.now - 10, Time.now)
9
+ ```
10
+
11
+ - `SortedSet#each` now returns an Enumerator when called without a block
12
+
1
13
  ### 0.3.1
2
14
 
3
15
  - Return correct set size for ranged sets
data/Gemfile CHANGED
@@ -1,5 +1,2 @@
1
1
  source "https://rubygems.org"
2
-
3
- gem "appraisal"
4
- gem "ohm"
5
- gem "ohm-contrib"
2
+ gemspec
data/Rakefile CHANGED
@@ -1,6 +1,5 @@
1
1
  require "rake/testtask"
2
2
  require "appraisal"
3
- require "bundler/setup"
4
3
 
5
4
  REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__)
6
5
  REDIS_CNF = File.join(REDIS_DIR, "test.conf")
data/lib/ohm/sorted.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require 'ohm'
2
- require 'ohm/contrib'
2
+
3
+ begin
4
+ require 'ohm/callbacks'
5
+ rescue LoadError
6
+ require 'ohm/contrib/callbacks'
7
+ end
3
8
 
4
9
  module Ohm
5
10
 
@@ -13,6 +18,7 @@ module Ohm
13
18
  @namespace = namespace
14
19
  @model = model
15
20
  @options = options
21
+ @range = options.fetch(:range, ["-inf", "inf"])
16
22
  end
17
23
 
18
24
  def offset
@@ -28,11 +34,23 @@ module Ohm
28
34
  end
29
35
 
30
36
  def between(first, last)
31
- range = first.to_f..last.to_f
37
+ range = [first.to_f, last.to_f]
38
+ range.reverse! if reversed?
39
+
32
40
  opts = @options.merge(range: range)
33
41
  RangedSortedSet.new(key, namespace, model, opts)
34
42
  end
35
43
 
44
+ def reversed?
45
+ @options.fetch(:reverse, false)
46
+ end
47
+
48
+ def reverse
49
+ opts = @options.merge(reverse: !reversed?, range: @range.reverse)
50
+
51
+ self.class.new(key, namespace, model, opts)
52
+ end
53
+
36
54
  def slice(*args)
37
55
  if args.count == 1
38
56
  self[args.first]
@@ -50,7 +68,11 @@ module Ohm
50
68
  end
51
69
 
52
70
  def ids
53
- execute { |key| db.zrangebyscore(key, "-inf", "inf", limit: [offset, count]) }
71
+ if reversed?
72
+ execute { |key| db.zrevrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
73
+ else
74
+ execute { |key| db.zrangebyscore(key, @range.first, @range.last, limit: [offset, count]) }
75
+ end
54
76
  end
55
77
 
56
78
  def inspect
@@ -83,8 +105,12 @@ module Ohm
83
105
  model.db
84
106
  end
85
107
 
86
- def each(&block)
87
- ids.each { |id| block.call(model.to_proc[id]) }
108
+ def each
109
+ if block_given?
110
+ ids.each { |id| block.call(model.to_proc[id]) }
111
+ else
112
+ to_enum(:each)
113
+ end
88
114
  end
89
115
 
90
116
  def [](id)
@@ -111,16 +137,8 @@ module Ohm
111
137
  end
112
138
 
113
139
  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
140
  def size
123
- execute { |key| db.zcount(key, range.begin, range.end) }
141
+ execute { |key| db.zcount(key, @range.first, @range.last) }
124
142
  end
125
143
  end
126
144
 
@@ -153,9 +171,8 @@ module Ohm
153
171
 
154
172
  def sorted_index_key(attribute, dict={})
155
173
  index_key = [key, "sorted", attribute]
156
- if dict.keys.size == 1
157
- index_key << dict.keys.first
158
- index_key << dict.values.first
174
+ if dict.size == 1
175
+ index_key.concat(dict.first)
159
176
  elsif dict.keys.size > 1
160
177
  raise ArgumentError
161
178
  end
@@ -228,9 +245,7 @@ module Ohm
228
245
  end
229
246
 
230
247
  def update_sorted_indices
231
- self.class.sorted_indices.each do |args|
232
- attribute, options = *args
233
-
248
+ self.class.sorted_indices.each do |attribute, options|
234
249
  opts = {}
235
250
  if options.include?(:group_by)
236
251
  group_by = options[:group_by]
data/ohm-sorted.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'ohm-sorted'
3
- s.version = '0.3.1'
3
+ s.version = '0.3.2'
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"
@@ -8,4 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.files = `git ls-files`.split("\n")
9
9
  s.homepage = 'https://github.com/educabilia/ohm-sorted'
10
10
  s.license = 'UNLICENSE'
11
+
12
+ s.add_development_dependency "appraisal"
13
+ s.add_development_dependency "ohm"
14
+ s.add_development_dependency "ohm-contrib"
11
15
  end
data/test/sorted_test.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'test/unit'
2
- require 'ohm/sorted'
2
+ require_relative '../lib/ohm/sorted'
3
3
 
4
4
  class Post < Ohm::Model
5
5
  include Ohm::Callbacks
@@ -158,4 +158,47 @@ class SortedTest < Test::Unit::TestCase
158
158
  sorted_set = Post.sorted_find(:order)
159
159
  assert !sorted_set.empty?
160
160
  end
161
+
162
+ def test_sorted_find_reverse
163
+ posts = []
164
+
165
+ posts << Post.create(order: 3)
166
+ posts << Post.create(order: 2)
167
+ posts << Post.create(order: 1)
168
+
169
+ assert_equal posts, Post.sorted_find(:order).reverse.to_a
170
+
171
+ assert_equal posts[0..1], Post.sorted_find(:order).reverse.slice(0, 2).to_a
172
+ assert_equal posts[1..2], Post.sorted_find(:order).reverse.slice(1, 2).to_a
173
+
174
+ assert_equal posts.reverse, Post.sorted_find(:order).reverse.reverse.to_a
175
+ end
176
+
177
+ def test_sorted_find_reverse_range
178
+ posts = []
179
+
180
+ posts << Post.create(order: 3)
181
+ posts << Post.create(order: 2)
182
+ posts << Post.create(order: 1)
183
+
184
+ assert_equal posts, Post.sorted_find(:order).reverse.between(1, 3).to_a
185
+
186
+ assert_equal posts[0..1], Post.sorted_find(:order).reverse.between(2, 3).to_a
187
+ end
188
+
189
+ def test_sorted_find_reverse_range_conmutative
190
+ posts = []
191
+
192
+ posts << Post.create(order: 3)
193
+ posts << Post.create(order: 2)
194
+ posts << Post.create(order: 1)
195
+
196
+ assert_equal posts, Post.sorted_find(:order).between(1, 3).reverse.to_a
197
+
198
+ assert_equal posts[0..1], Post.sorted_find(:order).between(2, 3).reverse.to_a
199
+ end
200
+
201
+ def test_each_without_block_returns_enumerable
202
+ assert Post.sorted_find(:order).each.kind_of?(Enumerator)
203
+ end
161
204
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm-sorted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Bond
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-10 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-10-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appraisal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ohm
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ohm-contrib
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
13
55
  description: An plugin for Ohm that lets you create sorted indices.
14
56
  email: federico@educabilia.com
15
57
  executables: []
@@ -48,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
90
  version: '0'
49
91
  requirements: []
50
92
  rubyforge_project:
51
- rubygems_version: 2.0.7
93
+ rubygems_version: 2.1.5
52
94
  signing_key:
53
95
  specification_version: 4
54
96
  summary: Sorted indices for Ohm.