pagelux 0.0.1 → 0.1.0

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: feafcc5a8a3b091d53e9deb825a65ba75a88fe81
4
- data.tar.gz: d421ad5c0a551e62686fa0a7569f98ff8ecf370f
3
+ metadata.gz: dbeb5f2ff84c4cd8b9cb8455a173e0a025dfa721
4
+ data.tar.gz: ca7386b257dde68a6dc9fde359362ed9767e363d
5
5
  SHA512:
6
- metadata.gz: 1afbc2a396018eecbaf43967a19cfcfa8274b96e34c68dfb492a7399328d120a0cba1b9bb9dc565e20a4de286312d5474cfc30ed2e945886ea66ce4b775264af
7
- data.tar.gz: e780f9048e4df2b67527396be0b3502c46defc1f21992277aedc37c127ac03e6cc59a4b53ac75d0bb95525a3c84af099bbc5dbab4e0d41cb1bc68952428225ea
6
+ metadata.gz: c88f8c3b298e9c218cd6f72ca2225bde9e88fbd5f84f9db189f33ffbd9a58dcfa610bd3380821d8ad44b6a4fffa762796b2879e470cdcbf6444423a86d1e99e3
7
+ data.tar.gz: bd1bf24b25af0a785770f6bac1e76aeb36a07cfc0e7a5952aac469f61d4e98adf15f718654495b66b1dd7970baad1b9b72ca95287bebbd3c85bc56a11fe9e60f
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -1,5 +1,11 @@
1
+ require 'forwardable'
2
+
1
3
  require "pagelux/query"
4
+ require "pagelux/query_paginator"
2
5
  require "pagelux/version"
3
6
 
4
7
  module Pagelux
8
+ def self.paginate(query, params)
9
+ QueryPaginator.new(query).paginate(params)
10
+ end
5
11
  end
@@ -2,9 +2,23 @@ module Pagelux
2
2
  class Query
3
3
  attr_accessor :page, :limit
4
4
 
5
- def initialize(page, limit=100)
6
- @page = page.to_i
7
- @limit = limit.to_i
5
+ def initialize(page=nil, limit=nil)
6
+ @limit = (limit || self.class.default_limit).to_i
7
+ @page = normalize_page(page)
8
+ end
9
+
10
+ def self.default_page
11
+ 1
12
+ end
13
+
14
+ def self.default_limit
15
+ 100
16
+ end
17
+
18
+ def normalize_page(n)
19
+ n = (n || self.class.default_page).to_i
20
+ n = self.class.default_page if n < self.class.default_page
21
+ n
8
22
  end
9
23
 
10
24
  def perform(query)
@@ -0,0 +1,51 @@
1
+ module Pagelux
2
+ class QueryPaginator
3
+ extend Forwardable
4
+
5
+ attr_reader :base_query
6
+
7
+ def initialize(base_query)
8
+ @base_query = base_query
9
+ end
10
+
11
+ def paginate(params)
12
+ @pagination_data = PaginationData.new(params[:page], params[:limit], base_query.count)
13
+ limit_query
14
+ end
15
+
16
+ def limit_query
17
+ base_query.offset((page-1) * limit).limit(limit)
18
+ end
19
+
20
+ def_delegators :@pagination_data, :page, :limit
21
+
22
+ class PaginationData
23
+ attr_reader :page, :limit, :total
24
+
25
+ def initialize(page=nil, limit=nil, query_total)
26
+ @total = query_total
27
+ @limit = (limit || self.class.default_limit).to_i
28
+ @page = normalize_page(page)
29
+ end
30
+
31
+ def self.default_page
32
+ 1
33
+ end
34
+
35
+ def self.default_limit
36
+ 100
37
+ end
38
+
39
+ def normalize_page(n)
40
+ n = (n || self.class.default_page).to_i
41
+ n = self.class.default_page if n < self.class.default_page
42
+ n = n > last_page ? last_page : n
43
+ n
44
+ end
45
+
46
+ def last_page
47
+ (total/limit.to_f).ceil
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Pagelux
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -6,8 +6,8 @@ require 'pagelux/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "pagelux"
8
8
  spec.version = Pagelux::VERSION
9
- spec.authors = ["Rae Bonfanti & Ryan Buckley"]
10
- spec.email = ["rae.bonfanti+ryan.buckley@socialchorus.com"]
9
+ spec.authors = ["Rae Bonfanti", "Ryan Buckley", "Kane Baccigalupi", "Sowjanya Mudunuri"]
10
+ spec.email = ["developers@socialchorus.com"]
11
11
  spec.description = %q{Basic pagination for ActiveRecord::Relation objects}
12
12
  spec.summary = %q{Usage: Pagelux::Query.new(page, limit).perform(the_ar_query)}
13
13
  spec.homepage = ""
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ # spec.add_dependency ''
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rspec'
23
26
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pagelux, 'facade' do
4
+ describe '.paginate(query, params)' do
5
+ it "wraps the QueryPaginator" do
6
+ params = double('params')
7
+ query = double('query')
8
+ paginator = double('paginator')
9
+
10
+ Pagelux::QueryPaginator.should_receive(:new).with(query).and_return(paginator)
11
+ paginator.should_receive(:paginate).with(params)
12
+
13
+ Pagelux.paginate(query, params)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pagelux::QueryPaginator do
4
+ let(:query) { Query.new(('a'..'m').to_a) }
5
+ let(:paginator) { Pagelux::QueryPaginator.new(query) }
6
+ # let(:results) { paginator.paginate(params) }
7
+
8
+ describe 'normalizing pagination values' do
9
+ let(:params) { {} }
10
+
11
+ context 'when nothing is passed in' do
12
+ it 'uses defaults' do
13
+ paginator.paginate(params)
14
+
15
+ paginator.page.should == 1
16
+ paginator.limit.should == 100
17
+ end
18
+ end
19
+
20
+ context 'when page is less than 1' do
21
+ let(:params) {
22
+ {
23
+ page: 0
24
+ }
25
+ }
26
+
27
+ it "uses first page" do
28
+ paginator.paginate(params)
29
+
30
+ paginator.page.should == 1
31
+ end
32
+ end
33
+
34
+ context 'when the page is greater than what is available' do
35
+ let(:params) {
36
+ {
37
+ page: 4,
38
+ limit: 5
39
+ }
40
+ }
41
+
42
+ it 'gets the last page' do
43
+ paginator.paginate(params)
44
+
45
+ paginator.page.should == 3
46
+ end
47
+ end
48
+ end
49
+
50
+ describe 'query pagination' do
51
+ let(:results) { paginator.paginate({page: page, limit: 5}) }
52
+
53
+ context 'when on first page' do
54
+ let(:page) { 1 }
55
+
56
+ it "will return the right records" do
57
+ results.should == ('a'..'e').to_a
58
+ end
59
+ end
60
+
61
+ context 'when on middle page' do
62
+ let(:page) { 2 }
63
+ it "will return the right records for a middle page" do
64
+ results.should == ('f'..'j').to_a
65
+ end
66
+ end
67
+
68
+ context 'when on last page' do
69
+ let(:page) { 3 }
70
+
71
+ it "will return the right records no the last page" do
72
+ results.should == ('k'..'m').to_a
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,35 +1,64 @@
1
- require 'minitest/autorun'
2
- require 'fixtures/query'
3
- require File.expand_path('../../lib/pagelux/query', __FILE__)
1
+ require 'spec_helper'
4
2
 
5
- class TestPagelux < MiniTest::Unit::TestCase
3
+ describe Pagelux::Query do
4
+ let(:paginator) { Pagelux::Query.new(page, limit) }
6
5
 
7
- def setup
8
- @pagelux = Pagelux::Query.new(1)
9
- end
6
+ describe 'normalizing pagination values' do
7
+ context 'when nothing is passed in' do
8
+ let(:paginator) { Pagelux::Query.new }
10
9
 
11
- def test_defaults
12
- assert_equal 100, @pagelux.limit
13
- assert_equal 1, @pagelux.page
14
- end
10
+ it 'uses defaults' do
11
+ paginator.page.should == 1
12
+ paginator.limit.should == 100
13
+ end
14
+ end
15
+
16
+ context 'when nils are passed in (a real case with params)' do
17
+ let(:page) { nil }
18
+ let(:limit) { nil }
19
+
20
+ it 'uses defaults' do
21
+ paginator.page.should == 1
22
+ paginator.limit.should == 100
23
+ end
24
+ end
25
+
26
+ context 'when page is less than 1' do
27
+ let(:page) { 0 }
28
+ let(:limit) { 100 }
15
29
 
16
- def test_perform
17
- @pagelux.limit = 5
18
- query = @pagelux.perform(sample_query)
19
- assert_equal 5, query.length
20
- assert_equal ('a'..'e').to_a, query
21
- @pagelux.page += 1
22
- query = @pagelux.perform(sample_query)
23
- assert_equal 5, query.length
24
- assert_equal ('f'..'j').to_a, query
25
- @pagelux.page += 1
26
- query = @pagelux.perform(sample_query)
27
- assert_equal 3, query.length
28
- assert_equal ('k'..'m').to_a, query
30
+ it "uses first page" do
31
+ paginator.page.should == 1
32
+ end
33
+ end
29
34
  end
30
35
 
31
- # query size is 13
32
- def sample_query
33
- @sample_query ||= Query.new(('a'..'m').to_a)
36
+ describe 'query pagination' do
37
+ let(:limit) { 5 }
38
+ let(:sample_query) { Query.new(('a'..'m').to_a) }
39
+ let(:results) { paginator.perform(sample_query) }
40
+
41
+ context 'when on first page' do
42
+ let(:page) { 1 }
43
+
44
+ it "will return the right records" do
45
+ results.should == ('a'..'e').to_a
46
+ end
47
+ end
48
+
49
+ context 'when on middle page' do
50
+ let(:page) { 2 }
51
+ it "will return the right records for a middle page" do
52
+ results.should == ('f'..'j').to_a
53
+ end
54
+ end
55
+
56
+ context 'when on last page' do
57
+ let(:page) { 3 }
58
+
59
+ it "will return the right records no the last page" do
60
+ results.should == ('k'..'m').to_a
61
+ end
62
+ end
34
63
  end
35
64
  end
@@ -0,0 +1,22 @@
1
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
2
+
3
+ $: << File.dirname(__FILE__) + "/../lib"
4
+ require 'pagelux'
5
+
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # Require this file using `require "spec_helper"` to ensure that it is only
9
+ # loaded once.
10
+ #
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
@@ -9,4 +9,8 @@ class Query < Struct.new(:items, :index)
9
9
  def limit(num)
10
10
  items.slice(index, num)
11
11
  end
12
+
13
+ def count
14
+ items.length
15
+ end
12
16
  end
metadata CHANGED
@@ -1,14 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pagelux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
- - Rae Bonfanti & Ryan Buckley
7
+ - Rae Bonfanti
8
+ - Ryan Buckley
9
+ - Kane Baccigalupi
10
+ - Sowjanya Mudunuri
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []
11
- date: 2013-12-06 00:00:00.000000000 Z
14
+ date: 2013-12-20 00:00:00.000000000 Z
12
15
  dependencies:
13
16
  - !ruby/object:Gem::Dependency
14
17
  name: bundler
@@ -38,24 +41,43 @@ dependencies:
38
41
  - - '>='
39
42
  - !ruby/object:Gem::Version
40
43
  version: '0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: rspec
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
41
58
  description: Basic pagination for ActiveRecord::Relation objects
42
59
  email:
43
- - rae.bonfanti+ryan.buckley@socialchorus.com
60
+ - developers@socialchorus.com
44
61
  executables: []
45
62
  extensions: []
46
63
  extra_rdoc_files: []
47
64
  files:
48
65
  - .gitignore
66
+ - .rspec
49
67
  - Gemfile
50
68
  - LICENSE.txt
51
69
  - README.md
52
70
  - Rakefile
53
71
  - lib/pagelux.rb
54
72
  - lib/pagelux/query.rb
73
+ - lib/pagelux/query_paginator.rb
55
74
  - lib/pagelux/version.rb
56
75
  - pagelux.gemspec
57
- - spec/fixtures/query.rb
76
+ - spec/pagelux_spec.rb
77
+ - spec/query_paginator_spec.rb
58
78
  - spec/query_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/support/query.rb
59
81
  homepage: ''
60
82
  licenses:
61
83
  - MIT
@@ -76,10 +98,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
98
  version: '0'
77
99
  requirements: []
78
100
  rubyforge_project:
79
- rubygems_version: 2.0.6
101
+ rubygems_version: 2.0.3
80
102
  signing_key:
81
103
  specification_version: 4
82
104
  summary: 'Usage: Pagelux::Query.new(page, limit).perform(the_ar_query)'
83
105
  test_files:
84
- - spec/fixtures/query.rb
106
+ - spec/pagelux_spec.rb
107
+ - spec/query_paginator_spec.rb
85
108
  - spec/query_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/query.rb