csvbuilder-dynamic-columns-core 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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +100 -7
- data/lib/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base.rb +9 -2
- data/lib/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared.rb +15 -1
- data/lib/csvbuilder/dynamic/columns/core/internal/dynamic_column_attribute_base.rb +13 -1
- data/lib/csvbuilder/dynamic/columns/core/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5fdfc267670291ff9caefe6dfee13d22561acaac5286f31eee8124f980b32936
|
4
|
+
data.tar.gz: e1308d57584b7bfc37ab11e125e0cc3f4bc4bab28a9da0eac62ba4f85c7a2642
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bff40a5481025d39c46822f193b49e2df35ad946c6c60cb1c8f366767d4e1915cddb92f82962af36a4c832c99c48e38eb8c4756edf1594d4f537ca4d312b1078
|
7
|
+
data.tar.gz: 50346320272ecf16a63b4736a3736b9c9c5621a6a403c720e29ea284c3431be07389ef67dcbaa0cd6d05e6eb2ecd8f71713d51a47f67b82390d0dd6e210c4d86
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,22 +1,115 @@
|
|
1
|
-
# Csvbuilder::Dynamic::Columns
|
1
|
+
# Csvbuilder::Dynamic::Columns::Core
|
2
2
|
|
3
|
-
|
3
|
+
[Csvbuilder::Dynamic::Columns::Core](https://github.com/joel/csvbuilder-dynamic-columns-core) is part of the [csvbuilder-collection](https://github.com/joel/csvbuilder)
|
4
4
|
|
5
|
-
|
5
|
+
The Dynamic Columns Core contains the shared components used and extended by the Dynamic Columns Exporter and the Dynamic Columns Importer. It carries the architecture for the Dynamic Columns feature, and it can’t be used alone.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Install the gem and add to the application's Gemfile by executing:
|
10
10
|
|
11
|
-
$ bundle add csvbuilder-dynamic-columns
|
11
|
+
$ bundle add csvbuilder-dynamic-columns-core
|
12
12
|
|
13
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
14
|
|
15
|
-
$ gem install csvbuilder-dynamic-columns
|
15
|
+
$ gem install csvbuilder-dynamic-columns-core
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
19
|
-
|
19
|
+
The Dynamic Columns Core adds a new entry into the
|
20
|
+
DSL of the Model:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class UserCsvRowModel
|
24
|
+
include Csvbuilder::Model
|
25
|
+
|
26
|
+
column :first_name
|
27
|
+
column :last_name
|
28
|
+
|
29
|
+
dynamic_column :skills
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Here we indicate we will have length variable headers about skills in the CSV file. Important notes, only one dynamic column can be defined per model, and it must be placed after the regular headers!
|
34
|
+
|
35
|
+
The Dynamic Columns add two new methods to the Model:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class BasicRowModel
|
39
|
+
include Csvbuilder::Model
|
40
|
+
|
41
|
+
class << self
|
42
|
+
|
43
|
+
# Safe to override. Method applied to each dynamic_column attribute
|
44
|
+
#
|
45
|
+
# @param cells [Array] Array of values
|
46
|
+
# @param column_name [Symbol] Dynamic column name
|
47
|
+
def format_dynamic_column_cells(cells, _column_name, _context)
|
48
|
+
cells
|
49
|
+
end
|
50
|
+
|
51
|
+
# Safe to override
|
52
|
+
#
|
53
|
+
# @return [String] formatted header
|
54
|
+
def format_dynamic_column_header(header_model, _column_name, _context)
|
55
|
+
header_model
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
The collection of Objects is expected to be provided through the context with a key of the same name as the declared dynamic_column.
|
62
|
+
|
63
|
+
## Aliasing
|
64
|
+
|
65
|
+
When the Dynamic Column is defined, it can be aliased to another name if needed:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
class UserCsvRowModel
|
69
|
+
include Csvbuilder::Model
|
70
|
+
|
71
|
+
column :name
|
72
|
+
|
73
|
+
dynamic_column :skills, as: :abilities
|
74
|
+
|
75
|
+
def ability(cell_value, header)
|
76
|
+
# logic comes here
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Formatted Headers
|
82
|
+
|
83
|
+
Formatting can be applied to the headers as follow:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class UserCsvRowModel
|
87
|
+
include Csvbuilder::Model
|
88
|
+
|
89
|
+
column :name
|
90
|
+
|
91
|
+
dynamic_column :skills
|
92
|
+
|
93
|
+
class << self
|
94
|
+
def format_dynamic_column_header(header_model, column_name, _context)
|
95
|
+
"#{column_name}: [#{header_model}]"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
For simple cases, the shorter way can be used instead:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
class UserCsvRowModel
|
105
|
+
include Csvbuilder::Model
|
106
|
+
|
107
|
+
column :name
|
108
|
+
|
109
|
+
dynamic_column :skills, header: ->(name) { "skills: [#{name}]" }
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
20
113
|
|
21
114
|
## Development
|
22
115
|
|
@@ -26,7 +119,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
26
119
|
|
27
120
|
## Contributing
|
28
121
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
122
|
+
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
123
|
|
31
124
|
## License
|
32
125
|
|
@@ -22,7 +22,11 @@ module Csvbuilder
|
|
22
22
|
}.freeze
|
23
23
|
ATTRIBUTE_METHODS.each do |method_name, attribute_method|
|
24
24
|
define_method(method_name) do
|
25
|
-
super().merge!
|
25
|
+
super().merge!(
|
26
|
+
column_names_to_attribute_value(
|
27
|
+
self.class.dynamic_column_names, attribute_method
|
28
|
+
)
|
29
|
+
)
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
@@ -38,7 +42,10 @@ module Csvbuilder
|
|
38
42
|
# Define default attribute method for a dynamic_column
|
39
43
|
# @param column_name [Symbol] the cell's column_name
|
40
44
|
def define_dynamic_attribute_method(column_name)
|
41
|
-
define_proxy_method(column_name)
|
45
|
+
define_proxy_method(column_name) do
|
46
|
+
original_attribute(column_name)
|
47
|
+
end
|
48
|
+
|
42
49
|
dynamic_attribute_class.define_process_cell(self, column_name)
|
43
50
|
end
|
44
51
|
|
@@ -21,12 +21,26 @@ module Csvbuilder
|
|
21
21
|
#
|
22
22
|
# header models
|
23
23
|
#
|
24
|
+
# return the collection given by the context
|
25
|
+
# i.e: skills: ["Ruby", "Python", "Javascript"]
|
26
|
+
# header_models => ["Ruby", "Python", "Javascript"]
|
27
|
+
#
|
28
|
+
# @return [Array] Array of values
|
24
29
|
def header_models
|
25
30
|
Array(context.public_send(header_models_context_key))
|
26
31
|
end
|
27
32
|
|
33
|
+
# The name of the dynamic_column
|
34
|
+
#
|
35
|
+
# i.e: With dynamic_column :skills
|
36
|
+
# => :skills
|
37
|
+
#
|
38
|
+
# i.e: With dynamic_column :skills, as: :programming_languages
|
39
|
+
# => :programming_languages
|
40
|
+
#
|
41
|
+
# @return [Symbol] Symbol
|
28
42
|
def header_models_context_key
|
29
|
-
options[:
|
43
|
+
options[:as] || column_name
|
30
44
|
end
|
31
45
|
end
|
32
46
|
end
|
@@ -7,20 +7,32 @@ module Csvbuilder
|
|
7
7
|
class DynamicColumnAttributeBase < AttributeBase
|
8
8
|
include DynamicColumnShared
|
9
9
|
|
10
|
+
# The value is the collection of cell's values
|
11
|
+
# @return [Array] Array of values
|
10
12
|
def value
|
11
13
|
@value ||= row_model_class.format_dynamic_column_cells(unformatted_value, column_name, row_model.context)
|
12
14
|
end
|
13
15
|
|
16
|
+
# The source_cells is meant to be implemented in the concret class
|
17
|
+
# @return [Array] Array of values
|
14
18
|
def formatted_cells
|
15
19
|
source_cells.map do |cell|
|
16
20
|
row_model_class.format_cell(cell, column_name, row_model.context)
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
24
|
+
def source_cells
|
25
|
+
raise NotImplementedError
|
26
|
+
end
|
27
|
+
|
28
|
+
def unformatted_value
|
29
|
+
raise NotImplementedError
|
30
|
+
end
|
31
|
+
|
20
32
|
protected
|
21
33
|
|
22
34
|
def process_cell_method_name
|
23
|
-
self.class.process_cell_method_name(
|
35
|
+
self.class.process_cell_method_name(header_models_context_key)
|
24
36
|
end
|
25
37
|
|
26
38
|
# Calls the process_cell to return the value of a Attribute given the args
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csvbuilder-dynamic-columns-core
|
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-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|