redis_pagination 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.rspec +2 -1
- data/CHANGELOG.md +7 -1
- data/LICENSE +1 -1
- data/README.md +25 -19
- data/lib/redis_pagination/paginator/list_paginator.rb +13 -3
- data/lib/redis_pagination/paginator/none_paginator.rb +1 -1
- data/lib/redis_pagination/paginator/sorted_set_paginator.rb +24 -3
- data/lib/redis_pagination/version.rb +1 -1
- data/redis_pagination.gemspec +1 -0
- data/spec/redis_pagination/paginator/list_paginator_spec.rb +11 -0
- data/spec/redis_pagination/paginator/sorted_set_paginator_spec.rb +11 -0
- data/spec/redis_pagination/version_spec.rb +1 -1
- metadata +7 -14
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZTViNTg2OWQ3Y2U3ZTQyNmQ0ZGRiMjM0ZTBlM2QwZjY2MjcxNmJhMQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NzE2YWE1NTFlMmZjOTY2NDdkZWFlNGJjYWYzMWM2MjZkOWQyOGEzMA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZjZjOWU5MjU1MTUxM2YyODkwMzhkMDBkOTFmMmIwYmI0YWFkMWE3MjU1OTNm
|
10
|
+
NGU0MDlmYjg5YzcwZDY4MTEzZjZlYjgwNDU4ODZkZDJlYjA1Mzc4ZGFjYzA4
|
11
|
+
YjZiYjA2NTlhMGQxOWFkZTgzOWJmMjQ3NDc3NTY1NTg1NDg2NWI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MzdiYWYzZTA1OTlmNWNjZjNiNWY1NzNlNTFiYzMwMDQyZjQwNTRlNDk5NmE0
|
14
|
+
ZGJkMjI5Yzk5YjIxNDY5NjA2YzUyZWYxZmM4OGQ0ZWQ3YmVhNTAxMzQyNmFk
|
15
|
+
MWMwNzI2ZDg1MzdjYzRmZjMyNzM1NjlkOGNiYzE5Y2NkZDY3NDk=
|
data/.rspec
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
# CHANGELOG
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
## 1.1.0 (2014-05-14)
|
4
|
+
|
5
|
+
* Added `#all` method to `RedisPagination::Paginator::SortedSetPaginator` and
|
6
|
+
`RedisPagination::Paginator::ListPaginator` to retrieve all elements
|
7
|
+
from these data types for a given key.
|
2
8
|
|
3
9
|
## 1.0.0 (2012-07-27)
|
4
10
|
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Simple pagination for Redis lists and sorted sets.
|
4
4
|
|
5
|
-
Make sure your redis server is running! Redis configuration is outside the scope of this README, but
|
5
|
+
Make sure your redis server is running! Redis configuration is outside the scope of this README, but
|
6
6
|
check out the [Redis documentation](http://redis.io/documentation) for more information.
|
7
7
|
|
8
8
|
## Installation
|
@@ -30,7 +30,7 @@ $ gem install redis_pagination
|
|
30
30
|
Configure redis_pagination:
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
|
33
|
+
RedisPagination.configure do |configuration|
|
34
34
|
configuration.redis = Redis.new
|
35
35
|
configuration.page_size = 25
|
36
36
|
end
|
@@ -48,15 +48,15 @@ end
|
|
48
48
|
|
49
49
|
# List
|
50
50
|
add_items_to_list('items', RedisPagination.page_size + 2)
|
51
|
-
=> 1
|
51
|
+
=> 1
|
52
52
|
items_paginator = RedisPagination.paginate('items')
|
53
|
-
=> #<RedisPagination::Paginator::ListPaginator:0x007f8109b08ba0 @key="items">
|
53
|
+
=> #<RedisPagination::Paginator::ListPaginator:0x007f8109b08ba0 @key="items">
|
54
54
|
items_paginator.total_items
|
55
|
-
=> 27
|
55
|
+
=> 27
|
56
56
|
items_paginator.total_pages
|
57
|
-
=> 2
|
57
|
+
=> 2
|
58
58
|
items_paginator.total_pages(5)
|
59
|
-
=> 6
|
59
|
+
=> 6
|
60
60
|
items = items_paginator.page(1)
|
61
61
|
=> {:current_page=>1, :total_pages=>2, :total_items=>27, :items=>["item_1", "item_2", "item_3", "item_4", "item_5", "item_6", "item_7", "item_8", "item_9", "item_10", "item_11", "item_12", "item_13", "item_14", "item_15", "item_16", "item_17", "item_18", "item_19", "item_20", "item_21", "item_22", "item_23", "item_24", "item_25"]}
|
62
62
|
items = items_paginator.page(2)
|
@@ -64,15 +64,15 @@ items = items_paginator.page(2)
|
|
64
64
|
|
65
65
|
# Sorted Set
|
66
66
|
add_items_to_sorted_set('items', RedisPagination.page_size + 2)
|
67
|
-
=> 1
|
67
|
+
=> 1
|
68
68
|
items_paginator = RedisPagination.paginate('items')
|
69
|
-
=> #<RedisPagination::Paginator::SortedSetPaginator:0x007f8109a25828 @key="items">
|
69
|
+
=> #<RedisPagination::Paginator::SortedSetPaginator:0x007f8109a25828 @key="items">
|
70
70
|
items_paginator.total_items
|
71
|
-
=> 27
|
71
|
+
=> 27
|
72
72
|
items_paginator.total_pages
|
73
|
-
=> 2
|
73
|
+
=> 2
|
74
74
|
items_paginator.total_pages(5)
|
75
|
-
=> 6
|
75
|
+
=> 6
|
76
76
|
items = items_paginator.page(1)
|
77
77
|
=> {:current_page=>1, :total_pages=>2, :total_items=>27, :items=>[["item_27", 27.0], ["item_26", 26.0], ["item_25", 25.0], ["item_24", 24.0], ["item_23", 23.0], ["item_22", 22.0], ["item_21", 21.0], ["item_20", 20.0], ["item_19", 19.0], ["item_18", 18.0], ["item_17", 17.0], ["item_16", 16.0], ["item_15", 15.0], ["item_14", 14.0], ["item_13", 13.0], ["item_12", 12.0], ["item_11", 11.0], ["item_10", 10.0], ["item_9", 9.0], ["item_8", 8.0], ["item_7", 7.0], ["item_6", 6.0], ["item_5", 5.0], ["item_4", 4.0], ["item_3", 3.0]]}
|
78
78
|
items = items_paginator.page(2)
|
@@ -82,19 +82,25 @@ items = items_paginator.page(1, :with_scores => false, :reverse => false)
|
|
82
82
|
|
83
83
|
# If the key is non-existent, the paginate call will return a RedisPagination::Paginator::NonePaginator
|
84
84
|
items_paginator = RedisPagination.paginate('unknown-key-in-redis')
|
85
|
-
=> #<RedisPagination::Paginator::NonePaginator:0x007f956b8052c0>
|
85
|
+
=> #<RedisPagination::Paginator::NonePaginator:0x007f956b8052c0>
|
86
86
|
items_paginator.total_items
|
87
|
-
=> 0
|
87
|
+
=> 0
|
88
88
|
items_paginator.total_pages
|
89
|
-
=> 0
|
89
|
+
=> 0
|
90
90
|
items_paginator.total_pages(5)
|
91
|
-
=> 0
|
91
|
+
=> 0
|
92
92
|
items = items_paginator.page(1)
|
93
|
-
=> {:current_page=>1, :total_pages=>0, :total_items=>0, :items=>[]}
|
93
|
+
=> {:current_page=>1, :total_pages=>0, :total_items=>0, :items=>[]}
|
94
94
|
items = items_paginator.page(2)
|
95
95
|
=> {:current_page=>2, :total_pages=>0, :total_items=>0, :items=>[]}
|
96
96
|
```
|
97
97
|
|
98
|
+
## Retrieving All Members
|
99
|
+
|
100
|
+
The `RedisPagination::Paginator::SortedSetPaginator` and `RedisPagination::Paginator::ListPaginator`
|
101
|
+
classes have an `all` method that can be called to retrieve all elements from these data types for
|
102
|
+
a given key.
|
103
|
+
|
98
104
|
## Paging Options
|
99
105
|
|
100
106
|
Valid options in the `page` call for paginating a Redis list are:
|
@@ -105,9 +111,9 @@ Valid options in the `page` call for paginating a Redis sorted set are:
|
|
105
111
|
|
106
112
|
* `:page_size` controls the page size for the call. Default is `RedisPagination.page_size`.
|
107
113
|
* `:with_scores` controls whether the score is returned along with the item. Default is `true`.
|
108
|
-
* `:reverse controls
|
114
|
+
* `:reverse` controls whether to return items in highest-to-lowest (`true`) or lowest-to-highest order (`false`). Default is `true`.
|
109
115
|
|
110
|
-
## Differences in Redis Client Libraries
|
116
|
+
## Differences in Redis Client Libraries
|
111
117
|
|
112
118
|
There is a difference between how sorted set data with scores is returned between the 2.x and the 3.x branch of the Ruby Redis client library.
|
113
119
|
|
@@ -12,7 +12,7 @@ module RedisPagination
|
|
12
12
|
# Return the total number of pages for +key+.
|
13
13
|
#
|
14
14
|
# @param page_size [int] Page size to calculate total number of pages.
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# @return the total number of pages for +key+.
|
17
17
|
def total_pages(page_size = RedisPagination.page_size)
|
18
18
|
(RedisPagination.redis.llen(@key) / page_size.to_f).ceil
|
@@ -29,7 +29,7 @@ module RedisPagination
|
|
29
29
|
#
|
30
30
|
# @param page [int] Page of items to retrieve.
|
31
31
|
# @param options [Hash] Options. Valid options are :page_size.
|
32
|
-
# :page_size controls the page size for the call. Default is +RedisPagination.page_size+.
|
32
|
+
# :page_size controls the page size for the call. Default is +RedisPagination.page_size+.
|
33
33
|
#
|
34
34
|
# @return a +Hash+ containing +:current_page+, +:total_pages+, +:total_items+ and +:items+.
|
35
35
|
def page(page, options = {})
|
@@ -44,7 +44,17 @@ module RedisPagination
|
|
44
44
|
:total_pages => total_pages(page_size),
|
45
45
|
:total_items => total_items,
|
46
46
|
:items => RedisPagination.redis.lrange(@key, starting_offset, ending_offset)
|
47
|
-
}
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
# Retrieve all items for +key+.
|
51
|
+
#
|
52
|
+
# @return a +Hash+ containing +:total_items+ and +:items+.
|
53
|
+
def all(options = {})
|
54
|
+
{
|
55
|
+
:total_items => total_items,
|
56
|
+
:items => RedisPagination.redis.lrange(@key, 0, -1)
|
57
|
+
}
|
48
58
|
end
|
49
59
|
end
|
50
60
|
end
|
@@ -3,7 +3,7 @@ module RedisPagination
|
|
3
3
|
class NonePaginator
|
4
4
|
# Initialize a new instance with a given Redis +key+ and options.
|
5
5
|
#
|
6
|
-
# @param key [String] Redis
|
6
|
+
# @param key [String] Redis key.
|
7
7
|
# @param options [Hash] Options for paginator.
|
8
8
|
def initialize(key, options = {})
|
9
9
|
end
|
@@ -3,7 +3,7 @@ module RedisPagination
|
|
3
3
|
class SortedSetPaginator
|
4
4
|
# Initialize a new instance with a given Redis +key+ and options.
|
5
5
|
#
|
6
|
-
# @param key [String] Redis
|
6
|
+
# @param key [String] Redis sorted set key.
|
7
7
|
# @param options [Hash] Options for paginator.
|
8
8
|
def initialize(key, options = {})
|
9
9
|
@key = key
|
@@ -12,7 +12,7 @@ module RedisPagination
|
|
12
12
|
# Return the total number of pages for +key+.
|
13
13
|
#
|
14
14
|
# @param page_size [int] Page size to calculate total number of pages.
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# @return the total number of pages for +key+.
|
17
17
|
def total_pages(page_size = RedisPagination.page_size)
|
18
18
|
(RedisPagination.redis.zcard(@key) / page_size.to_f).ceil
|
@@ -53,7 +53,28 @@ module RedisPagination
|
|
53
53
|
else
|
54
54
|
RedisPagination.redis.zrange(@key, starting_offset, ending_offset, :with_scores => with_scores)
|
55
55
|
end
|
56
|
-
}
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
# Retrieve all items for +key+.
|
60
|
+
#
|
61
|
+
# @param options [Hash] Options. Valid options are :with_scores and :reverse.
|
62
|
+
# :with_scores controls whether the score is returned along with the item. Default is +true+.
|
63
|
+
# :reverse controls whether to return items in highest-to-lowest (+true+) or loweest-to-highest order (+false+). Default is +true+.
|
64
|
+
#
|
65
|
+
# @return a +Hash+ containing +:total_items+ and +:items+.
|
66
|
+
def all(options = {})
|
67
|
+
with_scores = options.has_key?(:with_scores) ? options[:with_scores] : true
|
68
|
+
reverse = options.has_key?(:reverse) ? options[:reverse] : true
|
69
|
+
|
70
|
+
{
|
71
|
+
:total_items => total_items,
|
72
|
+
:items => if reverse
|
73
|
+
RedisPagination.redis.zrevrange(@key, 0, -1, :with_scores => with_scores)
|
74
|
+
else
|
75
|
+
RedisPagination.redis.zrange(@key, 0, -1, :with_scores => with_scores)
|
76
|
+
end
|
77
|
+
}
|
57
78
|
end
|
58
79
|
end
|
59
80
|
end
|
data/redis_pagination.gemspec
CHANGED
@@ -7,6 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.description = %q{Simple pagination for Redis lists and sorted sets.}
|
8
8
|
gem.summary = %q{Simple pagination for Redis lists and sorted sets.}
|
9
9
|
gem.homepage = 'https://github.com/czarneckid/redis_pagination'
|
10
|
+
gem.license = 'MIT'
|
10
11
|
|
11
12
|
gem.files = `git ls-files`.split($\)
|
12
13
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -56,4 +56,15 @@ describe RedisPagination::Paginator::ListPaginator do
|
|
56
56
|
result[:total_pages].should == 6
|
57
57
|
end
|
58
58
|
end
|
59
|
+
|
60
|
+
describe '#all' do
|
61
|
+
it 'should return all the items with the default options' do
|
62
|
+
add_items_to_sorted_set('items', 50)
|
63
|
+
items_paginator = RedisPagination.paginate('items')
|
64
|
+
items_paginator.all.tap do |all_items|
|
65
|
+
all_items[:total_items].should == 50
|
66
|
+
all_items[:items].length.should == 50
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
59
70
|
end
|
@@ -61,4 +61,15 @@ describe RedisPagination::Paginator::SortedSetPaginator do
|
|
61
61
|
result[:total_pages].should == 6
|
62
62
|
end
|
63
63
|
end
|
64
|
+
|
65
|
+
describe '#all' do
|
66
|
+
it 'should return all the items with the default options' do
|
67
|
+
add_items_to_sorted_set('items', 50)
|
68
|
+
items_paginator = RedisPagination.paginate('items')
|
69
|
+
items_paginator.all.tap do |all_items|
|
70
|
+
all_items[:total_items].should == 50
|
71
|
+
all_items[:items].length.should == 50
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
64
75
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- David Czarnecki
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: redis
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -90,28 +83,28 @@ files:
|
|
90
83
|
- spec/redis_pagination/version_spec.rb
|
91
84
|
- spec/spec_helper.rb
|
92
85
|
homepage: https://github.com/czarneckid/redis_pagination
|
93
|
-
licenses:
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
94
89
|
post_install_message:
|
95
90
|
rdoc_options: []
|
96
91
|
require_paths:
|
97
92
|
- lib
|
98
93
|
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
-
none: false
|
100
94
|
requirements:
|
101
95
|
- - ! '>='
|
102
96
|
- !ruby/object:Gem::Version
|
103
97
|
version: '0'
|
104
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
99
|
requirements:
|
107
100
|
- - ! '>='
|
108
101
|
- !ruby/object:Gem::Version
|
109
102
|
version: '0'
|
110
103
|
requirements: []
|
111
104
|
rubyforge_project:
|
112
|
-
rubygems_version:
|
105
|
+
rubygems_version: 2.2.2
|
113
106
|
signing_key:
|
114
|
-
specification_version:
|
107
|
+
specification_version: 4
|
115
108
|
summary: Simple pagination for Redis lists and sorted sets.
|
116
109
|
test_files:
|
117
110
|
- spec/redis_pagination/configuration_spec.rb
|