folio-pagination 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -152,13 +152,14 @@ methods as `Folio::Page` but with the following overrides:
152
152
  Similarly, mixing `Folio::Ordinal` into a source provides the same
153
153
  methods as `Folio`, but simplifies your `build_page` and `fill_page`
154
154
  methods by moving some responsibility into the `paginate` method.
155
+ `build_page` also has a default implementation.
155
156
 
156
157
  * `build_page` no longer needs to configure `ordinal_page?`, `first_page`,
157
158
  or `last_page` on the instantiated page. Instead, just instantiate
158
- and return a `Folio::Page` or `Folio::Ordinal::Page`. If what you
159
- return is not a `Folio::Ordinal::Page`, `paginate` will decorate it
160
- to be one. Then `ordinal_page?`, `first_page`, and `last_page` are
161
- handled for you, as described above.
159
+ and return a `Folio::Page` or `Folio::Ordinal::Page`. Then
160
+ `ordinal_page?`, `first_page`, and `last_page` are handled for you,
161
+ as described above. If not provided, the default implementation just
162
+ returns a subclass of `Array` setup to be a `Folio::Ordinal::Page`.
162
163
 
163
164
  * `fill_page` no longer needs to configure `next_page` and
164
165
  `previous_page`; the ordinal page will handle them. (Note that if
@@ -166,31 +167,28 @@ methods by moving some responsibility into the `paginate` method.
166
167
  `paginate` will now perform ordinal bounds checking for you, so you
167
168
  can focus entirely on populating the page.
168
169
 
169
- ### Decorations and `create`
170
+ ### `BasicPage`s and `create`
170
171
 
171
- Often times you just want to take a simple collection, such as an
172
- array, and have it act like a page. One way would be to subclass
173
- `Array` and mixin `Folio::Page`, then instantiate the subclass.
172
+ Often times you just want to take the simplest collection possible. One
173
+ way would be to subclass `Array` and mixin `Folio::Page`, then
174
+ instantiate the subclass. When you want to add more, or `Array` isn't the
175
+ proper superclass, you can still do this.
174
176
 
175
- Alternately, you can just call `Folio::Page.decorate(collection)`. This
176
- will decorate the collection instance in a delegate that mixes in
177
- `Folio::Page` for you. So, for example, a simple `build_page` method
178
- could just be:
177
+ For the common case we've already done it. This is the
178
+ `Folio::BasicPage` class. We've also provided a shortcut for
179
+ instantiating one: `Folio::Page.create`. So, for example, a simple
180
+ `build_page` method could just be:
179
181
 
180
182
  ```
181
183
  def build_page
182
- Folio::Page.decorate([])
184
+ page = Folio::Page.create
185
+ # setup ordinal_pages?, first_page, etc.
186
+ page
183
187
  end
184
188
  ```
185
189
 
186
- For the common case where a new empty array is what you intend to
187
- decorate, you can simply call `Folio::Page.create`.
188
-
189
- `Folio::Page::Ordinal.decorate(collection)` and
190
- `Folio::Page::Ordinal.create` are also available, respectively, for the
191
- ordinal case. `decorate` will assume the passed in collection lacks any
192
- pagination and will include `Folio::Page` along with
193
- `Folio::Page::Ordinal`.
190
+ `Folio::Ordinal::BasicPage` and `Folio::Ordinal::Page.create` are also
191
+ available, respectively, for the ordinal case.
194
192
 
195
193
  ### Enumerable Extension
196
194
 
@@ -201,7 +199,8 @@ methods.
201
199
  `build_page` will simply return a basic ordinal page as from
202
200
  `Folio::Page::Ordinal.create`. `fill_page` then selects an appropriate
203
201
  range of items from the folio using standard `Enumerable` methods, then
204
- calls `replace` on the page (it's a decorated array) with this subset.
202
+ calls `replace` on the page (it's a `Folio::Ordinal::BasicPage`) with
203
+ this subset.
205
204
 
206
205
  This lets you do things like:
207
206
 
@@ -76,31 +76,20 @@ module Folio
76
76
  (current_page - 1) * per_page
77
77
  end
78
78
 
79
- class Decorator < Folio::Page::Decorator
80
- include Folio::Ordinal::Page
79
+ def self.create
80
+ Folio::Ordinal::BasicPage.new
81
81
  end
82
+ end
82
83
 
83
- class DecoratedArray < Decorator
84
- def initialize
85
- super []
86
- end
84
+ class BasicPage < Folio::BasicPage
85
+ include Folio::Ordinal::Page
87
86
 
88
- def replace(array)
89
- result = super
90
- if total_entries.nil? and length < per_page and (current_page == 1 or length > 0)
91
- self.total_entries = offset + length
92
- end
93
- result
87
+ def replace(array)
88
+ result = super
89
+ if total_entries.nil? and length < per_page and (current_page == 1 or length > 0)
90
+ self.total_entries = offset + length
94
91
  end
95
- end
96
-
97
- def self.decorate(collection)
98
- collection = Folio::Ordinal::Page::Decorator.new(collection) unless collection.is_a?(Folio::Ordinal::Page)
99
- collection
100
- end
101
-
102
- def self.create
103
- Folio::Ordinal::Page::DecoratedArray.new
92
+ result
104
93
  end
105
94
  end
106
95
  end
data/lib/folio/ordinal.rb CHANGED
@@ -4,12 +4,12 @@ require 'folio/invalid_page'
4
4
 
5
5
  # Mix in to a source to provide the same methods as Folio, but with simpler
6
6
  # build_page and fill_page methods required on the host (some responsibility is
7
- # moved into the paginate method).
7
+ # moved into the paginate method). build_page also has a default implementation.
8
8
  #
9
9
  # * build_page no longer needs to configure ordinal_page?, first_page,
10
10
  # or last_page on the instantiated page. Instead, just instantiate
11
- # and return a Folio::Ordinal::Page. If what you return is not a
12
- # Folio::Ordinal::Page, paginate will decorate it to be one.
11
+ # and return a Folio::Ordinal::Page. If not provided, the default
12
+ # implementation just returns a Folio::Ordinal::BasicPage.
13
13
  #
14
14
  # * fill_page no longer needs to configure next_page and previous_page; the
15
15
  # ordinal page will handle them. (Note that if necessary, you can still set
@@ -18,10 +18,13 @@ require 'folio/invalid_page'
18
18
  #
19
19
  module Folio
20
20
  module Ordinal
21
- # decorate the page before configuring, and then validate the configured
22
- # current_page before returning it
21
+ def build_page
22
+ Folio::Ordinal::Page.create
23
+ end
24
+
25
+ # validate the configured page before returning it
23
26
  def configure_pagination(page, options)
24
- page = super(::Folio::Ordinal::Page.decorate(page), options)
27
+ page = super(page, options)
25
28
  raise ::Folio::InvalidPage unless page.current_page.is_a?(Integer)
26
29
  raise ::Folio::InvalidPage if page.out_of_bounds?
27
30
  page
data/lib/folio/page.rb CHANGED
@@ -64,23 +64,12 @@ module Folio
64
64
  (total_entries / per_page.to_f).ceil
65
65
  end
66
66
 
67
- class Decorator < ::SimpleDelegator
68
- include Folio::Page
69
- end
70
-
71
- class DecoratedArray < Decorator
72
- def initialize
73
- super []
74
- end
75
- end
76
-
77
- def self.decorate(collection)
78
- collection = Folio::Page::Decorator.new(collection) unless collection.is_a?(Folio::Page)
79
- collection
80
- end
81
-
82
67
  def self.create
83
- Folio::Page::DecoratedArray.new
68
+ Folio::BasicPage.new
84
69
  end
85
70
  end
71
+
72
+ class BasicPage < Array
73
+ include Folio::Page
74
+ end
86
75
  end
data/lib/folio/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Folio
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -60,8 +60,6 @@ module Folio
60
60
  @target = target
61
61
  end
62
62
 
63
- # needed to prevent Folio::Ordinal#configure_pagination from trying
64
- # to decorate it
65
63
  include Folio::Ordinal::Page
66
64
 
67
65
  # called during Folio#configure_pagination, used to build up @rel
@@ -139,21 +139,21 @@ describe Folio::Ordinal::Page do
139
139
  end
140
140
  end
141
141
 
142
- describe "decorate" do
142
+ describe "BasicPage" do
143
143
  before do
144
- @page = Folio::Ordinal::Page.decorate([])
144
+ @page = Folio::Ordinal::BasicPage.new
145
145
  end
146
146
 
147
- it "should add page methods to the object" do
148
- assert_respond_to @page, :current_page
147
+ it "should be an Array" do
148
+ (Array === @page).must_equal true
149
149
  end
150
150
 
151
- it "should add ordinal page methods to the object" do
152
- @page.first_page.must_equal 1
151
+ it "should be a folio page" do
152
+ assert_respond_to @page, :current_page
153
153
  end
154
154
 
155
- it "should preserve other methods on the object" do
156
- assert_respond_to @page, :each
155
+ it "should be a ordinal folio page" do
156
+ @page.first_page.must_equal 1
157
157
  end
158
158
  end
159
159
 
@@ -162,12 +162,8 @@ describe Folio::Ordinal::Page do
162
162
  @page = Folio::Ordinal::Page.create
163
163
  end
164
164
 
165
- it "should be an Array at heart" do
166
- @page.must_be :is_a?, Array
167
- end
168
-
169
- it "should be decorated as an ordinal page" do
170
- @page.first_page.must_equal 1
165
+ it "should be a basic ordinal page" do
166
+ @page.class.must_equal Folio::Ordinal::BasicPage
171
167
  end
172
168
  end
173
169
  end
@@ -5,7 +5,7 @@ describe Folio::Ordinal do
5
5
  before do
6
6
  klass = Class.new do
7
7
  def build_page
8
- Folio::Page.create
8
+ Folio::Ordinal::Page.create
9
9
  end
10
10
 
11
11
  def fill_page(page)
@@ -18,11 +18,6 @@ describe Folio::Ordinal do
18
18
  end
19
19
 
20
20
  describe "paginate" do
21
- it "should decorate the result of build_page" do
22
- @page = @folio.paginate
23
- @page.is_a?(Folio::Ordinal::Page).must_equal true
24
- end
25
-
26
21
  describe "bounds checking" do
27
22
  before do
28
23
  @folio.per_page = 10
@@ -168,17 +168,17 @@ describe Folio::Page do
168
168
  end
169
169
  end
170
170
 
171
- describe "decorate" do
171
+ describe "BasicPage" do
172
172
  before do
173
- @page = Folio::Page.decorate([])
173
+ @page = Folio::BasicPage.new
174
174
  end
175
175
 
176
- it "should add page methods to the object" do
177
- assert_respond_to @page, :current_page
176
+ it "should be an Array" do
177
+ (Array === @page).must_equal true
178
178
  end
179
179
 
180
- it "should preserve other methods on the object" do
181
- assert_respond_to @page, :each
180
+ it "should be a folio page" do
181
+ assert_respond_to @page, :current_page
182
182
  end
183
183
  end
184
184
 
@@ -187,12 +187,8 @@ describe Folio::Page do
187
187
  @page = Folio::Page.create
188
188
  end
189
189
 
190
- it "should be an Array at heart" do
191
- @page.must_be :is_a?, Array
192
- end
193
-
194
- it "should be decorated as a page" do
195
- assert_respond_to @page, :current_page
190
+ it "should be a basic page" do
191
+ @page.class.must_equal Folio::BasicPage
196
192
  end
197
193
  end
198
194
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: folio-pagination
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2013-12-09 00:00:00.000000000 Z
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler