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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 286cc68dc88a8b1e34d6c35178197a18baaf87325413eb21cf3f85a988a189f2
4
- data.tar.gz: 2c67f25a060145b14c161fa14ec6cc1ae48f2c8176868f15b2c1b8f1900f7854
3
+ metadata.gz: 5fdfc267670291ff9caefe6dfee13d22561acaac5286f31eee8124f980b32936
4
+ data.tar.gz: e1308d57584b7bfc37ab11e125e0cc3f4bc4bab28a9da0eac62ba4f85c7a2642
5
5
  SHA512:
6
- metadata.gz: b978378d072890a66da304afc010f0eeabe18e204927c4f9ccc18888ab497acc3cb1cf22f6fe8812423bedaabd4bd9a2b3b2920c1c52a35244e1006b794304b6
7
- data.tar.gz: 642a379dc4ed3a6eee45846994fd856015f7b24d9a15f5e07f2438d1287e4242a01324c03bca5de492eee1ef7461556e6d94b42cb76e9fcf107635504ba94e01
6
+ metadata.gz: bff40a5481025d39c46822f193b49e2df35ad946c6c60cb1c8f366767d4e1915cddb92f82962af36a4c832c99c48e38eb8c4756edf1594d4f537ca4d312b1078
7
+ data.tar.gz: 50346320272ecf16a63b4736a3736b9c9c5621a6a403c720e29ea284c3431be07389ef67dcbaa0cd6d05e6eb2ecd8f71713d51a47f67b82390d0dd6e210c4d86
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2022-12-21
4
+
5
+ Change the option :header_models_context_key to :as
6
+ Fix the call of process_cell_method_name with the correct name
7
+
3
8
  ## [0.1.0] - 2022-12-21
4
9
 
5
10
  - Initial release
data/Gemfile.lock CHANGED
@@ -55,7 +55,7 @@ GEM
55
55
  unicode-display_width (>= 1.4.0, < 3.0)
56
56
  rubocop-ast (1.24.0)
57
57
  parser (>= 3.1.1.0)
58
- rubocop-performance (1.15.1)
58
+ rubocop-performance (1.15.2)
59
59
  rubocop (>= 1.7.0, < 2.0)
60
60
  rubocop-ast (>= 0.4.0)
61
61
  rubocop-rake (0.6.0)
data/README.md CHANGED
@@ -1,22 +1,115 @@
1
- # Csvbuilder::Dynamic::Columns
1
+ # Csvbuilder::Dynamic::Columns::Core
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::Core](https://github.com/joel/csvbuilder-dynamic-columns-core) 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 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
- TODO: Write usage instructions here
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/[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).
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! column_names_to_attribute_value(self.class.dynamic_column_names, attribute_method)
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) { original_attribute(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[:header_models_context_key] || column_name
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(column_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
@@ -4,7 +4,7 @@ module Csvbuilder
4
4
  module Dynamic
5
5
  module Columns
6
6
  module Core
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-core
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-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport