page_iterator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts = %w(-fs --color)
7
+ end
8
+ rescue LoadError
9
+ task :spec do
10
+ abort "Run `rake spec:deps` to be able to run the specs"
11
+ end
12
+
13
+ namespace :spec do
14
+ desc "Ensure spec dependencies are installed"
15
+ task :deps do
16
+ sh "gem list rspec | (grep 'rspec (2.0' 1> /dev/null) || gem install rspec --no-ri --no-rdoc"
17
+ end
18
+ end
19
+ end
20
+
21
+ gem_name = Dir["*.gemspec"].first.match(/\A(\w*).gemspec\z/).captures.first
22
+
23
+ desc "Build the gem, removing old ones"
24
+ task :build do
25
+ # gem_name = 'rakesupport'
26
+ gem_files = Dir['*.gem']
27
+ commands = []
28
+ commands << ["rm #{ gem_files.join(' ') }"] unless gem_files.empty?
29
+ commands << ["gem build #{gem_name}.gemspec"]
30
+ sh commands.join(" && ")
31
+ end
32
+
33
+ desc "Build and install the gem"
34
+ task :install => :build do
35
+ # gem_name = 'rakesupport'
36
+ gem_file = Dir['*.gem'].first
37
+ if gem_file.nil?
38
+ puts "There is no gem file to install"
39
+ else
40
+ sh "gem uninstall --ignore-dependencies #{gem_name}; gem install #{gem_file}"
41
+ end
42
+ end
43
+
44
+ task :default => :spec
@@ -0,0 +1,70 @@
1
+ class PageIterator
2
+ DEFAULT_ITENS_PER_PAGE = 50
3
+
4
+ attr_reader :total_itens, :per_page
5
+
6
+ def initialize(total_itens, logfile, itens_per_page=DEFAULT_ITENS_PER_PAGE)
7
+ @total_itens = total_itens
8
+ @itens_per_page = itens_per_page
9
+ @logfile = logfile
10
+ @current_page = current_page_from_file
11
+ end
12
+
13
+ def total_pages
14
+ pages = @total_itens / @itens_per_page
15
+ if pages == 0
16
+ 1
17
+ else
18
+ last_page_itens = @total_itens % @itens_per_page
19
+ last_page_itens > 0 ? pages + 1 : pages
20
+ end
21
+ end
22
+
23
+ def next!
24
+ @current_page += 1
25
+ log @current_page
26
+ end
27
+
28
+ def previous!
29
+ @current_page -= 1
30
+ log @current_page
31
+ end
32
+
33
+ def remaining_itens
34
+ if @current_page > total_pages
35
+ 0
36
+ else
37
+ @total_itens - (@current_page - 1) * @itens_per_page
38
+ end
39
+ end
40
+
41
+ def remaining_pages
42
+ @current_page..total_pages
43
+ end
44
+
45
+ def each_remaining_page!
46
+ remaining_pages.each do |page|
47
+ yield page
48
+ self.next!
49
+ end
50
+ self.next!
51
+ end
52
+
53
+ private
54
+
55
+ def current_page_from_file
56
+ current_page_from_file = File.exist?(@logfile) && File.read(@logfile).chomp
57
+ begin
58
+ current_page_from_file = current_page_from_file.to_i
59
+ current_page_from_file > 0 ? current_page_from_file : 1
60
+ rescue
61
+ 1
62
+ end
63
+ end
64
+
65
+ def log(page_number)
66
+ File.open(@logfile, "w") do |file|
67
+ file.print page_number
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,14 @@
1
+ gem_name = Dir["*.gemspec"].first.match(/\A(\w*).gemspec\z/).captures.first
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = gem_name
5
+ s.version = "0.1.0"
6
+ s.summary = "A library to split a number of data in pages and iterate through them, keeping track in log"
7
+ s.author = "Eric Fer"
8
+ s.email = "eric.fer@gmail.com"
9
+ s.homepage = "https://github.com/ericfer/#{gem_name}"
10
+ s.add_development_dependency "rspec"
11
+ s.add_development_dependency "mocha"
12
+ s.rubyforge_project = gem_name
13
+ s.files = Dir["{lib/**/*.rb,README.rdoc,Rakefile,*.gemspec}"]
14
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: page_iterator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Eric Fer
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-20 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mocha
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ description:
50
+ email: eric.fer@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/page_iterator.rb
59
+ - Rakefile
60
+ - page_iterator.gemspec
61
+ has_rdoc: true
62
+ homepage: https://github.com/ericfer/page_iterator
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project: page_iterator
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: A library to split a number of data in pages and iterate through them, keeping track in log
95
+ test_files: []
96
+