csvbuilder-dynamic-columns-exporter 0.1.0 → 0.1.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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9f1c37c6d462524857d49238849c63136b1b498f9ed759aefa4023a441ebc40
|
|
4
|
+
data.tar.gz: 7d502c386ee64eee159cf191fdedad76f774402e863fd55904e841c15ee125e2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3acac4c4f22ef9f8339f820dcacb2ac7d220e1b6005bd0cff597c9c0254d5016cb2844002f429eb4a509016854caa8fc67f514f469f72a01d2581e7b7483c12
|
|
7
|
+
data.tar.gz: 5763118e03eaecac46af5ee840e02c222b2039ad322a15287e388b42b60370b53cc03f66499928e2c5794735651897f9da7c0951ec750b0dc3e6746c0b0b9925
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.1.1] - 2022-12-30
|
|
4
|
+
|
|
5
|
+
Do not apply double formatting to the cells. The method `unformatted_value` should get the raw values from `source_cells`, not the value already formatted by row_model.format_cell.
|
|
6
|
+
|
|
3
7
|
## [0.1.0] - 2022-12-21
|
|
4
8
|
|
|
5
9
|
- Initial release
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,22 +1,107 @@
|
|
|
1
|
-
# Csvbuilder::Dynamic::Columns
|
|
1
|
+
# Csvbuilder::Dynamic::Columns::Exporter
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Csvbuilder::Dynamic::Columns::Exporter](https://github.com/joel/csvbuilder-dynamic-columns-exporter) is part of the [csvbuilder-collection](https://github.com/joel/csvbuilder)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The Dynamic Columns Exporter contains the implementation for exporting a collection of objects within a variable group as a CSV file.
|
|
6
|
+
|
|
7
|
+
For instance, any object belonging to categories.
|
|
6
8
|
|
|
7
9
|
## Installation
|
|
8
10
|
|
|
9
11
|
Install the gem and add to the application's Gemfile by executing:
|
|
10
12
|
|
|
11
|
-
$ bundle add csvbuilder-dynamic-columns
|
|
13
|
+
$ bundle add csvbuilder-dynamic-columns-exporter
|
|
12
14
|
|
|
13
15
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
16
|
|
|
15
|
-
$ gem install csvbuilder-dynamic-columns
|
|
17
|
+
$ gem install csvbuilder-dynamic-columns-exporter
|
|
16
18
|
|
|
17
19
|
## Usage
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
Let's consider a Developer with languages skill.
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class UserCsvRowModel
|
|
25
|
+
include Csvbuilder::Model
|
|
26
|
+
include Csvbuilder::Export
|
|
27
|
+
|
|
28
|
+
column :name, header: "Developer"
|
|
29
|
+
|
|
30
|
+
dynamic_column :skills
|
|
31
|
+
|
|
32
|
+
def skill(skill_name)
|
|
33
|
+
source_model.skills.where(name: skill_name).exists? ? "1" : "0"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For each Row, a method called by the singular version name of the declared Dynamic Columns will be called to determine the cell's value. In our example: `skill` must be implemented in the Exporter Model.
|
|
39
|
+
|
|
40
|
+
The `source_model` is provided and accessible within this method. In our example, it is the `User`. The File Exporter appends this object to the Row.
|
|
41
|
+
|
|
42
|
+
The dynamic part of the headers must be provided through the context under the same key name as the declared Dynamic Columns, here `skills`.
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
context = { skills: ["Ruby", "Python", "Javascript"] }
|
|
46
|
+
|
|
47
|
+
exporter = Csvbuilder::Export::File.new(UserCsvExportModel, context)
|
|
48
|
+
|
|
49
|
+
exporter.headers
|
|
50
|
+
# => ["Developer", "Ruby", "Python", "Javascript"]
|
|
51
|
+
|
|
52
|
+
sub_context = {} # it merged to the context
|
|
53
|
+
|
|
54
|
+
exporter.generate do |csv|
|
|
55
|
+
User.all.each do |user|
|
|
56
|
+
csv.append_model(user, sub_context) # user here is the source_model.
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
exporter.to_s
|
|
61
|
+
# => "Developer,Ruby,Python,Javascript\Bob,1,0,0\n"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Format Dynamic Columns
|
|
65
|
+
|
|
66
|
+
Instead of taking care of the formatting directly in the dynamic cell method, here `skill`, it can be delegated to `format_dynamic_column_cells.`
|
|
67
|
+
|
|
68
|
+
Consider the following rewrite:
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
class UserCsvRowModel
|
|
72
|
+
include Csvbuilder::Model
|
|
73
|
+
|
|
74
|
+
column :name, header: "Developer"
|
|
75
|
+
|
|
76
|
+
dynamic_column :skills
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
class UserCsvExportModel < UserCsvRowModel
|
|
82
|
+
include Csvbuilder::Export
|
|
83
|
+
|
|
84
|
+
def skill(skill_name)
|
|
85
|
+
source_model.skills.where(name: skill_name).exists?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
class << self
|
|
89
|
+
def format_dynamic_column_cells(cells, _column_name, _context)
|
|
90
|
+
cells.map do |cell|
|
|
91
|
+
case cell
|
|
92
|
+
when true
|
|
93
|
+
"1"
|
|
94
|
+
when false
|
|
95
|
+
"0"
|
|
96
|
+
else
|
|
97
|
+
"N/A"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
end
|
|
104
|
+
```
|
|
20
105
|
|
|
21
106
|
## Development
|
|
22
107
|
|
|
@@ -26,7 +111,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
|
26
111
|
|
|
27
112
|
## Contributing
|
|
28
113
|
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
114
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/joel/csvbuilder-dynamic-columns. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/csvbuilder-dynamic-columns/blob/main/CODE_OF_CONDUCT.md).
|
|
30
115
|
|
|
31
116
|
## License
|
|
32
117
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: csvbuilder-dynamic-columns-exporter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Joel Azemar
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-12-
|
|
11
|
+
date: 2022-12-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -34,44 +34,44 @@ dependencies:
|
|
|
34
34
|
name: csvbuilder-core
|
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
|
-
- - "
|
|
37
|
+
- - "~>"
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0'
|
|
39
|
+
version: '0.1'
|
|
40
40
|
type: :runtime
|
|
41
41
|
prerelease: false
|
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
|
43
43
|
requirements:
|
|
44
|
-
- - "
|
|
44
|
+
- - "~>"
|
|
45
45
|
- !ruby/object:Gem::Version
|
|
46
|
-
version: '0'
|
|
46
|
+
version: '0.1'
|
|
47
47
|
- !ruby/object:Gem::Dependency
|
|
48
48
|
name: csvbuilder-dynamic-columns-core
|
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
|
50
50
|
requirements:
|
|
51
|
-
- - "
|
|
51
|
+
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0'
|
|
53
|
+
version: '0.1'
|
|
54
54
|
type: :runtime
|
|
55
55
|
prerelease: false
|
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
57
|
requirements:
|
|
58
|
-
- - "
|
|
58
|
+
- - "~>"
|
|
59
59
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
60
|
+
version: '0.1'
|
|
61
61
|
- !ruby/object:Gem::Dependency
|
|
62
62
|
name: csvbuilder-exporter
|
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
|
64
64
|
requirements:
|
|
65
|
-
- - "
|
|
65
|
+
- - "~>"
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '0'
|
|
67
|
+
version: '0.1'
|
|
68
68
|
type: :runtime
|
|
69
69
|
prerelease: false
|
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
|
71
71
|
requirements:
|
|
72
|
-
- - "
|
|
72
|
+
- - "~>"
|
|
73
73
|
- !ruby/object:Gem::Version
|
|
74
|
-
version: '0'
|
|
74
|
+
version: '0.1'
|
|
75
75
|
description: Help handle CSVs in a more efficient way
|
|
76
76
|
email:
|
|
77
77
|
- joel.azemar@gmail.com
|