redis_pagination 0.0.1
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/.gitignore +18 -0
 - data/.rspec +2 -0
 - data/.rvmrc +1 -0
 - data/Gemfile +3 -0
 - data/LICENSE +22 -0
 - data/README.md +118 -0
 - data/Rakefile +12 -0
 - data/lib/redis_pagination.rb +10 -0
 - data/lib/redis_pagination/configuration.rb +29 -0
 - data/lib/redis_pagination/paginator.rb +29 -0
 - data/lib/redis_pagination/paginator/list_paginator.rb +51 -0
 - data/lib/redis_pagination/paginator/sorted_set_paginator.rb +60 -0
 - data/lib/redis_pagination/version.rb +3 -0
 - data/redis_pagination.gemspec +21 -0
 - data/spec/redis_pagination/configuration_spec.rb +11 -0
 - data/spec/redis_pagination/paginator/list_paginator_spec.rb +59 -0
 - data/spec/redis_pagination/paginator/sorted_set_paginator_spec.rb +64 -0
 - data/spec/redis_pagination/paginator_spec.rb +26 -0
 - data/spec/redis_pagination/version_spec.rb +7 -0
 - data/spec/spec_helper.rb +29 -0
 - metadata +119 -0
 
    
        data/.gitignore
    ADDED
    
    
    
        data/.rspec
    ADDED
    
    
    
        data/.rvmrc
    ADDED
    
    | 
         @@ -0,0 +1 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            rvm use ruby-1.9.3@redis_pagination_gem --create
         
     | 
    
        data/Gemfile
    ADDED
    
    
    
        data/LICENSE
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            Copyright (c) 2012 David Czarnecki
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            MIT License
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Permission is hereby granted, free of charge, to any person obtaining
         
     | 
| 
      
 6 
     | 
    
         
            +
            a copy of this software and associated documentation files (the
         
     | 
| 
      
 7 
     | 
    
         
            +
            "Software"), to deal in the Software without restriction, including
         
     | 
| 
      
 8 
     | 
    
         
            +
            without limitation the rights to use, copy, modify, merge, publish,
         
     | 
| 
      
 9 
     | 
    
         
            +
            distribute, sublicense, and/or sell copies of the Software, and to
         
     | 
| 
      
 10 
     | 
    
         
            +
            permit persons to whom the Software is furnished to do so, subject to
         
     | 
| 
      
 11 
     | 
    
         
            +
            the following conditions:
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            The above copyright notice and this permission notice shall be
         
     | 
| 
      
 14 
     | 
    
         
            +
            included in all copies or substantial portions of the Software.
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         
     | 
| 
      
 17 
     | 
    
         
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
         
     | 
| 
      
 18 
     | 
    
         
            +
            MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         
     | 
| 
      
 19 
     | 
    
         
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
         
     | 
| 
      
 20 
     | 
    
         
            +
            LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
         
     | 
| 
      
 21 
     | 
    
         
            +
            OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
         
     | 
| 
      
 22 
     | 
    
         
            +
            WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
         
     | 
    
        data/README.md
    ADDED
    
    | 
         @@ -0,0 +1,118 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # RedisPagination
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            Simple pagination for Redis lists and sorted sets.
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Make sure your redis server is running! Redis configuration is outside the scope of this README, but 
         
     | 
| 
      
 6 
     | 
    
         
            +
            check out the [Redis documentation](http://redis.io/documentation) for more information.
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            ## Installation
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            Add this line to your application's Gemfile:
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 13 
     | 
    
         
            +
            gem 'redis_pagination'
         
     | 
| 
      
 14 
     | 
    
         
            +
            ```
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
            And then execute:
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            ```
         
     | 
| 
      
 19 
     | 
    
         
            +
            $ bundle
         
     | 
| 
      
 20 
     | 
    
         
            +
            ```
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            Or install it yourself as:
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
            ```
         
     | 
| 
      
 25 
     | 
    
         
            +
            $ gem install redis_pagination
         
     | 
| 
      
 26 
     | 
    
         
            +
            ```
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
            ## Usage
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            Configure redis_pagination:
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 33 
     | 
    
         
            +
            RedisPaginations.configure do |configuration|
         
     | 
| 
      
 34 
     | 
    
         
            +
              configuration.redis = Redis.new
         
     | 
| 
      
 35 
     | 
    
         
            +
              configuration.page_size = 25
         
     | 
| 
      
 36 
     | 
    
         
            +
            end
         
     | 
| 
      
 37 
     | 
    
         
            +
            ```
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            Use redis_pagination:
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 42 
     | 
    
         
            +
            require 'redis_pagination'
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            RedisPagination.configure do |configuration|
         
     | 
| 
      
 45 
     | 
    
         
            +
              configuration.redis = Redis.new(:db => 15)
         
     | 
| 
      
 46 
     | 
    
         
            +
              configuration.page_size = 25
         
     | 
| 
      
 47 
     | 
    
         
            +
            end
         
     | 
| 
      
 48 
     | 
    
         
            +
             
     | 
| 
      
 49 
     | 
    
         
            +
            # List
         
     | 
| 
      
 50 
     | 
    
         
            +
            add_items_to_list('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 51 
     | 
    
         
            +
             => 1 
         
     | 
| 
      
 52 
     | 
    
         
            +
            items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 53 
     | 
    
         
            +
             => #<RedisPagination::Paginator::ListPaginator:0x007f8109b08ba0 @key="items"> 
         
     | 
| 
      
 54 
     | 
    
         
            +
            items_paginator.total_items
         
     | 
| 
      
 55 
     | 
    
         
            +
             => 27 
         
     | 
| 
      
 56 
     | 
    
         
            +
            items_paginator.total_pages
         
     | 
| 
      
 57 
     | 
    
         
            +
             => 2 
         
     | 
| 
      
 58 
     | 
    
         
            +
            items_paginator.total_pages(5)
         
     | 
| 
      
 59 
     | 
    
         
            +
             => 6 
         
     | 
| 
      
 60 
     | 
    
         
            +
            items = items_paginator.page(1)
         
     | 
| 
      
 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 
     | 
    
         
            +
            items = items_paginator.page(2)
         
     | 
| 
      
 63 
     | 
    
         
            +
             => {:current_page=>2, :total_pages=>2, :total_items=>27, :items=>["item_26", "item_27"]}
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
            # Sorted Set
         
     | 
| 
      
 66 
     | 
    
         
            +
            add_items_to_sorted_set('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 67 
     | 
    
         
            +
             => 1 
         
     | 
| 
      
 68 
     | 
    
         
            +
            items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 69 
     | 
    
         
            +
             => #<RedisPagination::Paginator::SortedSetPaginator:0x007f8109a25828 @key="items"> 
         
     | 
| 
      
 70 
     | 
    
         
            +
            items_paginator.total_items
         
     | 
| 
      
 71 
     | 
    
         
            +
             => 27 
         
     | 
| 
      
 72 
     | 
    
         
            +
            items_paginator.total_pages
         
     | 
| 
      
 73 
     | 
    
         
            +
             => 2 
         
     | 
| 
      
 74 
     | 
    
         
            +
            items_paginator.total_pages(5)
         
     | 
| 
      
 75 
     | 
    
         
            +
             => 6 
         
     | 
| 
      
 76 
     | 
    
         
            +
            items = items_paginator.page(1)
         
     | 
| 
      
 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 
     | 
    
         
            +
            items = items_paginator.page(2)
         
     | 
| 
      
 79 
     | 
    
         
            +
             => {:current_page=>2, :total_pages=>2, :total_items=>27, :items=>[["item_2", 2.0], ["item_1", 1.0]]}
         
     | 
| 
      
 80 
     | 
    
         
            +
            items = items_paginator.page(1, :with_scores => false, :reverse => false)
         
     | 
| 
      
 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 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
            ## Paging Options
         
     | 
| 
      
 85 
     | 
    
         
            +
             
     | 
| 
      
 86 
     | 
    
         
            +
            Valid options in the `page` call for paginating a Redis list are:
         
     | 
| 
      
 87 
     | 
    
         
            +
             
     | 
| 
      
 88 
     | 
    
         
            +
            * `:page_size` controls the page size for the call. Default is `RedisPagination.page_size`.
         
     | 
| 
      
 89 
     | 
    
         
            +
             
     | 
| 
      
 90 
     | 
    
         
            +
            Valid options in the `page` call for paginating a Redis sorted set are:
         
     | 
| 
      
 91 
     | 
    
         
            +
             
     | 
| 
      
 92 
     | 
    
         
            +
            * `:page_size` controls the page size for the call. Default is `RedisPagination.page_size`.
         
     | 
| 
      
 93 
     | 
    
         
            +
            * `:with_scores` controls whether the score is returned along with the item. Default is `true`.
         
     | 
| 
      
 94 
     | 
    
         
            +
            * `:reverse controls` whether to return items in highest-to-lowest (`true`) or loweest-to-highest order (`false`). Default is `true`.
         
     | 
| 
      
 95 
     | 
    
         
            +
             
     | 
| 
      
 96 
     | 
    
         
            +
            ## Caveats
         
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
            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 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
            If you are using the 2.x branch, you will get items returned with alternating item and score as follows:
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 103 
     | 
    
         
            +
            {:current_page=>1, :total_pages=>2, :total_items=>27, :items=>["item_27", "27", "item_26", "26", "item_25", "25", "item_24", "24", "item_23", "23", "item_22", "22", "item_21", "21", "item_20", "20", "item_19", "19", "item_18", "18", "item_17", "17", "item_16", "16", "item_15", "15", "item_14", "14", "item_13", "13", "item_12", "12", "item_11", "11", "item_10", "10", "item_9", "9", "item_8", "8", "item_7", "7", "item_6", "6", "item_5", "5", "item_4", "4", "item_3", "3"]}
         
     | 
| 
      
 104 
     | 
    
         
            +
            ```
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
            If you are using the 3.x branch, you will get items returned as an array of arrays, where the internal arrays are the item and score as follows:
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
            ```ruby
         
     | 
| 
      
 109 
     | 
    
         
            +
            {: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]]}
         
     | 
| 
      
 110 
     | 
    
         
            +
            ```
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
            ## Contributing
         
     | 
| 
      
 113 
     | 
    
         
            +
             
     | 
| 
      
 114 
     | 
    
         
            +
            1. Fork it
         
     | 
| 
      
 115 
     | 
    
         
            +
            2. Create your feature branch (`git checkout -b my-new-feature`)
         
     | 
| 
      
 116 
     | 
    
         
            +
            3. Commit your changes (`git commit -am 'Added some feature'`)
         
     | 
| 
      
 117 
     | 
    
         
            +
            4. Push to the branch (`git push origin my-new-feature`)
         
     | 
| 
      
 118 
     | 
    
         
            +
            5. Create new Pull Request
         
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1,12 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env rake
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'bundler/gem_tasks'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            require 'rspec/core'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'rspec/core/rake_task'
         
     | 
| 
      
 6 
     | 
    
         
            +
            RSpec::Core::RakeTask.new(:spec) do |spec|
         
     | 
| 
      
 7 
     | 
    
         
            +
              spec.pattern = FileList['spec/**/*_spec.rb']
         
     | 
| 
      
 8 
     | 
    
         
            +
              spec.rspec_opts = ['--backtrace']
         
     | 
| 
      
 9 
     | 
    
         
            +
              # spec.ruby_opts = ['-w']    
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            task :default => :spec
         
     | 
| 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RedisPagination
         
     | 
| 
      
 2 
     | 
    
         
            +
              # Configuration settings for redis_pagination.
         
     | 
| 
      
 3 
     | 
    
         
            +
              module Configuration
         
     | 
| 
      
 4 
     | 
    
         
            +
                # Redis instance.
         
     | 
| 
      
 5 
     | 
    
         
            +
                attr_accessor :redis
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                # Page size to be used when peforming paging operations.
         
     | 
| 
      
 8 
     | 
    
         
            +
                attr_writer :page_size
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                # Yield self to be able to configure redis_pagination with block-style configuration.
         
     | 
| 
      
 11 
     | 
    
         
            +
                #
         
     | 
| 
      
 12 
     | 
    
         
            +
                # Example:
         
     | 
| 
      
 13 
     | 
    
         
            +
                #
         
     | 
| 
      
 14 
     | 
    
         
            +
                #   RedisPagination.configure do |configuration|
         
     | 
| 
      
 15 
     | 
    
         
            +
                #     configuration.redis = Redis.new
         
     | 
| 
      
 16 
     | 
    
         
            +
                #     configuration.page_size = 25
         
     | 
| 
      
 17 
     | 
    
         
            +
                #   end
         
     | 
| 
      
 18 
     | 
    
         
            +
                def configure
         
     | 
| 
      
 19 
     | 
    
         
            +
                  yield self
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                # Page size to be used when peforming paging operations.
         
     | 
| 
      
 23 
     | 
    
         
            +
                #
         
     | 
| 
      
 24 
     | 
    
         
            +
                # @return the page size to be used when peforming paging operations or the default of 25 if not set.
         
     | 
| 
      
 25 
     | 
    
         
            +
                def page_size
         
     | 
| 
      
 26 
     | 
    
         
            +
                  @page_size ||= 25
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'redis_pagination/paginator/list_paginator'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'redis_pagination/paginator/sorted_set_paginator'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            module RedisPagination
         
     | 
| 
      
 5 
     | 
    
         
            +
              module Paginator
         
     | 
| 
      
 6 
     | 
    
         
            +
                # Retrieve a paginator class appropriate for the +key+ in Redis.
         
     | 
| 
      
 7 
     | 
    
         
            +
                # +key+ must be one of +list+ or +zset+, otherwise an exception 
         
     | 
| 
      
 8 
     | 
    
         
            +
                # will be raised.
         
     | 
| 
      
 9 
     | 
    
         
            +
                #
         
     | 
| 
      
 10 
     | 
    
         
            +
                # @params key [String] Redis key
         
     | 
| 
      
 11 
     | 
    
         
            +
                # @params options [Hash] Options to be passed to the individual paginator class.
         
     | 
| 
      
 12 
     | 
    
         
            +
                #
         
     | 
| 
      
 13 
     | 
    
         
            +
                # @return Returns either a +RedisPagination::Paginator::ListPaginator+ or 
         
     | 
| 
      
 14 
     | 
    
         
            +
                #   a +RedisPagination::Paginator::SortedSetPaginator+ depending on the 
         
     | 
| 
      
 15 
     | 
    
         
            +
                #   type of +key+.
         
     | 
| 
      
 16 
     | 
    
         
            +
                def paginate(key, options = {})
         
     | 
| 
      
 17 
     | 
    
         
            +
                  type = RedisPagination.redis.type(key)
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                  case type
         
     | 
| 
      
 20 
     | 
    
         
            +
                  when 'list'
         
     | 
| 
      
 21 
     | 
    
         
            +
                    RedisPagination::Paginator::ListPaginator.new(key, options)
         
     | 
| 
      
 22 
     | 
    
         
            +
                  when 'zset'
         
     | 
| 
      
 23 
     | 
    
         
            +
                    RedisPagination::Paginator::SortedSetPaginator.new(key, options)
         
     | 
| 
      
 24 
     | 
    
         
            +
                  else
         
     | 
| 
      
 25 
     | 
    
         
            +
                    raise "Pagination is not supported for #{type}"
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,51 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RedisPagination
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Paginator
         
     | 
| 
      
 3 
     | 
    
         
            +
                class ListPaginator
         
     | 
| 
      
 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 
     | 
    
         
            +
                    @key = key
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  # Return the total number of pages for +key+.
         
     | 
| 
      
 13 
     | 
    
         
            +
                  #
         
     | 
| 
      
 14 
     | 
    
         
            +
                  # @param page_size [int] Page size to calculate total number of pages.
         
     | 
| 
      
 15 
     | 
    
         
            +
                  # 
         
     | 
| 
      
 16 
     | 
    
         
            +
                  # @return the total number of pages for +key+.
         
     | 
| 
      
 17 
     | 
    
         
            +
                  def total_pages(page_size = RedisPagination.page_size)
         
     | 
| 
      
 18 
     | 
    
         
            +
                    (RedisPagination.redis.llen(@key) / page_size.to_f).ceil
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  # Return the total number of items for +key+.
         
     | 
| 
      
 22 
     | 
    
         
            +
                  #
         
     | 
| 
      
 23 
     | 
    
         
            +
                  # @return the total number of items for +key+.
         
     | 
| 
      
 24 
     | 
    
         
            +
                  def total_items
         
     | 
| 
      
 25 
     | 
    
         
            +
                    RedisPagination.redis.llen(@key)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  # Retrieve a page of items for +key+.
         
     | 
| 
      
 29 
     | 
    
         
            +
                  #
         
     | 
| 
      
 30 
     | 
    
         
            +
                  # @param page [int] Page of items to retrieve.
         
     | 
| 
      
 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+.      
         
     | 
| 
      
 33 
     | 
    
         
            +
                  #
         
     | 
| 
      
 34 
     | 
    
         
            +
                  # @return a +Hash+ containing +:current_page+, +:total_pages+, +:total_items+ and +:items+.
         
     | 
| 
      
 35 
     | 
    
         
            +
                  def page(page, options = {})
         
     | 
| 
      
 36 
     | 
    
         
            +
                    current_page = page < 1 ? 1 : page
         
     | 
| 
      
 37 
     | 
    
         
            +
                    index_for_redis = current_page - 1
         
     | 
| 
      
 38 
     | 
    
         
            +
                    page_size = options[:page_size] || RedisPagination.page_size
         
     | 
| 
      
 39 
     | 
    
         
            +
                    starting_offset = index_for_redis * page_size
         
     | 
| 
      
 40 
     | 
    
         
            +
                    ending_offset = (starting_offset + page_size) - 1
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                    {
         
     | 
| 
      
 43 
     | 
    
         
            +
                      :current_page => current_page,
         
     | 
| 
      
 44 
     | 
    
         
            +
                      :total_pages => total_pages(page_size),
         
     | 
| 
      
 45 
     | 
    
         
            +
                      :total_items => total_items,
         
     | 
| 
      
 46 
     | 
    
         
            +
                      :items => RedisPagination.redis.lrange(@key, starting_offset, ending_offset)
         
     | 
| 
      
 47 
     | 
    
         
            +
                    }        
         
     | 
| 
      
 48 
     | 
    
         
            +
                  end
         
     | 
| 
      
 49 
     | 
    
         
            +
                end
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,60 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module RedisPagination
         
     | 
| 
      
 2 
     | 
    
         
            +
              module Paginator
         
     | 
| 
      
 3 
     | 
    
         
            +
                class SortedSetPaginator
         
     | 
| 
      
 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 
     | 
    
         
            +
                    @key = key
         
     | 
| 
      
 10 
     | 
    
         
            +
                  end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                  # Return the total number of pages for +key+.
         
     | 
| 
      
 13 
     | 
    
         
            +
                  #
         
     | 
| 
      
 14 
     | 
    
         
            +
                  # @param page_size [int] Page size to calculate total number of pages.
         
     | 
| 
      
 15 
     | 
    
         
            +
                  # 
         
     | 
| 
      
 16 
     | 
    
         
            +
                  # @return the total number of pages for +key+.
         
     | 
| 
      
 17 
     | 
    
         
            +
                  def total_pages(page_size = RedisPagination.page_size)
         
     | 
| 
      
 18 
     | 
    
         
            +
                    (RedisPagination.redis.zcard(@key) / page_size.to_f).ceil
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                  # Return the total number of items for +key+.
         
     | 
| 
      
 22 
     | 
    
         
            +
                  #
         
     | 
| 
      
 23 
     | 
    
         
            +
                  # @return the total number of items for +key+.
         
     | 
| 
      
 24 
     | 
    
         
            +
                  def total_items
         
     | 
| 
      
 25 
     | 
    
         
            +
                    RedisPagination.redis.zcard(@key)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                  # Retrieve a page of items for +key+.
         
     | 
| 
      
 29 
     | 
    
         
            +
                  #
         
     | 
| 
      
 30 
     | 
    
         
            +
                  # @param page [int] Page of items to retrieve.
         
     | 
| 
      
 31 
     | 
    
         
            +
                  # @param options [Hash] Options. Valid options are :page_size, :with_scores and :reverse.
         
     | 
| 
      
 32 
     | 
    
         
            +
                  #   :page_size controls the page size for the call. Default is +RedisPagination.page_size+.
         
     | 
| 
      
 33 
     | 
    
         
            +
                  #   :with_scores controls whether the score is returned along with the item. Default is +true+.
         
     | 
| 
      
 34 
     | 
    
         
            +
                  #   :reverse controls whether to return items in highest-to-lowest (+true+) or loweest-to-highest order (+false+). Default is +true+.
         
     | 
| 
      
 35 
     | 
    
         
            +
                  #
         
     | 
| 
      
 36 
     | 
    
         
            +
                  # @return a +Hash+ containing +:current_page+, +:total_pages+, +:total_items+ and +:items+.
         
     | 
| 
      
 37 
     | 
    
         
            +
                  def page(page, options = {})
         
     | 
| 
      
 38 
     | 
    
         
            +
                    current_page = page < 1 ? 1 : page
         
     | 
| 
      
 39 
     | 
    
         
            +
                    index_for_redis = current_page - 1
         
     | 
| 
      
 40 
     | 
    
         
            +
                    page_size = options[:page_size] || RedisPagination.page_size
         
     | 
| 
      
 41 
     | 
    
         
            +
                    starting_offset = index_for_redis * page_size
         
     | 
| 
      
 42 
     | 
    
         
            +
                    ending_offset = (starting_offset + page_size) - 1
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                    with_scores = options.has_key?(:with_scores) ? options[:with_scores] : true
         
     | 
| 
      
 45 
     | 
    
         
            +
                    reverse = options.has_key?(:reverse) ? options[:reverse] : true
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                    {
         
     | 
| 
      
 48 
     | 
    
         
            +
                      :current_page => current_page,
         
     | 
| 
      
 49 
     | 
    
         
            +
                      :total_pages => total_pages(page_size),
         
     | 
| 
      
 50 
     | 
    
         
            +
                      :total_items => total_items,
         
     | 
| 
      
 51 
     | 
    
         
            +
                      :items => if reverse
         
     | 
| 
      
 52 
     | 
    
         
            +
                        RedisPagination.redis.zrevrange(@key, starting_offset, ending_offset, :with_scores => with_scores)
         
     | 
| 
      
 53 
     | 
    
         
            +
                      else
         
     | 
| 
      
 54 
     | 
    
         
            +
                        RedisPagination.redis.zrange(@key, starting_offset, ending_offset, :with_scores => with_scores)
         
     | 
| 
      
 55 
     | 
    
         
            +
                      end
         
     | 
| 
      
 56 
     | 
    
         
            +
                    } 
         
     | 
| 
      
 57 
     | 
    
         
            +
                  end
         
     | 
| 
      
 58 
     | 
    
         
            +
                end
         
     | 
| 
      
 59 
     | 
    
         
            +
              end
         
     | 
| 
      
 60 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,21 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            require File.expand_path('../lib/redis_pagination/version', __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            Gem::Specification.new do |gem|
         
     | 
| 
      
 5 
     | 
    
         
            +
              gem.authors       = ['David Czarnecki']
         
     | 
| 
      
 6 
     | 
    
         
            +
              gem.email         = ['me@davidczarnecki.com']
         
     | 
| 
      
 7 
     | 
    
         
            +
              gem.description   = %q{Simple pagination for Redis lists and sorted sets.}
         
     | 
| 
      
 8 
     | 
    
         
            +
              gem.summary       = %q{Simple pagination for Redis lists and sorted sets.}
         
     | 
| 
      
 9 
     | 
    
         
            +
              gem.homepage      = 'https://github.com/czarneckid/redis_pagination'
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
              gem.files         = `git ls-files`.split($\)
         
     | 
| 
      
 12 
     | 
    
         
            +
              gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         
     | 
| 
      
 13 
     | 
    
         
            +
              gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
      
 14 
     | 
    
         
            +
              gem.name          = 'redis_pagination'
         
     | 
| 
      
 15 
     | 
    
         
            +
              gem.require_paths = ['lib']
         
     | 
| 
      
 16 
     | 
    
         
            +
              gem.version       = RedisPagination::VERSION
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              gem.add_dependency('redis')
         
     | 
| 
      
 19 
     | 
    
         
            +
              gem.add_development_dependency('rake')
         
     | 
| 
      
 20 
     | 
    
         
            +
              gem.add_development_dependency('rspec')
         
     | 
| 
      
 21 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,59 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RedisPagination::Paginator::ListPaginator do
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe '#total_pages' do
         
     | 
| 
      
 5 
     | 
    
         
            +
                it 'should return the correct number of pages' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  add_items_to_list('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 9 
     | 
    
         
            +
                  items_paginator.total_pages.should == 2
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                it 'should return the correct number of pages using a different page size' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                  add_items_to_list('items', 25)
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 16 
     | 
    
         
            +
                  items_paginator.total_pages(5).should == 5
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              describe '#total_items' do
         
     | 
| 
      
 21 
     | 
    
         
            +
                it 'should return the correct number of items' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                  add_items_to_list('items', RedisPagination.page_size)
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 25 
     | 
    
         
            +
                  items_paginator.total_items.should == RedisPagination.page_size
         
     | 
| 
      
 26 
     | 
    
         
            +
                end
         
     | 
| 
      
 27 
     | 
    
         
            +
              end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
              describe '#page' do
         
     | 
| 
      
 30 
     | 
    
         
            +
                it 'should return the correct page of items' do
         
     | 
| 
      
 31 
     | 
    
         
            +
                  add_items_to_list('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 34 
     | 
    
         
            +
                  result = items_paginator.page(1)
         
     | 
| 
      
 35 
     | 
    
         
            +
                  result[:items].length.should == RedisPagination.page_size
         
     | 
| 
      
 36 
     | 
    
         
            +
                  result[:items][0].should == 'item_1'
         
     | 
| 
      
 37 
     | 
    
         
            +
                  result[:items][-1].should == 'item_25'
         
     | 
| 
      
 38 
     | 
    
         
            +
                  result[:current_page].should == 1
         
     | 
| 
      
 39 
     | 
    
         
            +
                  result[:total_pages].should == 2
         
     | 
| 
      
 40 
     | 
    
         
            +
                  result[:total_items].should == RedisPagination.page_size + 2
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
                  result = items_paginator.page(2)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  result[:items].length.should == 2
         
     | 
| 
      
 44 
     | 
    
         
            +
                  result[:items][-1].should == 'item_27'
         
     | 
| 
      
 45 
     | 
    
         
            +
                  result[:current_page].should == 2
         
     | 
| 
      
 46 
     | 
    
         
            +
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                it 'should return the correct page of items with the options set' do
         
     | 
| 
      
 49 
     | 
    
         
            +
                  add_items_to_list('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 52 
     | 
    
         
            +
                  result = items_paginator.page(1, :page_size => 5)
         
     | 
| 
      
 53 
     | 
    
         
            +
                  result[:items].length.should == 5
         
     | 
| 
      
 54 
     | 
    
         
            +
                  result[:items][-1].should == 'item_5'
         
     | 
| 
      
 55 
     | 
    
         
            +
                  result[:current_page].should == 1
         
     | 
| 
      
 56 
     | 
    
         
            +
                  result[:total_pages].should == 6
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
              end
         
     | 
| 
      
 59 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,64 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RedisPagination::Paginator::SortedSetPaginator do
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe '#total_pages' do
         
     | 
| 
      
 5 
     | 
    
         
            +
                it 'should return the correct number of pages' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  add_items_to_sorted_set('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 9 
     | 
    
         
            +
                  items_paginator.total_pages.should == 2
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                it 'should return the correct number of pages using a different page size' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                  add_items_to_sorted_set('items', 25)
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 16 
     | 
    
         
            +
                  items_paginator.total_pages(5).should == 5
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
              end
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
              describe '#total_items' do
         
     | 
| 
      
 21 
     | 
    
         
            +
                it 'should return the correct number of items' do
         
     | 
| 
      
 22 
     | 
    
         
            +
                  add_items_to_sorted_set('items', RedisPagination.page_size)
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 25 
     | 
    
         
            +
                  items_paginator.total_items.should == RedisPagination.page_size
         
     | 
| 
      
 26 
     | 
    
         
            +
                end
         
     | 
| 
      
 27 
     | 
    
         
            +
              end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
              describe '#page' do
         
     | 
| 
      
 30 
     | 
    
         
            +
                it 'should return the correct page of items with the default options' do
         
     | 
| 
      
 31 
     | 
    
         
            +
                  add_items_to_sorted_set('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 34 
     | 
    
         
            +
                  result = items_paginator.page(1)
         
     | 
| 
      
 35 
     | 
    
         
            +
                  result[:items].length.should == RedisPagination.page_size
         
     | 
| 
      
 36 
     | 
    
         
            +
                  result[:items][0].should == ['item_27', 27.0]
         
     | 
| 
      
 37 
     | 
    
         
            +
                  result[:items][-1].should == ['item_3', 3.0]
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  result = items_paginator.page(2)
         
     | 
| 
      
 40 
     | 
    
         
            +
                  result[:items].length.should == 2
         
     | 
| 
      
 41 
     | 
    
         
            +
                  result[:items][-1].should == ['item_1', 1.0]
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                it 'should return the correct page of items with the options set' do
         
     | 
| 
      
 45 
     | 
    
         
            +
                  add_items_to_sorted_set('items', RedisPagination.page_size + 2)
         
     | 
| 
      
 46 
     | 
    
         
            +
             
     | 
| 
      
 47 
     | 
    
         
            +
                  items_paginator = RedisPagination.paginate('items')
         
     | 
| 
      
 48 
     | 
    
         
            +
                  result = items_paginator.page(1, :reverse => false, :with_scores => false)
         
     | 
| 
      
 49 
     | 
    
         
            +
                  result[:items].length.should == RedisPagination.page_size
         
     | 
| 
      
 50 
     | 
    
         
            +
                  result[:items][0].should == 'item_1'
         
     | 
| 
      
 51 
     | 
    
         
            +
                  result[:items][-1].should == 'item_25'
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                  result = items_paginator.page(2, :reverse => false, :with_scores => false)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  result[:items].length.should == 2
         
     | 
| 
      
 55 
     | 
    
         
            +
                  result[:items][-1].should == 'item_27'
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                  result = items_paginator.page(1, :page_size => 5, :with_scores => false)
         
     | 
| 
      
 58 
     | 
    
         
            +
                  result[:items].length.should == 5
         
     | 
| 
      
 59 
     | 
    
         
            +
                  result[:items][-1].should == 'item_23'
         
     | 
| 
      
 60 
     | 
    
         
            +
                  result[:current_page].should == 1
         
     | 
| 
      
 61 
     | 
    
         
            +
                  result[:total_pages].should == 6
         
     | 
| 
      
 62 
     | 
    
         
            +
                end
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            describe RedisPagination::Paginator do
         
     | 
| 
      
 4 
     | 
    
         
            +
              describe '#paginate' do
         
     | 
| 
      
 5 
     | 
    
         
            +
                it 'should return a RedisPagination::Paginator::ListPaginator' do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  redis = RedisPagination.redis
         
     | 
| 
      
 7 
     | 
    
         
            +
                  redis.lpush("items", "item_1")
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
                  RedisPagination.paginate("items").should be_a_kind_of(RedisPagination::Paginator::ListPaginator)
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                it 'should return a RedisPagination::Paginator::SortedSetPaginator' do
         
     | 
| 
      
 13 
     | 
    
         
            +
                  redis = RedisPagination.redis
         
     | 
| 
      
 14 
     | 
    
         
            +
                  redis.zadd("items", 1, "item_1")
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
                  RedisPagination.paginate("items").should be_a_kind_of(RedisPagination::Paginator::SortedSetPaginator)
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                it 'should raise an exception if trying to paginate a Redis type that cannot be paginated' do
         
     | 
| 
      
 20 
     | 
    
         
            +
                  redis = RedisPagination.redis
         
     | 
| 
      
 21 
     | 
    
         
            +
                  redis.set("items", "item_1")
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  lambda { RedisPagination.paginate("items") }.should raise_error
         
     | 
| 
      
 24 
     | 
    
         
            +
                end
         
     | 
| 
      
 25 
     | 
    
         
            +
              end
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
    
        data/spec/spec_helper.rb
    ADDED
    
    | 
         @@ -0,0 +1,29 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rspec'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'redis_pagination'
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
            Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            RSpec.configure do |config|
         
     | 
| 
      
 7 
     | 
    
         
            +
              config.before(:all) do
         
     | 
| 
      
 8 
     | 
    
         
            +
                RedisPagination.configure do |configuration|
         
     | 
| 
      
 9 
     | 
    
         
            +
                  configuration.redis = Redis.new(:db => 15)
         
     | 
| 
      
 10 
     | 
    
         
            +
                  configuration.page_size = 25
         
     | 
| 
      
 11 
     | 
    
         
            +
                end
         
     | 
| 
      
 12 
     | 
    
         
            +
              end
         
     | 
| 
      
 13 
     | 
    
         
            +
             
     | 
| 
      
 14 
     | 
    
         
            +
              config.before(:each) do
         
     | 
| 
      
 15 
     | 
    
         
            +
                RedisPagination.redis.flushdb
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              def add_items_to_list(key, items_to_add = RedisPagination.page_size)
         
     | 
| 
      
 19 
     | 
    
         
            +
                1.upto(items_to_add) do |index|
         
     | 
| 
      
 20 
     | 
    
         
            +
                  RedisPagination.redis.rpush(key, "item_#{index}")
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
             
     | 
| 
      
 24 
     | 
    
         
            +
              def add_items_to_sorted_set(key, items_to_add = RedisPagination.page_size)
         
     | 
| 
      
 25 
     | 
    
         
            +
                1.upto(items_to_add) do |index|
         
     | 
| 
      
 26 
     | 
    
         
            +
                  RedisPagination.redis.zadd(key, index, "item_#{index}")
         
     | 
| 
      
 27 
     | 
    
         
            +
                end
         
     | 
| 
      
 28 
     | 
    
         
            +
              end
         
     | 
| 
      
 29 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,119 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: redis_pagination
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - David Czarnecki
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-07-19 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 14 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 15 
     | 
    
         
            +
              name: redis
         
     | 
| 
      
 16 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 17 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 18 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 19 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 20 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 21 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 22 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 25 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 26 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 27 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 28 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 29 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 30 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 31 
     | 
    
         
            +
              name: rake
         
     | 
| 
      
 32 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 33 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 34 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 35 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 36 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 37 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 38 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 39 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 40 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 41 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 42 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 43 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 44 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 45 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 46 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 47 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 48 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 49 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 50 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 51 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 52 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 53 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 54 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 55 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 56 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 57 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 58 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 59 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 60 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 61 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 62 
     | 
    
         
            +
            description: Simple pagination for Redis lists and sorted sets.
         
     | 
| 
      
 63 
     | 
    
         
            +
            email:
         
     | 
| 
      
 64 
     | 
    
         
            +
            - me@davidczarnecki.com
         
     | 
| 
      
 65 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 66 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 67 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 68 
     | 
    
         
            +
            files:
         
     | 
| 
      
 69 
     | 
    
         
            +
            - .gitignore
         
     | 
| 
      
 70 
     | 
    
         
            +
            - .rspec
         
     | 
| 
      
 71 
     | 
    
         
            +
            - .rvmrc
         
     | 
| 
      
 72 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 73 
     | 
    
         
            +
            - LICENSE
         
     | 
| 
      
 74 
     | 
    
         
            +
            - README.md
         
     | 
| 
      
 75 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 76 
     | 
    
         
            +
            - lib/redis_pagination.rb
         
     | 
| 
      
 77 
     | 
    
         
            +
            - lib/redis_pagination/configuration.rb
         
     | 
| 
      
 78 
     | 
    
         
            +
            - lib/redis_pagination/paginator.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
            - lib/redis_pagination/paginator/list_paginator.rb
         
     | 
| 
      
 80 
     | 
    
         
            +
            - lib/redis_pagination/paginator/sorted_set_paginator.rb
         
     | 
| 
      
 81 
     | 
    
         
            +
            - lib/redis_pagination/version.rb
         
     | 
| 
      
 82 
     | 
    
         
            +
            - redis_pagination.gemspec
         
     | 
| 
      
 83 
     | 
    
         
            +
            - spec/redis_pagination/configuration_spec.rb
         
     | 
| 
      
 84 
     | 
    
         
            +
            - spec/redis_pagination/paginator/list_paginator_spec.rb
         
     | 
| 
      
 85 
     | 
    
         
            +
            - spec/redis_pagination/paginator/sorted_set_paginator_spec.rb
         
     | 
| 
      
 86 
     | 
    
         
            +
            - spec/redis_pagination/paginator_spec.rb
         
     | 
| 
      
 87 
     | 
    
         
            +
            - spec/redis_pagination/version_spec.rb
         
     | 
| 
      
 88 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     | 
| 
      
 89 
     | 
    
         
            +
            homepage: https://github.com/czarneckid/redis_pagination
         
     | 
| 
      
 90 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 91 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 92 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 93 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 94 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 95 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 96 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 97 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 98 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 99 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 100 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 101 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 102 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 103 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 104 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 105 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 106 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 107 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 108 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 109 
     | 
    
         
            +
            rubygems_version: 1.8.24
         
     | 
| 
      
 110 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 111 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 112 
     | 
    
         
            +
            summary: Simple pagination for Redis lists and sorted sets.
         
     | 
| 
      
 113 
     | 
    
         
            +
            test_files:
         
     | 
| 
      
 114 
     | 
    
         
            +
            - spec/redis_pagination/configuration_spec.rb
         
     | 
| 
      
 115 
     | 
    
         
            +
            - spec/redis_pagination/paginator/list_paginator_spec.rb
         
     | 
| 
      
 116 
     | 
    
         
            +
            - spec/redis_pagination/paginator/sorted_set_paginator_spec.rb
         
     | 
| 
      
 117 
     | 
    
         
            +
            - spec/redis_pagination/paginator_spec.rb
         
     | 
| 
      
 118 
     | 
    
         
            +
            - spec/redis_pagination/version_spec.rb
         
     | 
| 
      
 119 
     | 
    
         
            +
            - spec/spec_helper.rb
         
     |