ntq_excelsior 0.6.0 → 1.0.0

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: 30e1baadc2da0aac67e885c5e4c1a3db870b0701b8cd22b74a396d62b8ad75f0
4
- data.tar.gz: c186e0afb0b554b220678415dc228a88c1a7157066238d1453c92352657166bc
3
+ metadata.gz: b1b7c2ea8995dcb68450bc1771e8fef43e04dc6aeea67b585783378bf68bd8c1
4
+ data.tar.gz: a8f6b916a1b0cca7e6ee306a9453ac802d0ce859eb1f3bf82d1aa4d8e991ef40
5
5
  SHA512:
6
- metadata.gz: 271686e925552310034a81b8b5e91234a4832189075a08813d79cba53481afaddb8906e40ff765363868e78b81db432c19824c6b44bf7f38d4655bfa4359aa3a
7
- data.tar.gz: 5790ae3ee05cb1b71f03c880c937b66ac35c33cfa23615e351ff935904338db2532535294d107001a1063fa1f507d3b4bcdcdccd93fc841e9c18a93aad7a8f15
6
+ metadata.gz: 0efaeba2512cd02f8770014c4a02c06b7245e6e42db275bd0f4d5f2a5455ca54b1aaec40fc221086d50795e706b1ec5594a5054cb7a3d6b04898abf57d795d39
7
+ data.tar.gz: 3db4c3881a7a1677bd6a61b820384657cc25b2041f78463b175597b3032932f94550c7a8b941b975b18e571383dbdeb90e0e99618c873915c6aa57815c9fab0a
data/.codeclimate.yml ADDED
@@ -0,0 +1,19 @@
1
+ version: "2"
2
+ plugins:
3
+ rubocop:
4
+ enabled: true
5
+ channel: "beta"
6
+ exclude_patterns:
7
+ - "config/"
8
+ - "db/"
9
+ - "dist/"
10
+ - "features/"
11
+ - "**/node_modules/"
12
+ - "script/"
13
+ - "**/spec/"
14
+ - "**/test/"
15
+ - "**/tests/"
16
+ - "Tests/"
17
+ - "**/vendor/"
18
+ - "**/*_test.go"
19
+ - "**/*.d.ts"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ntq_excelsior (0.6.0)
4
+ ntq_excelsior (1.0.0)
5
5
  caxlsx (< 4)
6
6
  roo (< 3)
7
7
 
data/README.md CHANGED
@@ -132,7 +132,8 @@ class UserImporter < NtqExcelsior::Importer
132
132
  required: true
133
133
  },
134
134
  active: {
135
- header: /Actif/i,
135
+ header: /Actif.+/i,
136
+ humanized_header: 'Actif', # (Optional) if provided, will be displayed instead regex in missing headers
136
137
  required: false
137
138
  }
138
139
  })
@@ -2,11 +2,9 @@ require 'roo'
2
2
 
3
3
  module NtqExcelsior
4
4
  class Importer
5
-
6
5
  attr_accessor :file, :check, :lines, :options, :status_tracker
7
6
 
8
7
  class << self
9
-
10
8
  def autosave(value = nil)
11
9
  @autosave ||= value
12
10
  end
@@ -48,38 +46,56 @@ module NtqExcelsior
48
46
  return @spreadsheet unless @spreadsheet.nil?
49
47
 
50
48
  raise 'File is missing' unless file.present?
51
-
49
+
52
50
  @spreadsheet = Roo::Spreadsheet.open(file, self.class.spreadsheet_options || {})
53
51
  end
54
52
 
55
53
  def required_headers
56
54
  return @required_headers if @required_headers
57
55
 
58
- @required_columns = self.class.schema.select { |field, column_config| !column_config.is_a?(Hash) || !column_config.has_key?(:required) || column_config[:required] }
59
- @required_line_keys = @required_columns.map{ |k, v| k }
60
- @required_headers = @required_columns.map{ |k, column_config| column_config.is_a?(Hash) ? column_config[:header] : column_config }.map{|header| header.is_a?(String) ? Regexp.new("^#{header}$", "i") : header}
61
- if self.class.primary_key && !@required_line_keys.include?(self.class.primary_key)
62
- @required_line_keys = @required_line_keys.unshift(self.class.primary_key)
63
- @required_headers = @required_headers.unshift(Regexp.new(self.class.primary_key.to_s, "i"))
56
+ @required_columns = self.class.schema.select { |_field, column_config| !column_config.is_a?(Hash) || !column_config.key?(:required) || column_config[:required] }
57
+ @required_headers = @required_columns.values.map { |column| get_column_header(column) }.map { |header| tranform_header_to_regexp(header) }
58
+ if self.class.primary_key && !@required_columns.keys.include?(self.class.primary_key)
59
+ @required_headers.unshift(Regexp.new(self.class.primary_key.to_s, "i"))
64
60
  end
65
61
  @required_headers
66
62
  end
67
63
 
68
64
  def spreadsheet_data
69
- spreadsheet.sheet(spreadsheet.sheets[0]).parse(header_search: required_headers)
65
+ begin
66
+ spreadsheet_data = spreadsheet.sheet(spreadsheet.sheets[0]).parse(header_search: required_headers)
67
+ raise 'File is inconsistent, please check all headers of your file for specials characters (, / ; etc...)'
68
+ rescue Roo::HeaderRowNotFoundError => e
69
+ missing_headers = []
70
+
71
+ e.message.slice(1..-1).chop.split(",").map(&:strip).each do |header_missing|
72
+ header_missing_regex = tranform_header_to_regexp(header_missing, true)
73
+ header_found = @required_columns.values.find do |column|
74
+ tranform_header_to_regexp(get_column_header(column)) == header_missing_regex
75
+ end
76
+ if header_found && header_found.is_a?(Hash) && header_found[:humanized_header].present?
77
+ missing_headers << header_found[:humanized_header]
78
+ else
79
+ missing_headers << header_missing
80
+ end
81
+ end
82
+ raise Roo::HeaderRowNotFoundError, missing_headers.join(", ")
83
+ end
70
84
  end
71
85
 
72
86
  def detect_header_scheme
73
87
  return @header_scheme if @header_scheme
88
+
74
89
  @header_scheme = {}
75
- l = spreadsheet_data[0].dup
90
+ l = spreadsheet_data[0].dup || []
76
91
 
77
92
  self.class.schema.each do |field, column_config|
78
93
  header = column_config.is_a?(Hash) ? column_config[:header] : column_config
79
94
 
80
95
  l.each do |parsed_header, _value|
96
+ next unless parsed_header
81
97
  next unless (header.is_a?(Regexp) && parsed_header && parsed_header.match?(header)) || header.is_a?(String) && parsed_header == header
82
-
98
+
83
99
  l.delete(parsed_header)
84
100
  @header_scheme[parsed_header] = field
85
101
  end
@@ -131,7 +147,7 @@ module NtqExcelsior
131
147
  def record_attributes(record)
132
148
  return @record_attributes if @record_attributes
133
149
 
134
- @record_attributes = self.class.schema.keys.select{|k| k.to_sym != :id && record.respond_to?(:"#{k}=") }
150
+ @record_attributes = self.class.schema.keys.select { |k| k.to_sym != :id && record.respond_to?(:"#{k}=") }
135
151
  end
136
152
 
137
153
  def set_record_fields(record, line)
@@ -149,7 +165,7 @@ module NtqExcelsior
149
165
  @success = false
150
166
  @action = nil
151
167
  @errors = []
152
-
168
+
153
169
  if (self.class.autoset)
154
170
  record = set_record_fields(record, line)
155
171
  end
@@ -199,6 +215,24 @@ module NtqExcelsior
199
215
 
200
216
  { success_count: success_count, not_found_count: not_found_count, errors: errors_lines }
201
217
  end
218
+
219
+ private
220
+
221
+ def get_column_header(column)
222
+ return column unless column.is_a?(Hash)
223
+
224
+ column[:header]
225
+ end
226
+
227
+ def tranform_header_to_regexp(header, gsub_enclosure = false)
228
+ return header unless header.is_a?(String)
229
+
230
+ if gsub_enclosure && header.scan(/^\/[\^]?([^(\$\/)]+)[\$]?\/[i]?$/i) && $1
231
+ header = $1
232
+ end
233
+ Regexp.new("^#{header}$", "i")
234
+ end
202
235
 
203
236
  end
237
+
204
238
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NtqExcelsior
4
- VERSION = "0.6.0"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ntq_excelsior
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-03 00:00:00.000000000 Z
11
+ date: 2023-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: caxlsx
@@ -45,6 +45,7 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - ".codeclimate.yml"
48
49
  - ".rspec"
49
50
  - ".rubocop.yml"
50
51
  - CHANGELOG.md