csvbuilder-dynamic-columns-exporter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d6adf64db2e5a0d9c8dc5fb555489ff195bd00c3cebba5736a0ea6fcfb37b11
4
- data.tar.gz: f084c0dc4f1b0df1547e47d7ff285ab7ab597e6f4838d0a926645e09a9a8da61
3
+ metadata.gz: a9f1c37c6d462524857d49238849c63136b1b498f9ed759aefa4023a441ebc40
4
+ data.tar.gz: 7d502c386ee64eee159cf191fdedad76f774402e863fd55904e841c15ee125e2
5
5
  SHA512:
6
- metadata.gz: 32a3f92953ef63f35d3b30f88d64c72dfba217077e6fb5ffd51ef9d32695098aeb42925acb0fa7e3bbd8336d4691b15d10113a751a8a213d1aa948a8c9823806
7
- data.tar.gz: 2349dcb6730b77a227f3e6340ffb7b2e753bb2f7d880d8f4fb2b3ad678d69bf1e402c0b059f56e805d3ddc353b38a88430b5c76a0c912404b8507c5b68694970
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
@@ -14,3 +14,5 @@ gem "rubocop"
14
14
  gem "rubocop-performance"
15
15
  gem "rubocop-rake"
16
16
  gem "rubocop-rspec"
17
+
18
+ # gem "csvbuilder-dynamic-columns-core", path: "../csvbuilder-dynamic-columns-core"
data/README.md CHANGED
@@ -1,22 +1,107 @@
1
- # Csvbuilder::Dynamic::Columns
1
+ # Csvbuilder::Dynamic::Columns::Exporter
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csvbuilder/dynamic/columns`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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/[USERNAME]/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).
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
 
@@ -6,7 +6,7 @@ module Csvbuilder
6
6
  module Export
7
7
  class DynamicColumnAttribute < Csvbuilder::DynamicColumnAttributeBase
8
8
  def unformatted_value
9
- formatted_cells
9
+ source_cells
10
10
  end
11
11
 
12
12
  def source_cells
@@ -4,7 +4,7 @@ module Csvbuilder
4
4
  module Dynamic
5
5
  module Columns
6
6
  module Exporter
7
- VERSION = "0.1.0"
7
+ VERSION = "0.1.1"
8
8
  end
9
9
  end
10
10
  end
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.0
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-23 00:00:00.000000000 Z
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