ntq_excelsior 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: 8bf1d29e56d85c440a139e5f9e51aab9acd22b68e2da799352435298382e9c11
4
+ data.tar.gz: 4de9735295bee676cb0fa6fe738cf18253348021b00060b1dacee3ae6d9d2197
5
+ SHA512:
6
+ metadata.gz: 179249f8e55907b347cc715c96c8b0b4ece8c0fa98526a2233fe5587333cf05a89d303b32e15c7ec921c0ccc55e620e330d762d279e99b68d6fa7d9bad8eee73
7
+ data.tar.gz: 54fe084c4e0cc8525703547a7075ed64791300cbbeb32664fdf75fba463a2a0e124c6e5c7b9e302c8e0ff8d3c08c660433f189cc2a01c9230a73b6ad2342ce62
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-02
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 kevin@9troisquarts.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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ntq_excelsior.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem 'caxlsx', "~> 3.2"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kevin
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,72 @@
1
+ # NtqExcelsior
2
+
3
+ Ntq excelsior simplifie l'import et l'export de vos données.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add ntq_excelsior
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install ntq_excelsior
14
+
15
+ ## Usage
16
+
17
+ ### Export
18
+
19
+ ```ruby
20
+ class UserExporter < NtqExcelsior::Exporter
21
+
22
+ schema ({
23
+ name: 'Mobilités',
24
+ extra_headers: [
25
+ [
26
+ {
27
+ title: "Utilisateurs",
28
+ width: 4
29
+ }
30
+ ],
31
+ ],
32
+ columns: [
33
+ {
34
+ title: 'Name',
35
+ resolve: -> (record) { [record.first_name, record.last_name].join(' ') },
36
+ },
37
+ {
38
+ title: 'Email',
39
+ resolve: 'email'
40
+ },
41
+ {
42
+ title: 'Address (nested)',
43
+ resolve: ['address', 'address_one']
44
+ },
45
+ {
46
+ title: 'City (nested)',
47
+ resolve: ['address', 'city']
48
+ }
49
+ ]
50
+ })
51
+
52
+ end
53
+
54
+ ```
55
+
56
+ ## Development
57
+
58
+ 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.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ntq_excelsior. 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]/ntq_excelsior/blob/master/CODE_OF_CONDUCT.md).
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in the NtqExcelsior project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ntq_excelsior/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,198 @@
1
+ require 'caxlsx'
2
+
3
+ module NtqExcelsior
4
+ class Exporter
5
+ attr_accessor :data
6
+
7
+ DEFAULT_STYLES = {
8
+ bold: {
9
+ b: true
10
+ },
11
+ italic: {
12
+ i: true
13
+ },
14
+ center: {
15
+ alignment: { wrap_text: true }
16
+ }
17
+ }
18
+
19
+ COLUMN_NAMES = Array('A'..'Z').freeze
20
+
21
+ class << self
22
+ def schema(value = nil)
23
+ @schema ||= value
24
+ end
25
+ def styles(value = nil)
26
+ @schema ||= value
27
+ end
28
+ end
29
+
30
+ def initialize(data)
31
+ @data = data
32
+ end
33
+
34
+ def schema
35
+ self.class.schema
36
+ end
37
+
38
+ def styles
39
+ self.class.styles
40
+ end
41
+
42
+ def column_name(col_index)
43
+ index = col_index - 1
44
+ return COLUMN_NAMES[index] if index < 26
45
+
46
+ letters = []
47
+ letters << index % 26
48
+
49
+ while index >= 26 do
50
+ index = index / 26 - 1
51
+ letters << index % 26
52
+ end
53
+
54
+ letters.reverse.map { |i| COLUMN_NAMES[i] }.join
55
+ end
56
+
57
+ def cell_name(col, row, *lock)
58
+ "#{lock.include?(:col) ? '$' : ''}#{column_name(col)}#{lock.include?(:row) ? '$' : ''}#{row}"
59
+ end
60
+
61
+ def cells_range(starting = [], ending = [])
62
+ "#{cell_name(*starting)}:#{cell_name(*ending)}"
63
+ end
64
+
65
+ def number_of_headers_row(columns, count = 1)
66
+ columns_with_children = columns.select{ |c| c[:children] && c[:children].any? }
67
+ return count unless columns_with_children && columns_with_children.size > 0
68
+
69
+ columns_with_children.each do |column|
70
+ number_of_children = number_of_headers_row(column[:children], count += 1)
71
+ count = number_of_children if number_of_children > count
72
+ end
73
+ count
74
+ end
75
+
76
+ def get_styles(styles)
77
+ end
78
+
79
+ def resolve_header_row(headers, index)
80
+ row = { values: [], styles: nil, merge_cells: [], height: nil }
81
+
82
+ col_index = 1
83
+ headers.each do |header|
84
+ width = header[:width] || 1
85
+ row[:values] << header[:title] || ''
86
+ if width > 1
87
+ colspan = width - 1
88
+ row[:values].push(*Array.new(colspan, nil))
89
+ row[:merge_cells].push cells_range([col_index, index], [col_index + colspan, index])
90
+ col_index += colspan
91
+ end
92
+
93
+ col_index += 1
94
+ end
95
+ row
96
+ end
97
+
98
+ def dig_value(value, accessors = [])
99
+ v = value
100
+ return v unless accessors && accessors.length > 0
101
+
102
+ v = v.send(accessors[0])
103
+ return v if accessors.length == 1
104
+ return dig_value(v, accessors.slice(1..-1))
105
+ end
106
+
107
+ def format_value(resolver, record)
108
+ return resolver.call(record) if resolver.is_a?(Proc)
109
+
110
+ accessors = resolver
111
+ accessors = accessors.split(".") if accessors.is_a?(String)
112
+ dig_value(record, accessors)
113
+ end
114
+
115
+ def resolve_record_row(schema, record, index)
116
+ row = { values: [], styles: nil, merge_cells: [], height: nil }
117
+ col_index = 1
118
+ schema.each do |column|
119
+ width = column[:width] || 1
120
+ row[:values] << format_value(column[:resolve], record)
121
+
122
+ if width > 1
123
+ colspan = width - 1
124
+ row[:values].push(*Array.new(colspan, nil))
125
+ row[:merge_cells].push cells_range([col_index, index], [col_index + colspan, index])
126
+ col_index += colspan
127
+ end
128
+
129
+ col_index += 1
130
+ end
131
+
132
+ row
133
+ end
134
+
135
+ def content
136
+ content = { rows: [] }
137
+ index = 0
138
+ (schema[:extra_headers] || []).each_with_index do |header|
139
+ index += 1
140
+ content[:rows] << resolve_header_row(header, index)
141
+ end
142
+ index += 1
143
+ content[:rows] << resolve_header_row(schema[:columns], index)
144
+ @data.each do |record|
145
+ index += 1
146
+ content[:rows] << resolve_record_row(schema[:columns], record, index)
147
+ end
148
+ content
149
+ end
150
+
151
+ def add_sheet_content(content, wb_styles, sheet)
152
+ content[:rows].each do |row|
153
+ row_style = wb_styles.add_style(row[:styles]) if row[:styles].is_a? Hash
154
+ row_style = row[:styles]&.map { |style| wb_styles.add_style(style) if style } if row[:styles].is_a? Array
155
+ sheet.add_row row[:values], style: row_style, height: row[:height]
156
+ next unless row[:merge_cells]
157
+
158
+ row[:merge_cells]&.each do |range|
159
+ sheet.merge_cells range
160
+ end
161
+ end
162
+
163
+ # do not apply styles if there are now rows
164
+ if content[:rows].present?
165
+ content[:styles]&.each_with_index do |(range, styles), index|
166
+ begin
167
+ sheet.add_style range, styles.except(:border) if range && styles
168
+ sheet.add_border range, styles[:border] if range && styles && styles[:border]
169
+ rescue NoMethodError
170
+ # do not apply styles if error
171
+ end
172
+ end
173
+
174
+ sheet.column_widths *content[:col_widths] if content[:col_widths].present?
175
+ end
176
+
177
+ sheet
178
+ end
179
+
180
+ def generate_workbook(wb, wb_styles)
181
+ columns = schema[:columns]
182
+ wb.add_worksheet(name: schema[:name]) do |sheet|
183
+ add_sheet_content content, wb_styles, sheet
184
+ end
185
+ end
186
+
187
+ def export
188
+ p = Axlsx::Package.new
189
+ wb = p.workbook
190
+ wb_styles = wb.styles
191
+
192
+ generate_workbook(wb, wb_styles)
193
+
194
+ p
195
+ end
196
+
197
+ end
198
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NtqExcelsior
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ntq_excelsior/version"
4
+ require 'ntq_excelsior/exporter'
5
+ module NtqExcelsior
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,4 @@
1
+ module NtqExcelsior
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ntq_excelsior
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kevin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: caxlsx
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ description: Library use by 9tq for import/export
28
+ email:
29
+ - kevin@9troisquarts.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/ntq_excelsior.rb
43
+ - lib/ntq_excelsior/exporter.rb
44
+ - lib/ntq_excelsior/version.rb
45
+ - sig/ntq_excelsior.rbs
46
+ homepage: https://github.com/9troisquarts/ntq-excelsior
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/9troisquarts/ntq-excelsior
51
+ source_code_uri: https://github.com/9troisquarts/ntq-excelsior
52
+ changelog_uri: https://github.com/9troisquarts/ntq-excelsior/CHANGELOG.md
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.6.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.3.7
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Library use by 9tq for import/export
72
+ test_files: []