dunkstewart-webbits 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +3 -0
- data/.rvmrc +2 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +29 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +27 -0
- data/VERSION +1 -0
- data/dunkstewart-webbits.gemspec +62 -0
- data/lib/paged.rb +58 -0
- data/lib/webbits.rb +1 -0
- data/spec/paged_spec.rb +75 -0
- data/spec/spec_helper.rb +3 -0
- metadata +122 -0
data/.rspec
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ZenTest (4.5.0)
|
5
|
+
autotest (4.4.6)
|
6
|
+
ZenTest (>= 4.4.1)
|
7
|
+
diff-lcs (1.1.2)
|
8
|
+
git (1.2.5)
|
9
|
+
jeweler (1.5.2)
|
10
|
+
bundler (~> 1.0.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rake
|
13
|
+
rake (0.8.7)
|
14
|
+
rspec (2.5.0)
|
15
|
+
rspec-core (~> 2.5.0)
|
16
|
+
rspec-expectations (~> 2.5.0)
|
17
|
+
rspec-mocks (~> 2.5.0)
|
18
|
+
rspec-core (2.5.1)
|
19
|
+
rspec-expectations (2.5.0)
|
20
|
+
diff-lcs (~> 1.1.2)
|
21
|
+
rspec-mocks (2.5.0)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
autotest
|
28
|
+
jeweler
|
29
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Duncan Stewart
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= webbits
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to webbits
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Duncan Stewart. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "dunkstewart-webbits"
|
16
|
+
gem.homepage = "http://github.com/dunkstewart/webbits"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = "Useful bits of web code for my projects"
|
19
|
+
gem.description = "No really, that's all it is!"
|
20
|
+
gem.email = "dunkstewart@gmail.com"
|
21
|
+
gem.authors = ["Duncan Stewart"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dunkstewart-webbits}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Duncan Stewart"]
|
12
|
+
s.date = %q{2011-04-12}
|
13
|
+
s.description = %q{No really, that's all it is!}
|
14
|
+
s.email = %q{dunkstewart@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".rspec",
|
21
|
+
".rvmrc",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"dunkstewart-webbits.gemspec",
|
29
|
+
"lib/paged.rb",
|
30
|
+
"lib/webbits.rb",
|
31
|
+
"spec/paged_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/dunkstewart/webbits}
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.5.2}
|
38
|
+
s.summary = %q{Useful bits of web code for my projects}
|
39
|
+
s.test_files = [
|
40
|
+
"spec/paged_spec.rb",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<rspec>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<autotest>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
53
|
+
s.add_dependency(%q<autotest>, [">= 0"])
|
54
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
58
|
+
s.add_dependency(%q<autotest>, [">= 0"])
|
59
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
data/lib/paged.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
class CleanSlate
|
2
|
+
instance_methods.each { |m| undef_method m unless m.to_s =~ /^__/ }
|
3
|
+
end
|
4
|
+
|
5
|
+
class Paged < CleanSlate
|
6
|
+
def initialize(items, opts)
|
7
|
+
raise ArgumentError.new ':page_size must be a positive number' unless opts[:page_size] and opts[:page_size] > 0
|
8
|
+
@items = items
|
9
|
+
@page_size = opts[:page_size]
|
10
|
+
@page = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
def page=(page)
|
14
|
+
raise ArgumentError.new 'page must be a number' if not page.kind_of? Numeric
|
15
|
+
@page = page - 1
|
16
|
+
end
|
17
|
+
|
18
|
+
def page
|
19
|
+
@page + 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def previous_page
|
23
|
+
return if outside_bounds
|
24
|
+
page - 1 if page > 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def next_page
|
28
|
+
return if outside_bounds
|
29
|
+
page + 1 if page < total_pages
|
30
|
+
end
|
31
|
+
|
32
|
+
def respond_to?(symbol, include_private=false)
|
33
|
+
@items.respond_to? symbol, include_private
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def method_missing(symbol, *args, &block)
|
39
|
+
page_of_items.send(symbol, *args, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def page_of_items
|
43
|
+
return [] if outside_bounds
|
44
|
+
@items[start_index, @page_size]
|
45
|
+
end
|
46
|
+
|
47
|
+
def start_index
|
48
|
+
@page * @page_size
|
49
|
+
end
|
50
|
+
|
51
|
+
def total_pages
|
52
|
+
(@items.size.quo @page_size).ceil
|
53
|
+
end
|
54
|
+
|
55
|
+
def outside_bounds
|
56
|
+
@page < 0 or @page > total_pages
|
57
|
+
end
|
58
|
+
end
|
data/lib/webbits.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'paged'
|
data/spec/paged_spec.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Paged do
|
4
|
+
context 'a list of items' do
|
5
|
+
before :each do
|
6
|
+
@list = ['apple', 'banana', 'cat', 'dog', 'elephant', 'frog', 'goat', 'house', 'ice', 'john']
|
7
|
+
@paged_list = Paged.new @list, :page_size => 3
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should show first page of items' do
|
11
|
+
@paged_list.should =~ ['apple', 'banana', 'cat']
|
12
|
+
@paged_list.next_page.should == 2
|
13
|
+
@paged_list.previous_page.should_not be
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should show second page of items' do
|
17
|
+
@paged_list.page = 2
|
18
|
+
@paged_list.should =~ ['dog', 'elephant', 'frog']
|
19
|
+
@paged_list.next_page.should == 3
|
20
|
+
@paged_list.previous_page.should == 1
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should show incomplete last page' do
|
24
|
+
@paged_list.page = 4
|
25
|
+
@paged_list.should =~ ['john']
|
26
|
+
@paged_list.next_page.should_not be
|
27
|
+
@paged_list.previous_page.should == 3
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should show nothing after last page' do
|
31
|
+
@paged_list.page = 99
|
32
|
+
@paged_list.should have(:no).items
|
33
|
+
@paged_list.next_page.should_not be
|
34
|
+
@paged_list.previous_page.should_not be
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should show nothing before first page' do
|
38
|
+
@paged_list.page = -1
|
39
|
+
@paged_list.should have(:no).items
|
40
|
+
@paged_list.next_page.should_not be
|
41
|
+
@paged_list.previous_page.should_not be
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should raise error for nil or non positive page size' do
|
45
|
+
expect { Paged.new @list, :page_size => nil }.to raise_error ArgumentError
|
46
|
+
expect { Paged.new @list, :page_size => 0 }.to raise_error ArgumentError
|
47
|
+
expect { Paged.new @list, :page_size => -1 }.to raise_error ArgumentError
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should raise error for nil or non number page' do
|
51
|
+
expect { @paged_list.page = nil }.to raise_error ArgumentError
|
52
|
+
expect { @paged_list.page = "2" }.to raise_error ArgumentError
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should show one page with no previous or next when page size matches list size' do
|
56
|
+
paged_list = Paged.new ['apple', 'banana'], :page_size => 2
|
57
|
+
paged_list.should =~ ['apple', 'banana']
|
58
|
+
paged_list.next_page.should_not be
|
59
|
+
paged_list.previous_page.should_not be
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should show two pages when list size is one greater then page size' do
|
63
|
+
paged_list = Paged.new ['apple', 'banana', 'cake'], :page_size => 2
|
64
|
+
paged_list.should =~ ['apple', 'banana']
|
65
|
+
paged_list.next_page.should == 2
|
66
|
+
paged_list.previous_page.should_not be
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should iterate page from list' do
|
70
|
+
x = 0
|
71
|
+
@paged_list.each { |item| x += 1 }
|
72
|
+
x.should == 3
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dunkstewart-webbits
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Duncan Stewart
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-12 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
name: rspec
|
33
|
+
version_requirements: *id001
|
34
|
+
prerelease: false
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :runtime
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
name: autotest
|
47
|
+
version_requirements: *id002
|
48
|
+
prerelease: false
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
type: :development
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
name: jeweler
|
61
|
+
version_requirements: *id003
|
62
|
+
prerelease: false
|
63
|
+
description: No really, that's all it is!
|
64
|
+
email: dunkstewart@gmail.com
|
65
|
+
executables: []
|
66
|
+
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files:
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.rdoc
|
72
|
+
files:
|
73
|
+
- .rspec
|
74
|
+
- .rvmrc
|
75
|
+
- Gemfile
|
76
|
+
- Gemfile.lock
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.rdoc
|
79
|
+
- Rakefile
|
80
|
+
- VERSION
|
81
|
+
- dunkstewart-webbits.gemspec
|
82
|
+
- lib/paged.rb
|
83
|
+
- lib/webbits.rb
|
84
|
+
- spec/paged_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://github.com/dunkstewart/webbits
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 3
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
requirements: []
|
114
|
+
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 1.5.2
|
117
|
+
signing_key:
|
118
|
+
specification_version: 3
|
119
|
+
summary: Useful bits of web code for my projects
|
120
|
+
test_files:
|
121
|
+
- spec/paged_spec.rb
|
122
|
+
- spec/spec_helper.rb
|