csv_composer 1.0.2
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 +54 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/LICENSE +22 -0
- data/README.md +80 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/csv_composer.gemspec +36 -0
- data/lib/csv_composer.rb +16 -0
- data/lib/csv_composer/base.rb +34 -0
- data/lib/csv_composer/file_exporters/base.rb +7 -0
- data/lib/csv_composer/file_exporters/string_io.rb +12 -0
- data/lib/csv_composer/generic.rb +35 -0
- data/lib/csv_composer/header_processors/base.rb +7 -0
- data/lib/csv_composer/header_processors/generic.rb +11 -0
- data/lib/csv_composer/item_processors/base.rb +7 -0
- data/lib/csv_composer/item_processors/generic.rb +11 -0
- data/lib/csv_composer/version.rb +3 -0
- data/lib/csv_composer/writer.rb +25 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6220ec5129f3168f1a59775a32bfdc9139c9f8ea
|
4
|
+
data.tar.gz: eb76a39ae13c4c52d0b4fa43e85aaaea641cc796
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86bfa69b651a4d6f800ca5d1296c4b67e2ee97a3f24c9fb4d1c6fcce872b1230e54db2eb044491e6d9746e0523cd82f64d23ff60bd230e733ad3d87bb3d0bf19
|
7
|
+
data.tar.gz: 0f7af2630ddbf78a7d7a100c67f9f8c0f0017cb7ce58ac8bdacb33e50a8a2f26dccdb13a7d370753ef6f25322a48954d8de49a574e279583001695d8bda90127
|
data/.gitignore
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
51
|
+
|
52
|
+
*.iml
|
53
|
+
.idea/
|
54
|
+
.travis.yml
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
csv_composer (1.0.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.10.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
bundler (~> 1.14)
|
17
|
+
csv_composer!
|
18
|
+
minitest (~> 5.0)
|
19
|
+
rake (~> 10.0)
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.16.0
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
MIT License
|
3
|
+
|
4
|
+
Copyright (c) 2018 Twist Bioscience
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# CsvComposer
|
2
|
+
|
3
|
+
Csv composer is a flexible and generic csv writer and exporter. Allows you to easily control whatever you
|
4
|
+
want to do in each step of csv making, such as how to:
|
5
|
+
- iterate over the collection
|
6
|
+
- generate file header
|
7
|
+
- generate rows
|
8
|
+
- export final output
|
9
|
+
|
10
|
+
Easy to set up, just extend composer entities to create your own custom csv's or use the generic one.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'csv_composer'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install csv_composer
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Basic usage:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
collection = [
|
34
|
+
{ id: 'element1', value: 'value1'},
|
35
|
+
{ id: 'element2', value: 'value2'},
|
36
|
+
{ id: 'element3', value: 'value3'}
|
37
|
+
]
|
38
|
+
|
39
|
+
exporter = CsvComposer::Generic.new
|
40
|
+
exporter.compose(collection)
|
41
|
+
```
|
42
|
+
|
43
|
+
Where collection could be everything that includes Enumerable module (and implements each method).
|
44
|
+
This way you are able to implement your custom iterators.
|
45
|
+
|
46
|
+
## Custom csvs exporters
|
47
|
+
You can create your own custom exporters just extending CsvComposer::Base.
|
48
|
+
|
49
|
+
Override your own custom 'columns_mapping' to map every kind of object (in the collection) to csv value using lambdas:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
def generic_lambda
|
53
|
+
->(item, key) { item[key] || '' }
|
54
|
+
end
|
55
|
+
|
56
|
+
def columns_mapping
|
57
|
+
[
|
58
|
+
{ id: 'Id', key: :id, lambda: generic_lambda },
|
59
|
+
{ id: 'Value', key: :value, lambda: generic_lambda }
|
60
|
+
]
|
61
|
+
end
|
62
|
+
```
|
63
|
+
Create and set your own custom Header processors extending 'CsvComposer::HeaderProcessors::Base' and your own custom
|
64
|
+
ItemProcessors 'extending CsvComposer::ItemProcessors::Base'
|
65
|
+
|
66
|
+
Finally create and set your own custom file exporters extending 'CsvComposer::FileExporters::Base' to
|
67
|
+
export csv to wherever location you want such as a file, upload it to the cloud, etc.
|
68
|
+
|
69
|
+
Optionally you can send a second parameter to 'compose' method with a hash containing custom options that will be sent into
|
70
|
+
both header and item processor that you can use when overriding them.
|
71
|
+
|
72
|
+
## Development
|
73
|
+
|
74
|
+
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.
|
75
|
+
|
76
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Twistbioscience/csv_composer
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "csv_composer"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "csv_composer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv_composer"
|
8
|
+
spec.version = CsvComposer::VERSION
|
9
|
+
spec.authors = ["Jorge Smulevici"]
|
10
|
+
spec.email = ["jsmulevici@twistbioscience.com", "open-source@twistbioscience.com"]
|
11
|
+
spec.licenses = ['MIT']
|
12
|
+
|
13
|
+
spec.summary = %q{csv exporter}
|
14
|
+
spec.description = %q{Easy and generic csv writer and exporter}
|
15
|
+
spec.homepage = "https://github.com/Twistbioscience/csv_composer"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
36
|
+
end
|
data/lib/csv_composer.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "csv_composer/version"
|
2
|
+
|
3
|
+
module CsvComposer
|
4
|
+
require 'csv_composer/base'
|
5
|
+
require 'csv_composer/generic'
|
6
|
+
require 'csv_composer/writer'
|
7
|
+
|
8
|
+
require 'csv_composer/header_processors/base'
|
9
|
+
require 'csv_composer/header_processors/generic'
|
10
|
+
|
11
|
+
require 'csv_composer/item_processors/base'
|
12
|
+
require 'csv_composer/item_processors/generic'
|
13
|
+
|
14
|
+
require 'csv_composer/file_exporters/base'
|
15
|
+
require 'csv_composer/file_exporters/string_io'
|
16
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CsvComposer
|
2
|
+
class Base
|
3
|
+
|
4
|
+
def compose(items, opts = {})
|
5
|
+
content = write(items, opts)
|
6
|
+
export(content, opts)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def header_processor
|
12
|
+
end
|
13
|
+
|
14
|
+
def item_processor
|
15
|
+
end
|
16
|
+
|
17
|
+
def exporter
|
18
|
+
end
|
19
|
+
|
20
|
+
def columns_mapping
|
21
|
+
# Create a mapping for each column in the export CSV
|
22
|
+
end
|
23
|
+
|
24
|
+
def write(collection, writing_options = {})
|
25
|
+
writer = Writer.new
|
26
|
+
writer.write(header_processor.new, item_processor.new, collection, columns_mapping, writing_options)
|
27
|
+
end
|
28
|
+
|
29
|
+
def export(content, opts = {})
|
30
|
+
exporter.new.export(content, opts)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module CsvComposer::FileExporters
|
2
|
+
class StringIo < Base
|
3
|
+
|
4
|
+
def export(content, opts = { })
|
5
|
+
file = StringIO.open(content)
|
6
|
+
file.class.class_eval { attr_accessor :original_filename }
|
7
|
+
file.original_filename = opts[:filename]
|
8
|
+
file
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module CsvComposer
|
2
|
+
class Generic < Base
|
3
|
+
protected
|
4
|
+
|
5
|
+
def header_processor
|
6
|
+
HeaderProcessors::Generic
|
7
|
+
end
|
8
|
+
|
9
|
+
def item_processor
|
10
|
+
ItemProcessors::Generic
|
11
|
+
end
|
12
|
+
|
13
|
+
def hash_key_lambda
|
14
|
+
->(item, key) { item[key] || '' }
|
15
|
+
end
|
16
|
+
|
17
|
+
def public_method_lambda
|
18
|
+
lambda do |item, method_name|
|
19
|
+
item.public_send(method_name)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def columns_mapping
|
24
|
+
[
|
25
|
+
{ id: 'Id', key: :id, lambda: hash_key_lambda },
|
26
|
+
{ id: 'Value', key: :value, lambda: hash_key_lambda }
|
27
|
+
]
|
28
|
+
end
|
29
|
+
|
30
|
+
def exporter
|
31
|
+
FileExporters::StringIo
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module CsvComposer
|
4
|
+
class Writer
|
5
|
+
|
6
|
+
def write(header_processor, item_processor, collection, columns_mapping, opts = {})
|
7
|
+
CSV.generate do |csv|
|
8
|
+
headers = header_processor.process(columns_mapping, opts)
|
9
|
+
csv << headers.compact
|
10
|
+
opts[:headers] = headers # We are adding the headers in case you need them to process your items
|
11
|
+
write_content(csv, item_processor, columns_mapping, collection, opts)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def write_content(csv, item_processor, columns_mapping, collection, opts)
|
18
|
+
collection.each do |item|
|
19
|
+
row_components = item_processor.process_value(item, columns_mapping, opts)
|
20
|
+
csv << row_components.compact
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_composer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jorge Smulevici
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-12 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.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Easy and generic csv writer and exporter
|
56
|
+
email:
|
57
|
+
- jsmulevici@twistbioscience.com
|
58
|
+
- open-source@twistbioscience.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- csv_composer.gemspec
|
72
|
+
- lib/csv_composer.rb
|
73
|
+
- lib/csv_composer/base.rb
|
74
|
+
- lib/csv_composer/file_exporters/base.rb
|
75
|
+
- lib/csv_composer/file_exporters/string_io.rb
|
76
|
+
- lib/csv_composer/generic.rb
|
77
|
+
- lib/csv_composer/header_processors/base.rb
|
78
|
+
- lib/csv_composer/header_processors/generic.rb
|
79
|
+
- lib/csv_composer/item_processors/base.rb
|
80
|
+
- lib/csv_composer/item_processors/generic.rb
|
81
|
+
- lib/csv_composer/version.rb
|
82
|
+
- lib/csv_composer/writer.rb
|
83
|
+
homepage: https://github.com/Twistbioscience/csv_composer
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.6.12
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: csv exporter
|
107
|
+
test_files: []
|