Skip to content

Commit f2c0f33

Browse files
committed
Remove all the "magic" from #options
1 parent b4d8219 commit f2c0f33

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

lib/pliny/helpers/paginator/integer_paginator.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Pliny::Helpers
22
module Paginator
33
class IntegerPaginator
44
attr_reader :sinatra, :count
5-
attr_writer :options
5+
attr_accessor :options
66

77
class << self
88
def run(*args, &block)
@@ -13,23 +13,21 @@ def run(*args, &block)
1313
def initialize(sinatra, count, options = {})
1414
@sinatra = sinatra
1515
@count = count
16-
@opts = options
16+
@options = options
1717
end
1818

1919
def run
20+
options = calculate_pages
21+
2022
{
2123
order_by: options[:sort_by],
2224
offset: options[:first],
2325
limit: options[:args][:max]
2426
}
2527
end
2628

27-
def options
28-
@options ||= calculate_pages
29-
end
30-
3129
def calculate_pages
32-
Paginator.run(self, count, @opts) do |paginator|
30+
Paginator.run(self, count, options) do |paginator|
3331
max = paginator[:args][:max].to_i
3432
paginator[:last] =
3533
paginator[:first].to_i + max - 1

lib/pliny/helpers/paginator/paginator.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class Paginator
99
ARGS = /(?<args>.*)/
1010
RANGE = /\A#{SORT_BY}(?:\s+#{FIRST})?(\.{2}#{LAST})?#{COUNT}?(;\s*#{ARGS})?\z/
1111

12+
attr_accessor :options
1213
attr_reader :sinatra, :count
13-
attr_writer :options
1414

1515
class << self
1616
def run(*args, &block)
@@ -21,7 +21,7 @@ def run(*args, &block)
2121
def initialize(sinatra, count, options = {})
2222
@sinatra = sinatra
2323
@count = count
24-
@opts =
24+
@options =
2525
{
2626
accepted_ranges: [:id],
2727
sort_by: :id,
@@ -35,6 +35,8 @@ def initialize(sinatra, count, options = {})
3535
end
3636

3737
def run
38+
options.merge!(request_options)
39+
3840
result = yield(self) if block_given?
3941

4042
halt unless valid_options?
@@ -43,10 +45,6 @@ def run
4345
result
4446
end
4547

46-
def options
47-
@options ||= @opts.merge(request_options)
48-
end
49-
5048
def request_options
5149
range = sinatra.request.env['Range']
5250
return {} if range.nil? || range.empty?

test/helpers/paginator/paginator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
describe '#run' do
1111
it 'evaluates block' do
12+
mock(subject).request_options { {} }
1213
mock(subject).valid_options? { true }
1314
mock(subject).set_headers
1415
subject.instance_variable_set(:@options, args: { max: 200 })

0 commit comments

Comments
 (0)