Skip to content

Commit f37db07

Browse files
committed
chore: rubocop
1 parent 4348911 commit f37db07

20 files changed

+186
-66
lines changed

.rubocop.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
inherit_from: .rubocop_todo.yml
2+
13
AllCops:
24
NewCops: enable
35
SuggestExtensions: false

.rubocop_todo.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2024-08-27 09:46:41 UTC using RuboCop version 1.65.1.
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: 1
10+
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
11+
Lint/DuplicateBranch:
12+
Exclude:
13+
- 'lib/genericode/code_list.rb'
14+
15+
# Offense count: 8
16+
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
17+
Metrics/AbcSize:
18+
Max: 182
19+
20+
# Offense count: 2
21+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
22+
# AllowedMethods: refine
23+
Metrics/BlockLength:
24+
Max: 34
25+
26+
# Offense count: 1
27+
# Configuration parameters: CountComments, CountAsOne.
28+
Metrics/ClassLength:
29+
Max: 220
30+
31+
# Offense count: 5
32+
# Configuration parameters: AllowedMethods, AllowedPatterns.
33+
Metrics/CyclomaticComplexity:
34+
Max: 92
35+
36+
# Offense count: 10
37+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
38+
Metrics/MethodLength:
39+
Max: 86
40+
41+
# Offense count: 3
42+
# Configuration parameters: AllowedMethods, AllowedPatterns.
43+
Metrics/PerceivedComplexity:
44+
Max: 92
45+
46+
# Offense count: 2
47+
# This cop supports safe autocorrection (--autocorrect).
48+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
49+
# URISchemes: http, https
50+
Layout/LineLength:
51+
Max: 121

exe/genericode

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require_relative "../lib/genericode"
45
require_relative "../lib/genericode/cli"

genericode.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Gem::Specification.new do |spec|
2929
spec.require_paths = ["lib"]
3030

3131
spec.add_dependency "lutaml-model"
32-
spec.add_dependency "thor"
3332
spec.add_dependency "tabulo"
33+
spec.add_dependency "thor"
3434

3535
spec.add_development_dependency "nokogiri"
3636
spec.add_development_dependency "rake"

lib/genericode/cli.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Genericode
24
module Cli
35
end

lib/genericode/cli/code_lister.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative "../code_list"
24
require "tabulo"
35
require "csv"
@@ -12,10 +14,8 @@ def list_codes(file_path, format: :tsv)
1214
# Validate data types
1315
code_list.validate_verbose.each do |error|
1416
raise Error, "#{error[:code]}: #{error[:message]}" if error[:code] == "INVALID_DATA_TYPE"
15-
end
1617

17-
# Ensure valid ColumnRefs
18-
code_list.validate_verbose.each do |error|
18+
# Ensure valid ColumnRefs
1919
raise Error, "#{error[:code]}: #{error[:message]}" if error[:code] == "INVALID_COLUMN_REF"
2020
end
2121

lib/genericode/cli/code_lookup.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative "../code_list"
24

35
module Genericode

lib/genericode/cli/commands.rb

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "thor"
24
require_relative "validator"
35
require_relative "converter"
@@ -10,9 +12,7 @@ class Commands < Thor
1012
desc "convert INPUT OUTPUT", "Convert between Genericode XML and JSON formats"
1113

1214
def convert(input, output)
13-
if Converter.convert(input, output)
14-
puts "Conversion successful."
15-
end
15+
puts "Conversion successful." if Converter.convert(input, output)
1616
rescue Error => e
1717
puts "Conversion failed: #{e.message}"
1818
end
@@ -32,19 +32,17 @@ def validate(file)
3232
puts " [#{error[:code]}] #{error[:message]}"
3333
end
3434
end
35+
elsif code_list.valid?
36+
puts "File is valid."
3537
else
36-
if code_list.valid?
37-
puts "File is valid."
38-
else
39-
puts "File is invalid."
40-
end
38+
puts "File is invalid."
4139
end
4240
rescue Error => e
4341
puts "Validation failed: #{e.message}"
4442
end
4543

4644
desc "list_codes FILE", "List all codes and their associated data in a Genericode file"
47-
option :format, type: :string, default: "tsv", enum: ["tsv", "table"], desc: "Output format (tsv or table)"
45+
option :format, type: :string, default: "tsv", enum: %w[tsv table], desc: "Output format (tsv or table)"
4846
option :output, type: :string, desc: "Output file path (default: stdout)"
4947

5048
def list_codes(file)

lib/genericode/cli/converter.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Genericode
24
module Cli
35
class Converter
@@ -13,10 +15,10 @@ def self.convert(input_path, output_path)
1315
code_list = CodeList.from_file(input_path)
1416

1517
result = if output_format == ".gcj"
16-
code_list.to_json
17-
else
18-
code_list.to_xml
19-
end
18+
code_list.to_json
19+
else
20+
code_list.to_xml
21+
end
2022

2123
File.write(output_path, result)
2224
true

lib/genericode/cli/validator.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Genericode
24
module Cli
35
class Validator

lib/genericode/code_list.rb

+54-32
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,32 @@ def simple_code_list_to_json(model, doc)
8989

9090
def lookup(path)
9191
parts = path.split(">")
92-
conditions = parts[0].split(",").map { |c| c.split(":") }.to_h
92+
conditions = parts[0].split(",").to_h { |c| c.split(":") }
9393
target_column = parts[1]
9494

9595
result = simple_code_list.row.find do |row|
9696
conditions.all? do |col, value|
9797
column = column_set.column.find { |c| c.short_name.content.downcase == col.downcase }
9898
raise Error, "Column not found: #{col}" unless column
99+
99100
row_value = row.value.find { |v| v.column_ref == column.id }&.simple_value&.content
100101
row_value == value
101102
end
102103
end
103104

104-
if result
105-
if target_column
106-
column = column_set.column.find { |c| c.short_name.content.downcase == target_column.downcase }
107-
raise Error, "Target column not found: #{target_column}" unless column
108-
result.value.find { |v| v.column_ref == column.id }&.simple_value&.content
109-
else
110-
result.value.map { |v| [column_set.column.find { |c| c.id == v.column_ref }.short_name.content, v.simple_value.content] }.to_h
111-
end
105+
raise Error, "No matching row found for path: #{path}" unless result
106+
107+
if target_column
108+
column = column_set.column.find { |c| c.short_name.content.downcase == target_column.downcase }
109+
raise Error, "Target column not found: #{target_column}" unless column
110+
111+
result.value.find { |v| v.column_ref == column.id }&.simple_value&.content
112112
else
113-
raise Error, "No matching row found for path: #{path}"
113+
result.value.to_h do |v|
114+
[column_set.column.find do |c|
115+
c.id == v.column_ref
116+
end.short_name.content, v.simple_value.content,]
117+
end
114118
end
115119
end
116120

@@ -122,10 +126,16 @@ def validate_verbose
122126
errors = []
123127

124128
# Rule 1: ColumnSet presence
125-
errors << { code: "MISSING_COLUMN_SET", message: "ColumnSet is missing or empty" } if column_set.nil? || column_set.column.empty?
129+
if column_set.nil? || column_set.column.empty?
130+
errors << { code: "MISSING_COLUMN_SET",
131+
message: "ColumnSet is missing or empty", }
132+
end
126133

127134
# Rule 2: SimpleCodeList presence
128-
errors << { code: "MISSING_SIMPLE_CODE_LIST", message: "SimpleCodeList is missing or empty" } if simple_code_list.nil? || simple_code_list.row.empty?
135+
if simple_code_list.nil? || simple_code_list.row.empty?
136+
errors << { code: "MISSING_SIMPLE_CODE_LIST",
137+
message: "SimpleCodeList is missing or empty", }
138+
end
129139

130140
# Rule 3: Unique column IDs
131141
column_ids = column_set&.column&.map(&:id) || []
@@ -137,7 +147,8 @@ def validate_verbose
137147
simple_code_list&.row&.each_with_index do |row, index|
138148
row.value.each do |value|
139149
unless column_ids.include?(value.column_ref)
140-
errors << { code: "INVALID_COLUMN_REF", message: "Invalid ColumnRef '#{value.column_ref}' in row #{index + 1}" }
150+
errors << { code: "INVALID_COLUMN_REF",
151+
message: "Invalid ColumnRef '#{value.column_ref}' in row #{index + 1}", }
141152
end
142153
end
143154
end
@@ -158,7 +169,8 @@ def validate_verbose
158169
simple_code_list&.row&.each_with_index do |row, index|
159170
required_columns.each do |col|
160171
unless row.value.any? { |v| v.column_ref == col.id && v.simple_value&.content }
161-
errors << { code: "MISSING_REQUIRED_VALUE", message: "Missing value for required column '#{col.short_name&.content}' in row #{index + 1}" }
172+
errors << { code: "MISSING_REQUIRED_VALUE",
173+
message: "Missing value for required column '#{col.short_name&.content}' in row #{index + 1}", }
162174
end
163175
end
164176
end
@@ -169,7 +181,8 @@ def validate_verbose
169181
simple_code_list&.row&.each_with_index do |row, index|
170182
value = row.value.find { |v| v.column_ref == col.id }&.simple_value&.content
171183
unless value_matches_type?(value, data_type)
172-
errors << { code: "INVALID_DATA_TYPE", message: "Invalid data type for column '#{col.short_name&.content}' in row #{index + 1}" }
184+
errors << { code: "INVALID_DATA_TYPE",
185+
message: "Invalid data type for column '#{col.short_name&.content}' in row #{index + 1}", }
173186
end
174187
end
175188
end
@@ -182,21 +195,20 @@ def validate_verbose
182195
# Rule 19: Datatype ID validation
183196
column_set&.column&.each do |col|
184197
if col.data&.type && !valid_datatype_id?(col.data.type)
185-
errors << { code: "INVALID_DATATYPE_ID", message: "Invalid datatype ID for column '#{col.short_name&.content}'" }
198+
errors << { code: "INVALID_DATATYPE_ID",
199+
message: "Invalid datatype ID for column '#{col.short_name&.content}'", }
186200
end
187-
end
188201

189-
# Rule 20 and 22: Complex data validation
190-
column_set&.column&.each do |col|
202+
# Rule 20 and 22: Complex data validation
191203
if col.data&.type == "*" && col.data&.datatype_library != "*"
192-
errors << { code: "INVALID_COMPLEX_DATA", message: "Invalid complex data configuration for column '#{col.short_name&.content}'" }
204+
errors << { code: "INVALID_COMPLEX_DATA",
205+
message: "Invalid complex data configuration for column '#{col.short_name&.content}'", }
193206
end
194-
end
195207

196-
# Rule 23: Language attribute validation
197-
column_set&.column&.each do |col|
208+
# Rule 23: Language attribute validation
198209
if col.data&.lang && col.data_restrictions&.lang
199-
errors << { code: "DUPLICATE_LANG_ATTRIBUTE", message: "Duplicate lang attribute for column '#{col.short_name&.content}'" }
210+
errors << { code: "DUPLICATE_LANG_ATTRIBUTE",
211+
message: "Duplicate lang attribute for column '#{col.short_name&.content}'", }
200212
end
201213
end
202214

@@ -210,17 +222,19 @@ def validate_verbose
210222
# Rule 39: ShortName whitespace check
211223
column_set&.column&.each do |col|
212224
if col.short_name&.content&.match?(/\s/)
213-
errors << { code: "INVALID_SHORT_NAME", message: "ShortName '#{col.short_name&.content}' contains whitespace" }
225+
errors << { code: "INVALID_SHORT_NAME",
226+
message: "ShortName '#{col.short_name&.content}' contains whitespace", }
214227
end
215228
end
216229

217230
# Rule 42 and 43: ComplexValue validation
218231
simple_code_list&.row&.each_with_index do |row, index|
219232
row.value.each do |value|
220-
if value.complex_value
221-
unless valid_complex_value?(value.complex_value, column_set&.column&.find { |c| c.id == value.column_ref })
222-
errors << { code: "INVALID_COMPLEX_VALUE", message: "Invalid ComplexValue in row #{index + 1}, column '#{value.column_ref}'" }
223-
end
233+
next unless value.complex_value
234+
235+
unless valid_complex_value?(value.complex_value, column_set&.column&.find { |c| c.id == value.column_ref })
236+
errors << { code: "INVALID_COMPLEX_VALUE",
237+
message: "Invalid ComplexValue in row #{index + 1}, column '#{value.column_ref}'", }
224238
end
225239
end
226240
end
@@ -237,9 +251,17 @@ def value_matches_type?(value, type)
237251
when "integer"
238252
value.to_i.to_s == value
239253
when "decimal"
240-
Float(value) rescue false
254+
begin
255+
Float(value)
256+
rescue StandardError
257+
false
258+
end
241259
when "date"
242-
Date.parse(value) rescue false
260+
begin
261+
Date.parse(value)
262+
rescue StandardError
263+
false
264+
end
243265
else
244266
true # If type is unknown, consider it valid
245267
end
@@ -250,7 +272,7 @@ def valid_uri?(uri)
250272
end
251273

252274
def valid_datatype_id?(id)
253-
["string", "token", "boolean", "decimal", "integer", "date"].include?(id)
275+
%w[string token boolean decimal integer date].include?(id)
254276
end
255277

256278
def valid_complex_value?(complex_value, column)

lib/genericode/code_list_set.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ def validate_verbose
4646

4747
# Rule 48-51: URI validations
4848
[canonical_uri, canonical_version_uri].each do |uri|
49-
unless valid_uri?(uri)
50-
errors << { code: "INVALID_URI", message: "Invalid URI: #{uri}" }
51-
end
49+
errors << { code: "INVALID_URI", message: "Invalid URI: #{uri}" } unless valid_uri?(uri)
5250
end
5351

5452
# Rule 52-53: LocationUri validation

lib/genericode/identification.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Identification < Lutaml::Model::Serializable
3434
map "LocationUri", to: :location_uri, with: { from: :location_uri_from_json, to: :location_uri_to_json }
3535
map "AlternateFormatLocationUri", to: :alternate_format_location_uri,
3636
with: { from: :alternate_format_location_uri_from_json,
37-
to: :alternate_format_location_uri_to_json }
37+
to: :alternate_format_location_uri_to_json, }
3838
map "Agency", to: :agency
3939
end
4040

@@ -51,8 +51,8 @@ def long_name_to_json(model, doc)
5151
def location_uri_from_json(model, value)
5252
# model.location_uri = Shale::Type::String.of_json(Utils.array_wrap(value))
5353
model.location_uri = Utils.array_wrap(value).map do |val|
54-
Lutaml::Model::Type::String.cast(val)
55-
end
54+
Lutaml::Model::Type::String.cast(val)
55+
end
5656
end
5757

5858
def location_uri_to_json(model, doc)

lib/genericode/json/canonical_uri_mixin.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Genericode
24
module Json
35
module CanonicalUriMixin

0 commit comments

Comments
 (0)