csv_files 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +3 -0
- data/csv_files.gemspec +24 -0
- data/lib/csv_files.rb +54 -0
- data/lib/csv_files/version.rb +3 -0
- data/spec/csv_files_spec.rb +33 -0
- data/spec/spec_helper.rb +1 -0
- data/tasks/rspec.rake +3 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21d49d53fcf95ff07842f2d2149fb1591e6ab89f
|
4
|
+
data.tar.gz: f091feec0c93607120f6c69526a20b5b70cd3615
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54e5895a4dbba909d36b2c6079ab83242e3deeb9f8850318c64e90bcb435cb824100c5fe098bebbe24b93a8243aee61dc081614b3c3a34ae9974ad5ff9f8e179
|
7
|
+
data.tar.gz: ebdcf6c5b30460ab5d739269c3ab0f7e901a60cafdb25d547c1f6571ec00c79c068352171104ef6c27bb811d891d53213d6fc761fc168d8436a4c46a98622cb0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Damien Brzoska
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# CsvFiles
|
2
|
+
|
3
|
+
CSV files generator for large datasets. This gem let you save large datasets as soon as the row limit (default to 10_000) is reach.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'csv_files'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install csv_files
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
csv = CsvFiles::Generator.new("path-to-export-file", ['header', 'values', 'goes', 'here'], 10_000)
|
25
|
+
```
|
26
|
+
|
27
|
+
You need to define you storing method as follow:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
csv.store_fn = lambda do |filename, text|
|
31
|
+
puts filename
|
32
|
+
puts text
|
33
|
+
# You can store this to S3 or your file system in this block
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
To insert data you can use the following:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
csv << ['my', 'values', 'go', 'here']
|
41
|
+
```
|
42
|
+
|
43
|
+
A file will automatically be created as soon as the limit is reach. If the limit isn't reach, call the following method:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
csv.store
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it ( https://github.com/[my-github-username]/csv_files/fork )
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/csv_files.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv_files/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv_files"
|
8
|
+
spec.version = CsvFiles::VERSION
|
9
|
+
spec.authors = ["Damien Brzoska"]
|
10
|
+
spec.email = ["damien.brz@gmail.com"]
|
11
|
+
spec.description = %q{CSV files generator for large datasets}
|
12
|
+
spec.summary = ""
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/csv_files.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require "csv_files/version"
|
3
|
+
|
4
|
+
module CsvFiles
|
5
|
+
class Generator
|
6
|
+
attr_accessor :filename, :headers, :limit, :items, :count, :store_fn
|
7
|
+
|
8
|
+
def initialize(filename, headers, limit = 10_000)
|
9
|
+
raise "Filename must be present" if filename.nil? || filename.to_s == ""
|
10
|
+
raise "Headers must be present" if headers.nil?
|
11
|
+
raise "Headers must be an array" if !headers.is_a?(Array) || headers.size.zero?
|
12
|
+
raise "Limit must be a number" if !limit.is_a?(Numeric)
|
13
|
+
|
14
|
+
self.filename = formatted_filename(filename.to_s)
|
15
|
+
self.limit = limit.round
|
16
|
+
self.headers = headers
|
17
|
+
self.items = Array.new()
|
18
|
+
self.count = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def <<(row)
|
22
|
+
@items << row
|
23
|
+
store if limit_reached?
|
24
|
+
row
|
25
|
+
end
|
26
|
+
|
27
|
+
def store
|
28
|
+
csv = CSV.generate do |r|
|
29
|
+
r << @headers
|
30
|
+
@items.each { |i| r << i }
|
31
|
+
end
|
32
|
+
store_fn.call(generated_filename, csv)
|
33
|
+
@items.clear
|
34
|
+
@count += 1
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def limit_reached?
|
39
|
+
@items.size >= @limit
|
40
|
+
end
|
41
|
+
|
42
|
+
def generated_filename
|
43
|
+
"#{@filename}-#{@count}.csv"
|
44
|
+
end
|
45
|
+
|
46
|
+
def formatted_filename(name)
|
47
|
+
if (match = name.match(/(.*)(\.csv)$/))
|
48
|
+
match[1]
|
49
|
+
else
|
50
|
+
name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CsvFiles::Generator do
|
4
|
+
it "should be initialize with at least a filename" do
|
5
|
+
subject = CsvFiles::Generator.new("my-file-name", ['email'], 1_000)
|
6
|
+
expect(subject.filename).to eq("my-file-name")
|
7
|
+
expect(subject.limit).to eq(1_000)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should raise an exception when filename is missing or headers are missing or limit is not a number" do
|
11
|
+
expect { CsvFiles::Generator.new() }.to raise_error
|
12
|
+
expect { CsvFiles::Generator.new('filename') }.to raise_error
|
13
|
+
expect { CsvFiles::Generator.new('filename', ['email'], 'abc') }.to raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#<<" do
|
17
|
+
subject { CsvFiles::Generator.new("export", ['value'], 1) }
|
18
|
+
|
19
|
+
it "should raise an error if #store_fn has not be set" do
|
20
|
+
expect {
|
21
|
+
subject << [1]
|
22
|
+
}.to raise_error
|
23
|
+
end
|
24
|
+
|
25
|
+
it "shouldn't raise an error if #store_fn has been defined" do
|
26
|
+
subject.store_fn = lambda { |name, text| puts "#{name} - #{text}" }
|
27
|
+
|
28
|
+
expect {
|
29
|
+
subject << [1]
|
30
|
+
}.to output("export-0.csv - value\n1\n").to_stdout
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'csv_files'
|
data/tasks/rspec.rake
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_files
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Damien Brzoska
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: CSV files generator for large datasets
|
56
|
+
email:
|
57
|
+
- damien.brz@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- csv_files.gemspec
|
68
|
+
- lib/csv_files.rb
|
69
|
+
- lib/csv_files/version.rb
|
70
|
+
- spec/csv_files_spec.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- tasks/rspec.rake
|
73
|
+
homepage: ''
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: ''
|
97
|
+
test_files:
|
98
|
+
- spec/csv_files_spec.rb
|
99
|
+
- spec/spec_helper.rb
|