csv_serializer 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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +90 -0
- data/Rakefile +13 -0
- data/lib/csv_serializer.rb +15 -0
- data/lib/csv_serializer/definition.rb +39 -0
- data/lib/csv_serializer/definition/all_column.rb +21 -0
- data/lib/csv_serializer/definition/function_array.rb +17 -0
- data/lib/csv_serializer/definition/function_hash.rb +15 -0
- data/lib/csv_serializer/definition/symbol_array.rb +21 -0
- data/lib/csv_serializer/methods.rb +22 -0
- data/lib/csv_serializer/railtie.rb +4 -0
- data/lib/csv_serializer/serializer.rb +30 -0
- data/lib/csv_serializer/version.rb +3 -0
- data/lib/tasks/csv_serializer_tasks.rake +4 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d8bba76706d52efc7cce1caa94b6553eda5a272c6573c74c629b5d4f20b0fb07
|
4
|
+
data.tar.gz: 5b7285367e093d88dc1a6fc93dfc10ddf157b47c8747412db481a06d28558b1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1402ccb3fb4b465bd71d24b415e2c36e7810633e9130ff89c55a569e2d3bd21101cb1a843679a836f085661be3c6a8e091c0db925095bbad9205623354c7724e
|
7
|
+
data.tar.gz: 60a3dceccb1c5442683c754d355cba4d57cfab597181d9cc2b505cf160294b7f60e9e05a112c1cb8fcb0bb69b1af073a637f173fe62f050b73438b7e7412a6df
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2021 Yudai Tanaka
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# CsvSerializer
|
2
|
+
Adds easy CSV generation functionality to `ActiveRecord`.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
Suppose Person model is defined.
|
6
|
+
```ruby
|
7
|
+
class Person < ApplicationRecord
|
8
|
+
# In schema.rb, columns are defined as below definition.
|
9
|
+
# create_table "people", force: :cascade do |t|
|
10
|
+
# t.string "name"
|
11
|
+
# t.integer "age"
|
12
|
+
# t.integer "tall"
|
13
|
+
# t.integer "weight"
|
14
|
+
# t.datetime "created_at", precision: 6, null: false
|
15
|
+
# t.datetime "updated_at", precision: 6, null: false
|
16
|
+
# end
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
`to_csv` returns csv as string.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
Person.to_csv
|
24
|
+
# => id,name,age,tall,weight,created_at,updated_at
|
25
|
+
# 1,sample1,1,128,34,2020-01-01 10:02:39 UTC,2020-01-01 11:02:39 UTC
|
26
|
+
# 2,sample2,2,130,32,2020-01-01 12:02:39 UTC,2020-01-01 14:02:39 UTC
|
27
|
+
````
|
28
|
+
|
29
|
+
If attribute names is passed, CSV string only contains the specified attributes will be returned.
|
30
|
+
```ruby
|
31
|
+
Person.to_csv(:id, :name)
|
32
|
+
# => id,name
|
33
|
+
# 1,sample1
|
34
|
+
# 2,sample2
|
35
|
+
```
|
36
|
+
|
37
|
+
Containing the value gained by processing the record is supported.
|
38
|
+
```ruby
|
39
|
+
Person.to_csv(
|
40
|
+
"long name": ->(user) { user.name * 2 },
|
41
|
+
"short name": ->(user) { user.name[-2..] }
|
42
|
+
)
|
43
|
+
# => long name,short name
|
44
|
+
# sample1sample1,e1
|
45
|
+
# sample2sample2,e2
|
46
|
+
```
|
47
|
+
|
48
|
+
Same named columns can be specified with arrays of a column name and a producer function.
|
49
|
+
```ruby
|
50
|
+
Person.all.to_csv(
|
51
|
+
["long name", ->(user) { user.name * 2 }],
|
52
|
+
["long name", ->(user) { user.name * 3 }]
|
53
|
+
)
|
54
|
+
# => Long name,Long name
|
55
|
+
# sample1sample1,sample1sample1sample1
|
56
|
+
```
|
57
|
+
|
58
|
+
### Output to files
|
59
|
+
```ruby
|
60
|
+
io = Tempfile.new
|
61
|
+
Person.all.to_csv_stream(io)
|
62
|
+
# Below lines are written to tempfile.
|
63
|
+
# id,name,age,tall,weight,created_at,updated_at
|
64
|
+
# 1,sample1,1,128,34,2020-01-01 10:02:39 UTC,2020-01-01 11:02:39 UTC
|
65
|
+
# 2,sample2,2,130,32,2020-01-01 12:02:39 UTC,2020-01-01 14:02:39 UTC
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
## Installation
|
70
|
+
Add this line to your application's Gemfile:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
gem 'csv_serializer'
|
74
|
+
```
|
75
|
+
|
76
|
+
And then execute:
|
77
|
+
```bash
|
78
|
+
$ bundle
|
79
|
+
```
|
80
|
+
|
81
|
+
Or install it yourself as:
|
82
|
+
```bash
|
83
|
+
$ gem install csv_serializer
|
84
|
+
```
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
Contribution directions go here.
|
88
|
+
|
89
|
+
## License
|
90
|
+
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,15 @@
|
|
1
|
+
require "csv_serializer/version"
|
2
|
+
require "csv_serializer/railtie"
|
3
|
+
require "csv_serializer/methods"
|
4
|
+
require "csv_serializer/serializer"
|
5
|
+
require "csv_serializer/definition"
|
6
|
+
require "csv_serializer/definition/all_column"
|
7
|
+
require "csv_serializer/definition/function_array"
|
8
|
+
require "csv_serializer/definition/symbol_array"
|
9
|
+
require "csv_serializer/definition/function_hash"
|
10
|
+
|
11
|
+
module CsvSerializer; end
|
12
|
+
|
13
|
+
ActiveSupport.on_load(:active_record) do
|
14
|
+
include CsvSerializer::Methods
|
15
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CsvSerializer
|
2
|
+
# Represents definition of csv columns.
|
3
|
+
class Definition
|
4
|
+
attr_reader :records
|
5
|
+
|
6
|
+
def self.build(array, hash, records)
|
7
|
+
if array.blank? && hash.blank?
|
8
|
+
AllColumn.new(records)
|
9
|
+
elsif array.blank?
|
10
|
+
FunctionHash.new(records, hash)
|
11
|
+
elsif array.all?(Symbol)
|
12
|
+
SymbolArray.new(records, array)
|
13
|
+
else
|
14
|
+
FunctionArray.new(records, array)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(records, array_or_hash = nil)
|
19
|
+
@records = records
|
20
|
+
@array_or_hash = array_or_hash
|
21
|
+
end
|
22
|
+
|
23
|
+
def definitions
|
24
|
+
@array_or_hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def serializer
|
28
|
+
Serializer.new(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def target_records
|
32
|
+
records.all
|
33
|
+
end
|
34
|
+
|
35
|
+
def header
|
36
|
+
column_names.map { records.human_attribute_name(_1) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CsvSerializer
|
2
|
+
class Definition
|
3
|
+
class AllColumn < CsvSerializer::Definition
|
4
|
+
def column_names
|
5
|
+
records.attribute_names
|
6
|
+
end
|
7
|
+
|
8
|
+
def target_records
|
9
|
+
records.pluck(*column_names)
|
10
|
+
end
|
11
|
+
|
12
|
+
def producers
|
13
|
+
definitions.map(&:last)
|
14
|
+
end
|
15
|
+
|
16
|
+
def process(record)
|
17
|
+
record
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module CsvSerializer
|
2
|
+
class Definition::FunctionArray < CsvSerializer::Definition
|
3
|
+
def column_names
|
4
|
+
definitions.map(&:first)
|
5
|
+
end
|
6
|
+
|
7
|
+
def process(record)
|
8
|
+
producers.map do |func|
|
9
|
+
func.call record
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def producers
|
14
|
+
definitions.map(&:last)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CsvSerializer::Definition::FunctionHash < CsvSerializer::Definition
|
2
|
+
def column_names
|
3
|
+
definitions.keys
|
4
|
+
end
|
5
|
+
|
6
|
+
def process(record)
|
7
|
+
producers.map do |func|
|
8
|
+
func.call record
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def producers
|
13
|
+
definitions.values
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class CsvSerializer::Definition::SymbolArray < CsvSerializer::Definition
|
2
|
+
def column_names
|
3
|
+
definitions
|
4
|
+
end
|
5
|
+
|
6
|
+
def process(record)
|
7
|
+
if definitions.size <= 1
|
8
|
+
[record]
|
9
|
+
else
|
10
|
+
record
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def producers
|
15
|
+
definitions
|
16
|
+
end
|
17
|
+
|
18
|
+
def target_records
|
19
|
+
records.pluck(*column_names)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CsvSerializer
|
4
|
+
# Provides methods available on ActiveRecords.
|
5
|
+
module Methods
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
def to_csv(*array, **hash)
|
10
|
+
serializer = Definition.build(array, hash, self).serializer
|
11
|
+
serializer.serialize
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_csv_stream(io, *array, **hash)
|
15
|
+
serializer = Definition.build(array, hash, self).serializer
|
16
|
+
Thread.new do
|
17
|
+
serializer.serialize_to(io)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "csv"
|
3
|
+
|
4
|
+
module CsvSerializer
|
5
|
+
# Provides functions to generate csv string and file from column definition object.
|
6
|
+
class Serializer
|
7
|
+
attr_reader :definitions, :records
|
8
|
+
|
9
|
+
def initialize(definitions)
|
10
|
+
@definitions = definitions
|
11
|
+
end
|
12
|
+
|
13
|
+
def serialize
|
14
|
+
CSV.generate do |csv|
|
15
|
+
csv << definitions.header
|
16
|
+
definitions.target_records.each do |record|
|
17
|
+
csv << definitions.process(record)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def serialize_to(io)
|
23
|
+
io << CSV.generate_line(definitions.header)
|
24
|
+
definitions.target_records.each do |record|
|
25
|
+
io << CSV.generate_line(definitions.process(record))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yudai Tanaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.1.1
|
27
|
+
description: Description of CsvSerializer.
|
28
|
+
email:
|
29
|
+
- ytnk531@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/csv_serializer.rb
|
38
|
+
- lib/csv_serializer/definition.rb
|
39
|
+
- lib/csv_serializer/definition/all_column.rb
|
40
|
+
- lib/csv_serializer/definition/function_array.rb
|
41
|
+
- lib/csv_serializer/definition/function_hash.rb
|
42
|
+
- lib/csv_serializer/definition/symbol_array.rb
|
43
|
+
- lib/csv_serializer/methods.rb
|
44
|
+
- lib/csv_serializer/railtie.rb
|
45
|
+
- lib/csv_serializer/serializer.rb
|
46
|
+
- lib/csv_serializer/version.rb
|
47
|
+
- lib/tasks/csv_serializer_tasks.rake
|
48
|
+
homepage: https://ytana.work/
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://ytana.work/
|
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: '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.2.3
|
69
|
+
signing_key:
|
70
|
+
specification_version: 4
|
71
|
+
summary: Summary of CsvSerializer.
|
72
|
+
test_files: []
|