acts_as_table_csv 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +1 -0
- data/LICENSE +30 -0
- data/README.md +76 -0
- data/Rakefile +13 -0
- data/lib/acts_as_table/csv.rb +38 -0
- data/lib/acts_as_table/csv/reader.rb +48 -0
- data/lib/acts_as_table/csv/writer.rb +38 -0
- data/lib/acts_as_table_csv.rb +17 -0
- data/lib/acts_as_table_csv/engine.rb +13 -0
- data/lib/acts_as_table_csv/version.rb +4 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39b160ffc4a93b79254c6b33905013a8654334773ef0a44bee15fd111d3e62d6
|
4
|
+
data.tar.gz: de211a8b138223ba2c927c3d7b88846465a521022c501498e67f962ba0d58a5b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9a058e1673e8d4c8358969a979864cd58ab20612e2ab3aab7cd8d02560092a1a937ec034c5ce5d7431d7b0cdf7febe0d154b2f08d0530b311206fae2d5ddf32
|
7
|
+
data.tar.gz: a63100f72aa8eff2b8879cf7baeb9ee956f0e7fde352cfa798c0623b456f819225c6f6de780288dde7e18ba3754483ccbfad10f36296dce6ab634957b54d45ef
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --markup-provider redcarpet
|
data/LICENSE
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
Copyright (c) 2020, Battelle Memorial Institute
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
1. Battelle Memorial Institute (hereinafter Battelle) hereby grants permission
|
5
|
+
to any person or entity lawfully obtaining a copy of this software and
|
6
|
+
associated documentation files (hereinafter "the Software") to redistribute
|
7
|
+
and use the Software in source and binary forms, with or without
|
8
|
+
modification. Such person or entity may use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and may permit
|
10
|
+
others to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
* Redistributions of source code must retain the above copyright notice, this
|
13
|
+
list of conditions and the following disclaimers.
|
14
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
15
|
+
this list of conditions and the following disclaimer in the documentation
|
16
|
+
and/or other materials provided with the distribution.
|
17
|
+
* Other than as used herein, neither the name Battelle Memorial Institute or
|
18
|
+
Battelle may be used in any form whatsoever without the express written
|
19
|
+
consent of Battelle.
|
20
|
+
|
21
|
+
2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
24
|
+
DISCLAIMED. IN NO EVENT SHALL BATTELLE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
25
|
+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
26
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
28
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
29
|
+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
30
|
+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# CSV Support for ActsAsTable
|
2
|
+
|
3
|
+
This is an [ActsAsTable](https://github.com/pnnl/acts_as_table) extension that adds support for parsing/serializing [Comma-Separated Values](https://tools.ietf.org/html/rfc4180) (CSV) data.
|
4
|
+
|
5
|
+
* https://github.com/pnnl/acts_as_table_csv
|
6
|
+
|
7
|
+
## Documentation
|
8
|
+
|
9
|
+
* {ActsAsTable::CSV}
|
10
|
+
* {ActsAsTable::CSV::Reader}
|
11
|
+
* {ActsAsTable::CSV::Writer}
|
12
|
+
|
13
|
+
## Dependencies
|
14
|
+
|
15
|
+
* [ActsAsTable](https://github.com/pnnl/acts_as_table) (~> 0.0.1)
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
The recommended installation method is via [RubyGems](http://rubygems.org/).
|
20
|
+
To install the latest, official release of the `ActsAsTable::CSV` gem, do:
|
21
|
+
```bash
|
22
|
+
% [sudo] gem install acts_as_table_csv
|
23
|
+
```
|
24
|
+
|
25
|
+
## Examples
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'acts_as_table'
|
29
|
+
```
|
30
|
+
|
31
|
+
### Writing CSV data
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# @return [ActsAsTable::RowModel]
|
35
|
+
@row_model = ...
|
36
|
+
|
37
|
+
# @return [Array<ActiveRecord::Base>]
|
38
|
+
@bases = ...
|
39
|
+
|
40
|
+
ActsAsTable.for(:csv).writer(@row_model, $stdout) do |writer|
|
41
|
+
@bases.each do |base|
|
42
|
+
writer << base
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
### Reading CSV data
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# @return [ActsAsTable::RowModel]
|
51
|
+
@row_model = ...
|
52
|
+
|
53
|
+
# @return [ActsAsTable::Table]
|
54
|
+
@table = @row_model.tables.new
|
55
|
+
|
56
|
+
ActsAsTable.for(:csv).reader(@row_model, $stdin) do |reader|
|
57
|
+
reader.each_row do |row|
|
58
|
+
# @return [Array<ActsAsTable::Record>]
|
59
|
+
records = @table.from_row(row)
|
60
|
+
|
61
|
+
records.each do |record|
|
62
|
+
record.position = reader.lineno
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## Author
|
69
|
+
|
70
|
+
* [Mark Borkum](https://github.com/markborkum)
|
71
|
+
|
72
|
+
## License
|
73
|
+
|
74
|
+
This software is licensed under a 3-clause BSD license.
|
75
|
+
|
76
|
+
For more information, see the accompanying {file:LICENSE} file.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
|
9
|
+
require 'yard'
|
10
|
+
|
11
|
+
YARD::Rake::YardocTask.new do |t|
|
12
|
+
t.files = ['lib/**/*.rb']
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'active_support/core_ext/module'
|
2
|
+
|
3
|
+
module ActsAsTable
|
4
|
+
# ActsAsTable serialization format module for comma-separated values (CSV) format.
|
5
|
+
module CSV
|
6
|
+
extend ::ActiveSupport::Autoload
|
7
|
+
|
8
|
+
autoload :Reader, 'acts_as_table/csv/reader'
|
9
|
+
autoload :Writer, 'acts_as_table/csv/writer'
|
10
|
+
|
11
|
+
# Returns the symbolic name for this ActsAsTable serialization format.
|
12
|
+
#
|
13
|
+
# @return [Symbol]
|
14
|
+
def self.format
|
15
|
+
:csv
|
16
|
+
end
|
17
|
+
|
18
|
+
# Returns a new ActsAsTable reader object for this serialization format.
|
19
|
+
#
|
20
|
+
# @param [Array<Object>] args
|
21
|
+
# @yieldparam [ActsAsTable::CSV::Reader] reader
|
22
|
+
# @yieldreturn [void]
|
23
|
+
# @return [ActsAsTable::CSV::Reader]
|
24
|
+
def self.reader(*args, &block)
|
25
|
+
Reader.new(*args, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns a new ActsAsTable writer object for this serialization format.
|
29
|
+
#
|
30
|
+
# @param [Array<Object>] args
|
31
|
+
# @yieldparam [ActsAsTable::CSV::Writer] writer
|
32
|
+
# @yieldreturn [void]
|
33
|
+
# @return [ActsAsTable::CSV::Writer]
|
34
|
+
def self.writer(*args, &block)
|
35
|
+
Writer.new(*args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module ActsAsTable::CSV
|
4
|
+
# ActsAsTable reader object for comma-separated values (CSV) format.
|
5
|
+
class Reader < ActsAsTable::Reader
|
6
|
+
# Returns a new ActsAsTable reader object for comma-separated values (CSV) format using the given ActsAsTable row model, input stream and options.
|
7
|
+
#
|
8
|
+
# @param [ActsAsTable::RowModel] row_model
|
9
|
+
# @param [IO] input
|
10
|
+
# @param [Hash<Symbol, Object>] options
|
11
|
+
# @yieldparam [ActsAsTable::CSV::Reader] reader
|
12
|
+
# @yieldreturn [void]
|
13
|
+
# @return [ActsAsTable::CSV::Reader]
|
14
|
+
#
|
15
|
+
# @note Delegates input stream and options to constructor for +CSV+ object.
|
16
|
+
# @see ::CSV.new
|
17
|
+
def initialize(row_model, input = $stdin, **options, &block)
|
18
|
+
# @return [Hash<Symbol, Object>]
|
19
|
+
csv_options = (options[:csv] || {}).merge({
|
20
|
+
headers: false,
|
21
|
+
})
|
22
|
+
|
23
|
+
@csv = ::CSV.new(input, **csv_options)
|
24
|
+
|
25
|
+
super(row_model, input, **options, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns a pair, where the first element is the next row or +nil+ if input stream is at end of file and the second element indicates if input stream is at end of file.
|
29
|
+
#
|
30
|
+
# @return [Object]
|
31
|
+
def read_row
|
32
|
+
# @return [Array<#to_s>, nil]
|
33
|
+
row = @csv.shift
|
34
|
+
|
35
|
+
if row.nil?
|
36
|
+
[row, true]
|
37
|
+
else
|
38
|
+
row = row.collect(&:to_s).collect(&:strip).collect { |s| s.empty? ? nil : s }
|
39
|
+
|
40
|
+
if row.all?(&:nil?)
|
41
|
+
row = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
[row, false]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module ActsAsTable::CSV
|
4
|
+
# ActsAsTable writer object for comma-separated values (CSV) format.
|
5
|
+
class Writer < ActsAsTable::Writer
|
6
|
+
# Returns a new ActsAsTable writer object for comma-separated values (CSV) format using the given ActsAsTable row model, output stream and options.
|
7
|
+
#
|
8
|
+
# @param [ActsAsTable::RowModel] row_model
|
9
|
+
# @param [IO] output
|
10
|
+
# @param [Hash<Symbol, Object>] options
|
11
|
+
# @yieldparam [ActsAsTable::CSV::Writer] writer
|
12
|
+
# @yieldreturn [void]
|
13
|
+
# @return [ActsAsTable::CSV::Writer]
|
14
|
+
#
|
15
|
+
# @note Delegates output stream and options to constructor for +CSV+ object.
|
16
|
+
# @see ::CSV.new
|
17
|
+
def initialize(row_model, output = $stdout, **options, &block)
|
18
|
+
# @return [Hash<Symbol, Object>]
|
19
|
+
csv_options = (options[:csv] || {}).merge({
|
20
|
+
headers: false,
|
21
|
+
})
|
22
|
+
|
23
|
+
@csv = ::CSV.new(output, **csv_options)
|
24
|
+
|
25
|
+
super(row_model, output, **options, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Writes a row to the output stream.
|
29
|
+
#
|
30
|
+
# @param [Array<String, nil>, nil] row
|
31
|
+
# @return [ActsAsTable::Writer]
|
32
|
+
def write_row(row)
|
33
|
+
@csv << row
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support/core_ext/module'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'rails/engine'
|
5
|
+
require 'acts_as_table_csv/engine'
|
6
|
+
rescue ::LoadError
|
7
|
+
# void
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'acts_as_table_csv/version'
|
11
|
+
|
12
|
+
# ActsAsTable extension for parsing/serializing CSV data.
|
13
|
+
module ActsAsTableCsv
|
14
|
+
extend ::ActiveSupport::Autoload
|
15
|
+
|
16
|
+
autoload :VERSION
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ActsAsTableCsv
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace ActsAsTableCsv
|
4
|
+
|
5
|
+
initializer 'acts_as_table_csv.append_format' do |app|
|
6
|
+
ActsAsTable.configure do
|
7
|
+
require 'acts_as_table/csv'
|
8
|
+
|
9
|
+
config.formats << :CSV
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_table_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Borkum
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: acts_as_table
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.0.1
|
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: redcarpet
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ActsAsTable extension for parsing/serializing CSV data.
|
70
|
+
email:
|
71
|
+
- mark.borkum@pnnl.gov
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".yardopts"
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/acts_as_table/csv.rb
|
81
|
+
- lib/acts_as_table/csv/reader.rb
|
82
|
+
- lib/acts_as_table/csv/writer.rb
|
83
|
+
- lib/acts_as_table_csv.rb
|
84
|
+
- lib/acts_as_table_csv/engine.rb
|
85
|
+
- lib/acts_as_table_csv/version.rb
|
86
|
+
homepage: https://github.com/pnnl/acts_as_table_csv
|
87
|
+
licenses:
|
88
|
+
- BSD-3-Clause
|
89
|
+
metadata:
|
90
|
+
bug_tracker_uri: https://github.com/pnnl/acts_as_table_csv/issues
|
91
|
+
source_code_uri: https://github.com/pnnl/acts_as_table_csv
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.0.8
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: CSV support for ActsAsTable.
|
111
|
+
test_files: []
|