csvbuilder-dynamic-columns-core 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 286cc68dc88a8b1e34d6c35178197a18baaf87325413eb21cf3f85a988a189f2
4
+ data.tar.gz: 2c67f25a060145b14c161fa14ec6cc1ae48f2c8176868f15b2c1b8f1900f7854
5
+ SHA512:
6
+ metadata.gz: b978378d072890a66da304afc010f0eeabe18e204927c4f9ccc18888ab497acc3cb1cf22f6fe8812423bedaabd4bd9a2b3b2920c1c52a35244e1006b794304b6
7
+ data.tar.gz: 642a379dc4ed3a6eee45846994fd856015f7b24d9a15f5e07f2438d1287e4242a01324c03bca5de492eee1ef7461556e6d94b42cb76e9fcf107635504ba94e01
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-performance
5
+ - rubocop-rake
6
+ - rubocop-rspec
7
+
8
+ AllCops:
9
+ NewCops: enable
10
+ TargetRubyVersion: 2.6
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/StringLiteralsInInterpolation:
17
+ Enabled: true
18
+ EnforcedStyle: double_quotes
19
+
20
+ Layout/LineLength:
21
+ Max: 120
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,96 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2022-12-21 17:24:07 UTC using RuboCop version 1.41.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 7
10
+ # This cop supports safe autocorrection (--autocorrect).
11
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
12
+ # URISchemes: http, https
13
+ Layout/LineLength:
14
+ Enabled: false
15
+
16
+ # Offense count: 1
17
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods, inherit_mode.
18
+ # AllowedMethods: refine
19
+ Metrics/BlockLength:
20
+ Max: 29
21
+
22
+ Metrics/ModuleLength:
23
+ Exclude:
24
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base_spec.rb'
25
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/import/dynamic_columns_spec.rb'
26
+
27
+ # Offense count: 4
28
+ # Configuration parameters: Prefixes, AllowedPatterns.
29
+ # Prefixes: when, with, without
30
+ RSpec/ContextWording:
31
+ Exclude:
32
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base_spec.rb'
33
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/export/dynamic_columns_spec.rb'
34
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/import/dynamic_columns_spec.rb'
35
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns_spec.rb'
36
+
37
+ # Offense count: 3
38
+ # Configuration parameters: CountAsOne.
39
+ RSpec/ExampleLength:
40
+ Max: 10
41
+ Exclude:
42
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base_spec.rb'
43
+
44
+ # Offense count: 1
45
+ RSpec/ExpectInHook:
46
+ Exclude:
47
+ - 'spec/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared_spec.rb'
48
+
49
+ # Offense count: 11
50
+ # Configuration parameters: .
51
+ # SupportedStyles: have_received, receive
52
+ RSpec/MessageSpies:
53
+ EnforcedStyle: receive
54
+
55
+ # Offense count: 9
56
+ RSpec/MultipleExpectations:
57
+ Max: 3
58
+
59
+ # Offense count: 6
60
+ RSpec/RepeatedDescription:
61
+ Exclude:
62
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base_spec.rb'
63
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/export/dynamic_columns_spec.rb'
64
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns_spec.rb'
65
+
66
+ # Offense count: 3
67
+ RSpec/StubbedMock:
68
+ Exclude:
69
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/import/dynamic_columns_spec.rb'
70
+ - 'spec/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared_spec.rb'
71
+ - 'spec/csvbuilder/dynamic/columns/core/internal/dynamic_column_attribute_base_spec.rb'
72
+
73
+ # Offense count: 9
74
+ # Configuration parameters: AllowedConstants.
75
+ Style/Documentation:
76
+ Exclude:
77
+ - 'spec/**/*'
78
+ - 'test/**/*'
79
+ - 'lib/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base.rb'
80
+ - 'lib/csvbuilder/dynamic/columns/core/concerns/export/dynamic_columns.rb'
81
+ - 'lib/csvbuilder/dynamic/columns/core/concerns/import/dynamic_columns.rb'
82
+ - 'lib/csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns.rb'
83
+ - 'lib/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared.rb'
84
+ - 'lib/csvbuilder/dynamic/columns/core/internal/dynamic_column_attribute_base.rb'
85
+ - 'lib/csvbuilder/dynamic/columns/core/internal/export/dynamic_column_attribute.rb'
86
+ - 'lib/csvbuilder/dynamic/columns/core/internal/import/dynamic_column_attribute.rb'
87
+ - 'lib/csvbuilder/dynamic/columns/core/internal/model/dynamic_column_header.rb'
88
+
89
+ # Offense count: 6
90
+ Style/OpenStructUse:
91
+ Exclude:
92
+ - 'spec/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base_spec.rb'
93
+ - 'spec/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared_spec.rb'
94
+ - 'spec/csvbuilder/dynamic/columns/core/internal/dynamic_column_attribute_base_spec.rb'
95
+ - 'spec/csvbuilder/dynamic/columns/core/internal/export/dynamic_column_attribute_spec.rb'
96
+ - 'spec/support/fixtures/concerns/attributes.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-12-21
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joel.azemar@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in csvbuilder-dynamic-columns.gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+
10
+ gem "rspec"
11
+
12
+ gem "rubocop"
13
+
14
+ gem "rubocop-performance"
15
+ gem "rubocop-rake"
16
+ gem "rubocop-rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ csvbuilder-dynamic-columns-core (0.1.0)
5
+ activesupport (>= 5.2, < 8)
6
+ csvbuilder-core
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (6.1.7)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 1.6, < 2)
14
+ minitest (>= 5.1)
15
+ tzinfo (~> 2.0)
16
+ zeitwerk (~> 2.3)
17
+ ast (2.4.2)
18
+ concurrent-ruby (1.1.10)
19
+ csvbuilder-core (0.1.2)
20
+ activesupport (>= 5.2, < 8)
21
+ diff-lcs (1.5.0)
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ json (2.6.3)
25
+ minitest (5.16.3)
26
+ parallel (1.22.1)
27
+ parser (3.1.3.0)
28
+ ast (~> 2.4.1)
29
+ rainbow (3.1.1)
30
+ rake (13.0.6)
31
+ regexp_parser (2.6.1)
32
+ rexml (3.2.5)
33
+ rspec (3.12.0)
34
+ rspec-core (~> 3.12.0)
35
+ rspec-expectations (~> 3.12.0)
36
+ rspec-mocks (~> 3.12.0)
37
+ rspec-core (3.12.0)
38
+ rspec-support (~> 3.12.0)
39
+ rspec-expectations (3.12.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-mocks (3.12.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-support (3.12.0)
46
+ rubocop (1.41.1)
47
+ json (~> 2.3)
48
+ parallel (~> 1.10)
49
+ parser (>= 3.1.2.1)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ regexp_parser (>= 1.8, < 3.0)
52
+ rexml (>= 3.2.5, < 4.0)
53
+ rubocop-ast (>= 1.23.0, < 2.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 3.0)
56
+ rubocop-ast (1.24.0)
57
+ parser (>= 3.1.1.0)
58
+ rubocop-performance (1.15.1)
59
+ rubocop (>= 1.7.0, < 2.0)
60
+ rubocop-ast (>= 0.4.0)
61
+ rubocop-rake (0.6.0)
62
+ rubocop (~> 1.0)
63
+ rubocop-rspec (2.16.0)
64
+ rubocop (~> 1.33)
65
+ ruby-progressbar (1.11.0)
66
+ tzinfo (2.0.5)
67
+ concurrent-ruby (~> 1.0)
68
+ unicode-display_width (2.3.0)
69
+ zeitwerk (2.6.6)
70
+
71
+ PLATFORMS
72
+ arm64-darwin-21
73
+ x86_64-linux
74
+
75
+ DEPENDENCIES
76
+ csvbuilder-dynamic-columns-core!
77
+ rake
78
+ rspec
79
+ rubocop
80
+ rubocop-performance
81
+ rubocop-rake
82
+ rubocop-rspec
83
+
84
+ BUNDLED WITH
85
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Joel Azemar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # Csvbuilder::Dynamic::Columns
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add csvbuilder-dynamic-columns
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install csvbuilder-dynamic-columns
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
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).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Csvbuilder::Dynamic::Columns project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/csvbuilder-dynamic-columns/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new(:rubocop) do |task|
11
+ task.options = ["-A"]
12
+ end
13
+
14
+ task default: %i[spec rubocop]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Runtime dependencies
6
+ gem "activesupport", "~> 5.2"
7
+
8
+ # Libraries dependencies
9
+ gem "csvbuilder-core"
10
+
11
+ # Development dependencies
12
+ gem "rake"
13
+ gem "rspec"
14
+ gem "rubocop"
15
+ gem "rubocop-performance"
16
+ gem "rubocop-rake"
17
+ gem "rubocop-rspec"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Runtime dependencies
6
+ gem "activesupport", "~> 6.1"
7
+
8
+ # Libraries dependencies
9
+ gem "csvbuilder-core"
10
+
11
+ # Development dependencies
12
+ gem "rake"
13
+ gem "rspec"
14
+ gem "rubocop"
15
+ gem "rubocop-performance"
16
+ gem "rubocop-rake"
17
+ gem "rubocop-rspec"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Runtime dependencies
6
+ gem "activemodel", "~> 7.0"
7
+
8
+ # Libraries dependencies
9
+ gem "csvbuilder-core"
10
+
11
+ # Development dependencies
12
+ gem "rake"
13
+ gem "rspec"
14
+ gem "rubocop"
15
+ gem "rubocop-performance"
16
+ gem "rubocop-rake"
17
+ gem "rubocop-rspec"
data/lib/.DS_Store ADDED
Binary file
Binary file
Binary file
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns"
4
+
5
+ # Shared between Import and Export, see test fixture for basic setup
6
+ module Csvbuilder
7
+ module DynamicColumnsBase
8
+ extend ActiveSupport::Concern
9
+ include Model::DynamicColumns
10
+
11
+ def attribute_objects
12
+ @attribute_objects ||= super.merge(dynamic_column_attribute_objects)
13
+ end
14
+
15
+ def attributes
16
+ super.merge!(attributes_from_method_names(self.class.dynamic_column_names))
17
+ end
18
+
19
+ ATTRIBUTE_METHODS = {
20
+ original_attributes: :value, # a map of `column_name => original_attribute(column_name)`
21
+ formatted_attributes: :formatted_cells # a map of `column_name => format_cell(column_name, ...)`
22
+ }.freeze
23
+ ATTRIBUTE_METHODS.each do |method_name, attribute_method|
24
+ define_method(method_name) do
25
+ super().merge! column_names_to_attribute_value(self.class.dynamic_column_names, attribute_method)
26
+ end
27
+ end
28
+
29
+ class_methods do
30
+ protected
31
+
32
+ # See {Model::DynamicColumns#dynamic_column}
33
+ def dynamic_column(column_name, options = {})
34
+ super
35
+ define_dynamic_attribute_method(column_name)
36
+ end
37
+
38
+ # Define default attribute method for a dynamic_column
39
+ # @param column_name [Symbol] the cell's column_name
40
+ def define_dynamic_attribute_method(column_name)
41
+ define_proxy_method(column_name) { original_attribute(column_name) }
42
+ dynamic_attribute_class.define_process_cell(self, column_name)
43
+ end
44
+
45
+ def ensure_define_dynamic_attribute_method
46
+ dynamic_column_names.each { |*args| define_dynamic_attribute_method(*args) }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/dynamic/columns/core/internal/model/dynamic_column_header"
4
+
5
+ module Csvbuilder
6
+ module Model
7
+ module DynamicColumns
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ mattr_accessor :_dynamic_columns, instance_writer: false, instance_reader: false
12
+ end
13
+
14
+ class_methods do
15
+ def dynamic_columns?
16
+ dynamic_columns.present?
17
+ end
18
+
19
+ # @return [Integer] index of dynamic_column of all columns
20
+ def dynamic_column_index(column_name)
21
+ offset = dynamic_column_names.index(column_name)
22
+ offset ? columns.size + offset : nil
23
+ end
24
+
25
+ # @return [Array<Symbol>] column names for the row model
26
+ def dynamic_column_names
27
+ dynamic_columns.keys
28
+ end
29
+
30
+ # See Model::Columns::headers
31
+ def headers(context = {})
32
+ super + dynamic_column_headers(context)
33
+ end
34
+
35
+ def dynamic_column_headers(context = {})
36
+ dynamic_column_names.map { |column_name| DynamicColumnHeader.new(column_name, self, context).value }.flatten
37
+ end
38
+
39
+ # Safe to override. Method applied to each dynamic_column attribute
40
+ #
41
+ # @param cells [Array] Array of values
42
+ # @param column_name [Symbol] Dynamic column name
43
+ def format_dynamic_column_cells(cells, _column_name, _context)
44
+ cells
45
+ end
46
+
47
+ # Safe to override
48
+ #
49
+ # @return [String] formatted header
50
+ def format_dynamic_column_header(header_model, _column_name, _context)
51
+ header_model
52
+ end
53
+
54
+ def dynamic_columns
55
+ _dynamic_columns || {}
56
+ end
57
+
58
+ protected
59
+
60
+ # define a dynamic_column, must be after all normal columns
61
+ #
62
+ # @param column_name [Symbol] column_name
63
+ # @option options [String] :header human friendly string of the column name, by default format_header(column_name)
64
+ def dynamic_column(column_name, options = {})
65
+ self._dynamic_columns = dynamic_columns.merge(column_name.to_sym => options)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/core/internal/concerns/column_shared"
4
+
5
+ module Csvbuilder
6
+ module DynamicColumnShared
7
+ include ColumnShared
8
+ extend ActiveSupport::Concern
9
+
10
+ #
11
+ # row_model_class
12
+ #
13
+ def column_index
14
+ @column_index ||= row_model_class.dynamic_column_index(column_name)
15
+ end
16
+
17
+ def options
18
+ row_model_class.dynamic_columns[column_name]
19
+ end
20
+
21
+ #
22
+ # header models
23
+ #
24
+ def header_models
25
+ Array(context.public_send(header_models_context_key))
26
+ end
27
+
28
+ def header_models_context_key
29
+ options[:header_models_context_key] || column_name
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/core/internal/attribute_base"
4
+ require "csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared"
5
+
6
+ module Csvbuilder
7
+ class DynamicColumnAttributeBase < AttributeBase
8
+ include DynamicColumnShared
9
+
10
+ def value
11
+ @value ||= row_model_class.format_dynamic_column_cells(unformatted_value, column_name, row_model.context)
12
+ end
13
+
14
+ def formatted_cells
15
+ source_cells.map do |cell|
16
+ row_model_class.format_cell(cell, column_name, row_model.context)
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def process_cell_method_name
23
+ self.class.process_cell_method_name(column_name)
24
+ end
25
+
26
+ # Calls the process_cell to return the value of a Attribute given the args
27
+ def call_process_cell(*args)
28
+ row_model.public_send(process_cell_method_name, *args)
29
+ end
30
+
31
+ class << self
32
+ def process_cell_method_name(column_name)
33
+ column_name.to_s.singularize.to_sym
34
+ end
35
+
36
+ # define a method to process each cell of the attribute method
37
+ # process_cell = one cell
38
+ # attribute_method = many cells = [process_cell(), process_cell()...]
39
+ def define_process_cell(row_model_class, column_name, &block)
40
+ row_model_class.define_proxy_method(process_cell_method_name(column_name), &block)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/core/internal/model/header"
4
+ require "csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared"
5
+
6
+ module Csvbuilder
7
+ module Model
8
+ class DynamicColumnHeader < Header
9
+ include DynamicColumnShared
10
+
11
+ def value
12
+ header_models.map { |header_model| header_proc.call(header_model) }
13
+ end
14
+
15
+ def header_proc
16
+ options[:header] || ->(header_model) { format_header(header_model) }
17
+ end
18
+
19
+ def format_header(header_model)
20
+ row_model_class.format_dynamic_column_header(header_model, column_name, context)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/core/public/model"
4
+
5
+ require "csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns"
6
+ Csvbuilder::Model.include(Csvbuilder::Model::DynamicColumns)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Csvbuilder
4
+ module Dynamic
5
+ module Columns
6
+ module Core
7
+ VERSION = "0.1.0"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csvbuilder/dynamic/columns/core/version"
4
+
5
+ require "csvbuilder/core"
6
+
7
+ require "csvbuilder/dynamic/columns/core/public/model"
@@ -0,0 +1,8 @@
1
+ module Csvbuilder
2
+ module Dynamic
3
+ module Columns
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csvbuilder-dynamic-columns-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Azemar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: csvbuilder-core
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Help handle CSVs in a more efficient way
48
+ email:
49
+ - joel.azemar@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - ".DS_Store"
55
+ - ".rspec"
56
+ - ".rubocop.yml"
57
+ - ".rubocop_todo.yml"
58
+ - CHANGELOG.md
59
+ - CODE_OF_CONDUCT.md
60
+ - Gemfile
61
+ - Gemfile.lock
62
+ - LICENSE.txt
63
+ - README.md
64
+ - Rakefile
65
+ - gemfiles/Gemfile.5.2._gemfile
66
+ - gemfiles/Gemfile.6.1._gemfile
67
+ - gemfiles/Gemfile.7.0._gemfile
68
+ - lib/.DS_Store
69
+ - lib/csvbuilder/.DS_Store
70
+ - lib/csvbuilder/dynamic/.DS_Store
71
+ - lib/csvbuilder/dynamic/columns/.DS_Store
72
+ - lib/csvbuilder/dynamic/columns/core.rb
73
+ - lib/csvbuilder/dynamic/columns/core/concerns/dynamic_columns_base.rb
74
+ - lib/csvbuilder/dynamic/columns/core/concerns/model/dynamic_columns.rb
75
+ - lib/csvbuilder/dynamic/columns/core/internal/concerns/dynamic_column_shared.rb
76
+ - lib/csvbuilder/dynamic/columns/core/internal/dynamic_column_attribute_base.rb
77
+ - lib/csvbuilder/dynamic/columns/core/internal/model/dynamic_column_header.rb
78
+ - lib/csvbuilder/dynamic/columns/core/public/model.rb
79
+ - lib/csvbuilder/dynamic/columns/core/version.rb
80
+ - sig/csvbuilder/dynamic/columns.rbs
81
+ homepage: https://github.com/joel/csvbuilder-dynamic-columns
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ homepage_uri: https://github.com/joel/csvbuilder-dynamic-columns
86
+ source_code_uri: https://github.com/joel/csvbuilder-dynamic-columns
87
+ changelog_uri: https://github.com/joel/csvbuilder-dynamic-columns/blob/main/CHANGELOG.md
88
+ rubygems_mfa_required: 'true'
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.6.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.3.22
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Csvbuilder Dynamic Columns contain the components to handle CSV dynamic columns
108
+ test_files: []