page_up 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4cad306d616a594283a2dfe39bde5bd33dd6164
4
- data.tar.gz: 33c6ac47219e9a53c08fc81e8a8f9fe0c14f641d
3
+ metadata.gz: bc4dafe82f5d801b24a6471c6c852e24dce7b7b8
4
+ data.tar.gz: ef4e91f8807d28faf5370381b69eb0b9df4ffe98
5
5
  SHA512:
6
- metadata.gz: 26ea5deee66abb0a8061674fbae44f9b571efa699069653b6e7c47922e65987f3019b34f4df10bf0e76f19e938167792a57c664f7b7c2bcad28741609ce6c1a4
7
- data.tar.gz: 9edb21ae0a03cacdb9cd9773a6e32ab39ec5c512f2213280f450571f286eefff42d6f9786472803fe34aa485965adc27127541836085be1441775426e9e1ad3d
6
+ metadata.gz: b82269e408155eae4e5ece5e69215f450d716998bdb7c9cbc1936fa3ec0e74d852f1407387bc75c207af577b830c292289480c0404fbc7459298dae47cdb7838
7
+ data.tar.gz: ff17bb46b8a1359fa2daa6f45ebc0e1fb3cd1faf4caa9fcaff49973ea6289968d518309f5adf35c60e94b544694acb8f48fdc04454dbffc5541df08583fe556a
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,41 @@
1
+ module PageUp
2
+ class Fragment
3
+ NullResult = 'NullResult'
4
+
5
+ def initialize fragment, page, per_page
6
+ @page, @per_page = page, per_page
7
+ @fragment = [].fill(NullResult, 0, offset) + fragment
8
+ end
9
+
10
+ def [] range
11
+ until covers? range
12
+ next_page = page_for(range)
13
+ result = @callback.call next_page, @per_page
14
+ result.each_with_index do |value, index|
15
+ @fragment[index + offset(next_page)] = value
16
+ end
17
+ end
18
+ @fragment[range]
19
+ end
20
+
21
+ def use &block
22
+ @callback = block
23
+ end
24
+
25
+ private
26
+
27
+ def page_for range
28
+ first_null = range.find { |index| @fragment.fetch(index,NullResult) == NullResult } || range.end + 1
29
+ (first_null / @per_page) + 1
30
+ end
31
+
32
+ def offset page = @page
33
+ (page - 1) * @per_page
34
+ end
35
+
36
+ def covers? range
37
+ !range.any? { |index| @fragment.fetch(index,NullResult) == NullResult }
38
+ end
39
+
40
+ end
41
+ end
data/lib/page_up/pages.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'delegate'
1
2
  module PageUp
2
3
  class Pages < SimpleDelegator
3
4
 
@@ -1,3 +1,3 @@
1
1
  module PageUp
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/page_up.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'page_up/pages'
2
+ require 'page_up/fragment'
2
3
 
3
4
  module PageUp
4
5
 
@@ -0,0 +1,44 @@
1
+ require 'page_up/fragment'
2
+
3
+ describe 'fragmented pages' do
4
+ let(:source) { ( 1..100).to_a }
5
+ let(:fragment) { (21..40 ).to_a }
6
+ let(:pages) { PageUp::Fragment.new fragment, 2, 20 }
7
+
8
+ context 'when the range requested is within the fragment' do
9
+ it 'returns the local results' do
10
+ expect(pages[20...40]).to eq fragment
11
+ end
12
+ end
13
+
14
+ context 'when the range is outside the fragment' do
15
+ let(:pages_requested) { [] }
16
+
17
+ before do
18
+ pages.use do |page, per_page|
19
+ pages_requested << [page, per_page]
20
+ source.slice (page -1)* per_page, per_page
21
+ end
22
+ end
23
+
24
+ it 'will call the block supplied to use with page and the amount per page' do
25
+ pages[40...60]
26
+ expect(pages_requested).to eq [[3,20]]
27
+ end
28
+
29
+ it 'will call the block with only the desired pages' do
30
+ pages[60...80]
31
+ expect(pages_requested).to eq [[4,20]]
32
+ end
33
+
34
+ it 'will call the block multiple times if required' do
35
+ pages[70...90]
36
+ expect(pages_requested).to eq [[4,20],[5,20]]
37
+ end
38
+
39
+ it 'will return the desired fragment' do
40
+ expect(pages[70...90]).to eq (71..90).to_a
41
+ end
42
+ end
43
+
44
+ end
@@ -1,4 +1,3 @@
1
- require 'delegate'
2
1
  require 'page_up'
3
2
 
4
3
  describe "pagination" do
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: page_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Rowe
@@ -29,7 +29,7 @@ cert_chain:
29
29
  2feWfO4gCNmvfFjULOAYHq9JHEjN5SLSXvj5HdSnDcCyIfJKn5Ya3JahWQaWIsXf
30
30
  /NPE/mB57TOwj+d7XUa2NC4HUadF8R51IYEcIB0PpIEzJlKtfXFcOZxO
31
31
  -----END CERTIFICATE-----
32
- date: 2013-10-31 00:00:00.000000000 Z
32
+ date: 2013-11-05 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: rake
@@ -87,9 +87,11 @@ files:
87
87
  - README.md
88
88
  - Rakefile
89
89
  - lib/page_up.rb
90
+ - lib/page_up/fragment.rb
90
91
  - lib/page_up/pages.rb
91
92
  - lib/page_up/version.rb
92
93
  - page_up.gemspec
94
+ - spec/page_up/fragment_spec.rb
93
95
  - spec/page_up/pages_spec.rb
94
96
  homepage: https://github.com/JonRowe/page_up
95
97
  licenses:
@@ -116,4 +118,5 @@ signing_key:
116
118
  specification_version: 4
117
119
  summary: Simple paginator
118
120
  test_files:
121
+ - spec/page_up/fragment_spec.rb
119
122
  - spec/page_up/pages_spec.rb
metadata.gz.sig CHANGED
Binary file