redis_pagination 0.0.1 → 1.0.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.
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.0.0 (2012-07-27)
4
+
5
+ * Added `RedisPagination::Paginator::NonePaginator` to handle non-existent keys.
6
+
7
+ ## 0.0.1 (2012-07-19)
8
+
9
+ * Initial release
data/README.md CHANGED
@@ -79,6 +79,20 @@ items = items_paginator.page(2)
79
79
  => {:current_page=>2, :total_pages=>2, :total_items=>27, :items=>[["item_2", 2.0], ["item_1", 1.0]]}
80
80
  items = items_paginator.page(1, :with_scores => false, :reverse => false)
81
81
  => {: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"]}
82
+
83
+ # If the key is non-existent, the paginate call will return a RedisPagination::Paginator::NonePaginator
84
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
85
+ => #<RedisPagination::Paginator::NonePaginator:0x007f956b8052c0>
86
+ items_paginator.total_items
87
+ => 0
88
+ items_paginator.total_pages
89
+ => 0
90
+ items_paginator.total_pages(5)
91
+ => 0
92
+ items = items_paginator.page(1)
93
+ => {:current_page=>1, :total_pages=>0, :total_items=>0, :items=>[]}
94
+ items = items_paginator.page(2)
95
+ => {:current_page=>2, :total_pages=>0, :total_items=>0, :items=>[]}
82
96
  ```
83
97
 
84
98
  ## Paging Options
@@ -93,7 +107,7 @@ Valid options in the `page` call for paginating a Redis sorted set are:
93
107
  * `:with_scores` controls whether the score is returned along with the item. Default is `true`.
94
108
  * `:reverse controls` whether to return items in highest-to-lowest (`true`) or loweest-to-highest order (`false`). Default is `true`.
95
109
 
96
- ## Caveats
110
+ ## Differences in Redis Client Libraries
97
111
 
98
112
  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.
99
113
 
@@ -1,4 +1,5 @@
1
1
  require 'redis_pagination/paginator/list_paginator'
2
+ require 'redis_pagination/paginator/none_paginator'
2
3
  require 'redis_pagination/paginator/sorted_set_paginator'
3
4
 
4
5
  module RedisPagination
@@ -21,6 +22,8 @@ module RedisPagination
21
22
  RedisPagination::Paginator::ListPaginator.new(key, options)
22
23
  when 'zset'
23
24
  RedisPagination::Paginator::SortedSetPaginator.new(key, options)
25
+ when 'none'
26
+ RedisPagination::Paginator::NonePaginator.new(key, options)
24
27
  else
25
28
  raise "Pagination is not supported for #{type}"
26
29
  end
@@ -0,0 +1,46 @@
1
+ module RedisPagination
2
+ module Paginator
3
+ class NonePaginator
4
+ # Initialize a new instance with a given Redis +key+ and options.
5
+ #
6
+ # @param key [String] Redis list key.
7
+ # @param options [Hash] Options for paginator.
8
+ def initialize(key, options = {})
9
+ end
10
+
11
+ # Return the total number of pages for +key+.
12
+ #
13
+ # @param page_size [int] Page size to calculate total number of pages.
14
+ #
15
+ # @return the total number of pages for +key+.
16
+ def total_pages(page_size = RedisPagination.page_size)
17
+ 0
18
+ end
19
+
20
+ # Return the total number of items for +key+.
21
+ #
22
+ # @return the total number of items for +key+.
23
+ def total_items
24
+ 0
25
+ end
26
+
27
+ # Retrieve a page of items for +key+.
28
+ #
29
+ # @param page [int] Page of items to retrieve.
30
+ # @param options [Hash] Options. Valid options are :page_size.
31
+ # :page_size controls the page size for the call. Default is +RedisPagination.page_size+.
32
+ #
33
+ # @return a +Hash+ containing +:current_page+, +:total_pages+, +:total_items+ and +:items+.
34
+ def page(page, options = {})
35
+ current_page = page < 1 ? 1 : page
36
+
37
+ {
38
+ :current_page => current_page,
39
+ :total_pages => 0,
40
+ :total_items => 0,
41
+ :items => []
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisPagination
2
- VERSION = '0.0.1'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe RedisPagination::Paginator::NonePaginator do
4
+ describe '#total_pages' do
5
+ it 'should return the correct number of pages' do
6
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
7
+ items_paginator.total_pages.should == 0
8
+ end
9
+
10
+ it 'should return the correct number of pages using a different page size' do
11
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
12
+ items_paginator.total_pages(5).should == 0
13
+ end
14
+ end
15
+
16
+ describe '#total_items' do
17
+ it 'should return the correct number of items' do
18
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
19
+ items_paginator.total_items.should == 0
20
+ end
21
+ end
22
+
23
+ describe '#page' do
24
+ it 'should return the correct page of items' do
25
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
26
+ result = items_paginator.page(1)
27
+ result[:items].length.should == 0
28
+ result[:current_page].should == 1
29
+ result[:total_pages].should == 0
30
+ result[:total_items].should == 0
31
+
32
+ result = items_paginator.page(2)
33
+ result[:items].length.should == 0
34
+ result[:current_page].should == 2
35
+ end
36
+
37
+ it 'should return the correct page of items with the options set' do
38
+ items_paginator = RedisPagination.paginate('unknown-key-in-redis')
39
+ result = items_paginator.page(1, :page_size => 5)
40
+ result[:items].length.should == 0
41
+ result[:current_page].should == 1
42
+ result[:total_pages].should == 0
43
+ end
44
+ end
45
+ end
@@ -16,6 +16,10 @@ describe RedisPagination::Paginator do
16
16
  RedisPagination.paginate("items").should be_a_kind_of(RedisPagination::Paginator::SortedSetPaginator)
17
17
  end
18
18
 
19
+ it 'should return a RedisPagination::Paginator::NonePaginator' do
20
+ RedisPagination.paginate("unknown-key-in-redis").should be_a_kind_of(RedisPagination::Paginator::NonePaginator)
21
+ end
22
+
19
23
  it 'should raise an exception if trying to paginate a Redis type that cannot be paginated' do
20
24
  redis = RedisPagination.redis
21
25
  redis.set("items", "item_1")
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe 'RedisPagination::VERSION' do
4
4
  it 'should be the correct version' do
5
- RedisPagination::VERSION.should == '0.0.1'
5
+ RedisPagination::VERSION.should == '1.0.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-19 00:00:00.000000000 Z
12
+ date: 2012-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -69,6 +69,7 @@ files:
69
69
  - .gitignore
70
70
  - .rspec
71
71
  - .rvmrc
72
+ - CHANGELOG.md
72
73
  - Gemfile
73
74
  - LICENSE
74
75
  - README.md
@@ -77,11 +78,13 @@ files:
77
78
  - lib/redis_pagination/configuration.rb
78
79
  - lib/redis_pagination/paginator.rb
79
80
  - lib/redis_pagination/paginator/list_paginator.rb
81
+ - lib/redis_pagination/paginator/none_paginator.rb
80
82
  - lib/redis_pagination/paginator/sorted_set_paginator.rb
81
83
  - lib/redis_pagination/version.rb
82
84
  - redis_pagination.gemspec
83
85
  - spec/redis_pagination/configuration_spec.rb
84
86
  - spec/redis_pagination/paginator/list_paginator_spec.rb
87
+ - spec/redis_pagination/paginator/none_paginator_spec.rb
85
88
  - spec/redis_pagination/paginator/sorted_set_paginator_spec.rb
86
89
  - spec/redis_pagination/paginator_spec.rb
87
90
  - spec/redis_pagination/version_spec.rb
@@ -113,6 +116,7 @@ summary: Simple pagination for Redis lists and sorted sets.
113
116
  test_files:
114
117
  - spec/redis_pagination/configuration_spec.rb
115
118
  - spec/redis_pagination/paginator/list_paginator_spec.rb
119
+ - spec/redis_pagination/paginator/none_paginator_spec.rb
116
120
  - spec/redis_pagination/paginator/sorted_set_paginator_spec.rb
117
121
  - spec/redis_pagination/paginator_spec.rb
118
122
  - spec/redis_pagination/version_spec.rb