simple_iterator 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6477d2c26d59c24b2643e36efc85be4f8bd90e56
4
+ data.tar.gz: cf2c0266ea36a1b3bc18eabe9c7bf00aaf433c1b
5
+ SHA512:
6
+ metadata.gz: 750dcb74319d1397079a47202f355f2c6b4f2d7254077c18651f536a737718eafc305b3609d540d09b87ea521716639a9e250bfdbcaff948befc1f23857463a4
7
+ data.tar.gz: fcdfca6dbbcc4709ac38d9238c3b2f076ea90ce97b544132644b048e54c06f6ca8e26f153b334bc59347336d0721ff1c74d2ccb7c800fb538c68a08067c85a26
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rspec'
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Joshimasa KISHIMOTO
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # SimpleIterator
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'simple_iterator'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install simple_iterator
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ itr = SimpleIterator::Plain.new(
23
+ offset: 0,
24
+ increment_offset: 100,
25
+ next_cb: lambda { |current_offset|
26
+ users = User.offset(current_offset).limit(100)
27
+ users.length > 0 ? users : nil
28
+ }
29
+ )
30
+
31
+ while (users = itr.next)
32
+ users.each do |user|
33
+ p user
34
+ end
35
+ end
36
+ ```
37
+
38
+ ```ruby
39
+ itr = SimpleIterator::File.new(
40
+ file_path: tsv_file,
41
+ separate: "\t",
42
+ increment_lines: 1000,
43
+ line_separate_cb: lambda { |row|
44
+ {
45
+ id: row[0],
46
+ name: row[1]
47
+ }
48
+ }
49
+ )
50
+
51
+ while (rows = itr.next)
52
+ rows.each do |row|
53
+ p row[:id]
54
+ p row[:name]
55
+ end
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jkishimoto/simple_iterator.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
66
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_iterator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require 'simple_iterator/version'
2
+ require 'simple_iterator/plain'
3
+ require 'simple_iterator/file'
4
+
5
+ module SimpleIterator
6
+ end
@@ -0,0 +1,36 @@
1
+ module SimpleIterator
2
+ class File < SimpleIterator::Plain
3
+ def initialize(args)
4
+ increment_lines = args[:increment_lines] || raise('increment_lines is required')
5
+ @file_path = args[:file_path] || raise('file_path is required')
6
+ @separate = args[:separate] || "\t"
7
+ @skip_headers = args[:skip_headers] || false
8
+ @line_separate_cb = args[:line_separate_cb] || lambda { |row| row }
9
+ @fh = ::File.open(@file_path, 'rb:UTF-8:UTF-8', undef: :replace)
10
+
11
+ next_cb = lambda { |offset|
12
+ @fh.gets if offset == 0 && @skip_headers
13
+
14
+ data = []
15
+ (0 .. increment_lines - 1).each do
16
+ line = @fh.gets
17
+ line.chomp! if !line.nil? && line.length > 0
18
+
19
+ break if line.nil?
20
+
21
+ row = @line_separate_cb.call(line.split(@separate))
22
+
23
+ data.push(row)
24
+ end
25
+
26
+ data.length > 0 ? data : nil
27
+ }
28
+
29
+ super(
30
+ next_cb: next_cb,
31
+ increment_offset: increment_lines,
32
+ offset: 0
33
+ )
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ module SimpleIterator
2
+ class Plain
3
+ def initialize(args)
4
+ @next_cb = args[:next_cb] || raise('next_cb is required')
5
+ @increment_offset = args[:increment_offset] || raise('increment_offset is required')
6
+ @current_offset = args[:offset] || 0
7
+ end
8
+
9
+ def next
10
+ res = @next_cb.call(@current_offset)
11
+ @current_offset += @increment_offset
12
+ res
13
+ end
14
+
15
+ def index
16
+ if @current_offset > @increment_offset
17
+ (@current_offset / @increment_offset).to_i
18
+ else
19
+ 1
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleIterator
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_iterator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'simple_iterator'
8
+ spec.version = SimpleIterator::VERSION
9
+ spec.authors = ['Joshimasa KISHIMOTO']
10
+ spec.email = ['joshimasa.kishimoto@gmail.com']
11
+
12
+ spec.summary = %q{Simple iterator}
13
+ spec.description = %q{Simple iterator}
14
+ spec.homepage = 'https://github.com/jkishimoto/simple_iterator'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.12'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'rspec'
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_iterator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Joshimasa KISHIMOTO
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple iterator
56
+ email:
57
+ - joshimasa.kishimoto@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/simple_iterator.rb
72
+ - lib/simple_iterator/file.rb
73
+ - lib/simple_iterator/plain.rb
74
+ - lib/simple_iterator/version.rb
75
+ - simple_iterator.gemspec
76
+ homepage: https://github.com/jkishimoto/simple_iterator
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple iterator
100
+ test_files: []