paginate_cached 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/init.rb +1 -0
  3. data/lib/paginate_cached.rb +38 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 52bcf6e765191486dc05c806ac23df6bbf0a5d28
4
+ data.tar.gz: 7e2a0aa95d1ec2d36c31d83912de74bfe4a71b1b
5
+ SHA512:
6
+ metadata.gz: 9eb44dcb40b49136ba50ac91a4dfc4656a0d25c9a307d68a7ad433886faa728856c460105b6f7a5dbcb0d9744c8c89453030abdb7edf09e9444b740b766f31ea
7
+ data.tar.gz: 88466bacb8262ca7f7149426f9c1adf58b7954afc2a8e8ed6d49b3c6bd18d2aaa382a7bdbd9fa3c177815ca6624cb05e80fa9f493f6bc72e381bb3a3b319e3d9
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'paginate_cached'
@@ -0,0 +1,38 @@
1
+ module PaginateCached
2
+
3
+ # Arguments:
4
+ # This function takes :page as the page number, and it is required.
5
+ # The number of results is passed in as :number_of_results. It defaults to 20.
6
+ # The variable used to hold the current state in the pagination should be placed
7
+ # in the :search_id variable.
8
+ #
9
+ # The actual code which produces the result is passed in as a block.
10
+ #
11
+ # Results:
12
+ # This returns a hash in which :results will hold the results, :search_id will hold the search id,
13
+ # next_page: will hold the new page number, and has_more_results will hold whether there are more results.
14
+ #
15
+ def paginate_cached args={}
16
+ page = args[:page] or raise "Page number must be defined."
17
+ number_of_results = args[:number_of_results] || 25
18
+ search_id = args[:search_id]
19
+ offset = (page-1)*number_of_results
20
+
21
+ unless search_id and results = Rails.cache.read(search_id) then
22
+ results = yield
23
+ search_id = "#{results.hash}#{Time.now.to_i}"
24
+ Rails.cache.write search_id, results, expires_id: 15.minutes
25
+ end
26
+
27
+ return {
28
+ results: results[offset, number_of_results],
29
+ search_id: search_id,
30
+ next_page: page+1,
31
+ has_more_results: results[((page)*number_of_results)+1] != nil
32
+ }
33
+
34
+ end
35
+
36
+ ActionController::Base.send :include, PaginateCached
37
+
38
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paginate_cached
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Kyte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: alexanderkyte@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/paginate_cached.rb
20
+ - init.rb
21
+ homepage: http://github.com/alexanderkyte/paginate_cached
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Provides a helper method which will memoize result generation using the cache,
44
+ and return the page requested.
45
+ test_files: []