csv_cutter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 071df0271fb093ff02744fca2ee8d65cb79cd37b44e833790030d75edf43f916
4
+ data.tar.gz: '090c7344a970f71744bf7ae9670c86861f7fb3ccdbb78988f340fde708b987eb'
5
+ SHA512:
6
+ metadata.gz: 5ff0b0952b8534065c9aab1a48cffad03b339be51180a43e3f9c7b2a7ede2f7cc52b3feefbcf2006c45ea2ac9718ba2b434eaaefc9d2a649c257ecac2ab18a9e
7
+ data.tar.gz: 0dbfa3e8a2836ef7eff5a4573878abcff39d7d7f161cbbcae718a83c01be9f79244cdb606548087bf4db940f94090350f1d35f0855d4df4bfa34d77791cfdd9b
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-08-13
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in csv_cutter.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 TODO: Write your name
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
+ # CsvCutter
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csv_cutter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'csv_cutter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install csv_cutter
22
+
23
+ ## Usage
24
+
25
+ ### For Ruby
26
+
27
+ ```ruby
28
+ csv = CsvCutter::Csv.new(
29
+ headers: true,
30
+ encoding: 'Shift_JIS:UTF-8',
31
+ col_sep: ',',
32
+ quote_char: '"',
33
+ out_dir: 'output_dir',
34
+ )
35
+ csv.split_by_number_rows(file_path: 'file.csv', number_rows: 100)
36
+ ```
37
+
38
+ ### For CLI
39
+
40
+ ```ruby
41
+ csv_cutter split_by_number_rows sample.csv output_dir --headers --encoding Shift_JIS:UTF-8 --col_sep , --quote_char '"' --number_rows 100
42
+ ```
43
+
44
+ ### Options
45
+ |option name|type|description|default_value|
46
+ |---|---|---|---|
47
+ |headers|boolean|Header flag|false|
48
+ |encoding|string|CSV encording|-|
49
+ |col_sep|string|Column separate char|`,`|
50
+ |quote_char|string|Quote char|`"`|
51
+ |out_dir|string|Output directory|required|
52
+ |number_rows|number|split number rows|10000|
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csv_cutter.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
data/exe/csv_cutter ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'csv_cutter/cli'
4
+
5
+ CsvCutter::CLI.start(ARGV)
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv_cutter'
4
+ require 'thor'
5
+
6
+ module CsvCutter
7
+ class CLI < Thor
8
+ desc 'split_by_number_rows', 'split csv by number rows'
9
+ method_options headers: :boolean, encoding: :string, col_sep: :string, quote_char: :string, number_rows: :numeric
10
+ def split_by_number_rows(file_path, out_dir)
11
+ symbolized_key_options = options.except('number_rows').keys.inject({}) do |new_options, key|
12
+ new_options[key.to_sym] = options[key]
13
+ new_options
14
+ end
15
+
16
+ csv = CsvCutter::Csv.new(**symbolized_key_options.merge(out_dir: out_dir))
17
+ csv.split_by_number_rows(file_path: file_path, number_rows: options['number_rows'] || 10000)
18
+
19
+ puts 'Successfully split csv.'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+
5
+ module CsvCutter
6
+ class Csv
7
+ attr_reader :options, :out_dir, :file_mode
8
+
9
+ def initialize(headers: false, out_dir:, encoding: nil, col_sep: ',', quote_char: '"')
10
+ @options = {
11
+ headers: headers,
12
+ encoding: encoding,
13
+ col_sep: col_sep,
14
+ quote_char: quote_char,
15
+ }
16
+ @out_dir = out_dir
17
+ @file_mode = encoding ? "w:#{encoding}" : 'w'
18
+ end
19
+
20
+ def split_by_number_rows(file_path:, number_rows: 10000)
21
+ threads = []
22
+ file_name = File.basename(file_path, ".*")
23
+
24
+ CSV.foreach(file_path, **options).each_slice(number_rows).with_index(1) do |rows, idx|
25
+ threads << Thread.new(idx) do |idx|
26
+ CSV.open("#{out_dir}/#{file_name}_#{idx}.csv", file_mode, **options.slice(:col_sep, :quote_char)) do |csv_file|
27
+ rows.each_with_index do |row, idx|
28
+ case row
29
+ when CSV::Row
30
+ csv_file << row.headers if idx == 0
31
+ csv_file << row.fields
32
+ else
33
+ csv_file << row
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ threads.each(&:join)
40
+ true
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CsvCutter
4
+ VERSION = "0.1.0"
5
+ end
data/lib/csv_cutter.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "csv_cutter/version"
4
+ require_relative "csv_cutter/csv"
5
+
6
+ module CsvCutter
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,4 @@
1
+ module CsvCutter
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_cutter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - murajun1978
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: CsvCutter is a gem for splitting CSV files
28
+ email:
29
+ - ''
30
+ executables:
31
+ - csv_cutter
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - exe/csv_cutter
41
+ - lib/csv_cutter.rb
42
+ - lib/csv_cutter/cli.rb
43
+ - lib/csv_cutter/csv.rb
44
+ - lib/csv_cutter/version.rb
45
+ - sig/csv_cutter.rbs
46
+ homepage: https://github.com/murajun1978/csv_cutter
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/murajun1978/csv_cutter
51
+ source_code_uri: https://github.com/murajun1978/csv_cutter
52
+ changelog_uri: https://github.com/murajun1978/csv_cutter/blob/main/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.3.7
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A gem for splitting CSV files
72
+ test_files: []