diff --git a/.rubocop.yml b/.rubocop.yml index bb8409c..482e793 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,32 +3,5 @@ inherit_from: .rubocop_todo.yml AllCops: NewCops: enable SuggestExtensions: false - TargetRubyVersion: 2.7 + TargetRubyVersion: 3.2 -Gemspec/DevelopmentDependencies: - Enabled: false - -Gemspec/RequireMFA: - Enabled: false - -Metrics/BlockLength: - AllowedMethods: - - describe - -Style/Documentation: - Enabled: false - -Style/StringLiterals: - EnforcedStyle: double_quotes - -Style/StringLiteralsInInterpolation: - EnforcedStyle: double_quotes - -Style/TrailingCommaInArguments: - EnforcedStyleForMultiline: consistent_comma - -Style/TrailingCommaInArrayLiteral: - EnforcedStyleForMultiline: consistent_comma - -Style/TrailingCommaInHashLiteral: - EnforcedStyleForMultiline: consistent_comma diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 321f8d6..96e9ed8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,18 +1,15 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2025-01-11 09:46:54 UTC using RuboCop version 1.70.0. +# on 2026-03-21 05:26:15 UTC using RuboCop version 1.85.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 4 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include. -# Include: **/*.gemfile, **/Gemfile, **/gems.rb -Bundler/OrderedGems: +# Offense count: 1 +Gemspec/RequiredRubyVersion: Exclude: - - 'Gemfile' + - 'genericode.gemspec' # Offense count: 1 # Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches, IgnoreDuplicateElseBranch. @@ -22,7 +19,7 @@ Lint/DuplicateBranch: # Offense count: 1 # This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AutoCorrect, CheckForMethodsWithNoSideEffects. +# Configuration parameters: CheckForMethodsWithNoSideEffects. Lint/Void: Exclude: - 'lib/genericode/code_list.rb' @@ -32,18 +29,18 @@ Lint/Void: Metrics/AbcSize: Max: 182 -# Offense count: 2 +# Offense count: 16 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. # AllowedMethods: refine Metrics/BlockLength: - Max: 34 + Max: 160 # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: Max: 216 -# Offense count: 5 +# Offense count: 6 # Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: Max: 92 @@ -58,9 +55,36 @@ Metrics/MethodLength: Metrics/PerceivedComplexity: Max: 92 +# Offense count: 2 +# Configuration parameters: Mode, AllowedMethods, AllowedPatterns, AllowBangMethods, WaywardPredicates. +# AllowedMethods: call +# WaywardPredicates: infinite?, nonzero? +Naming/PredicateMethod: + Exclude: + - 'lib/genericode/cli/converter.rb' + - 'lib/genericode/cli/validator.rb' + +# Offense count: 37 +# Configuration parameters: AllowedConstants. +Style/Documentation: + Enabled: false + +# Offense count: 53 +# This cop supports safe autocorrection (--autocorrect). +# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline. +# SupportedStyles: single_quotes, double_quotes +Style/StringLiterals: + Exclude: + - 'genericode.gemspec' + - 'lib/genericode/cli/code_lister.rb' + - 'lib/genericode/cli/code_lookup.rb' + - 'lib/genericode/cli/commands.rb' + - 'lib/genericode/cli/converter.rb' + - 'lib/genericode/cli/validator.rb' + # Offense count: 2 # This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings. +# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, AllowRBSInlineAnnotation, AllowCopDirectives, AllowedPatterns, SplitStrings. # URISchemes: http, https Layout/LineLength: Max: 121 diff --git a/Gemfile b/Gemfile index 93dfa2a..085398f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,13 +1,14 @@ # frozen_string_literal: true -source "https://rubygems.org" +source 'https://rubygems.org' # Specify your gem's dependencies in genericode.gemspec gemspec -gem "nokogiri" -gem "rake", "~> 13.0" -gem "rspec", "~> 3.0" -gem "rubocop" -gem "rubocop-performance" -gem "xml-c14n" +gem 'canon' +gem 'lutaml-model', github: 'lutaml/lutaml-model', ref: 'main' +gem 'nokogiri' +gem 'rake', '~> 13.0' +gem 'rspec', '~> 3.0' +gem 'rubocop' +gem 'rubocop-performance' diff --git a/Rakefile b/Rakefile index cca7175..4964751 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,11 @@ # frozen_string_literal: true -require "bundler/gem_tasks" -require "rspec/core/rake_task" +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) -require "rubocop/rake_task" +require 'rubocop/rake_task' RuboCop::RakeTask.new diff --git a/bin/console b/bin/console index 8c0e57f..3caee4a 100755 --- a/bin/console +++ b/bin/console @@ -1,11 +1,11 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require "bundler/setup" -require "genericode" +require 'bundler/setup' +require 'genericode' # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. -require "irb" +require 'irb' IRB.start(__FILE__) diff --git a/exe/genericode b/exe/genericode index 57a665c..813d743 100755 --- a/exe/genericode +++ b/exe/genericode @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require_relative "../lib/genericode" -require_relative "../lib/genericode/cli" +require_relative '../lib/genericode' +require_relative '../lib/genericode/cli' Genericode::Cli::Commands.start(ARGV) diff --git a/genericode.gemspec b/genericode.gemspec index db15151..9abcfb0 100644 --- a/genericode.gemspec +++ b/genericode.gemspec @@ -1,21 +1,22 @@ # frozen_string_literal: true -require_relative "lib/genericode/version" +require_relative 'lib/genericode/version' Gem::Specification.new do |spec| - spec.name = "genericode" + spec.name = 'genericode' spec.version = Genericode::VERSION - spec.authors = ["Ribose Inc."] + spec.authors = ['Ribose Inc.'] spec.email = ["open.source@ribose.com'"] - spec.summary = "Parser and generator for OASIS Genericode" - spec.description = "Parser and generator for OASIS Genericode" - spec.homepage = "https://github.com/lutaml/genericode" - spec.required_ruby_version = ">= 2.7.0" + spec.summary = 'Parser and generator for OASIS Genericode' + spec.description = 'Parser and generator for OASIS Genericode' + spec.homepage = 'https://github.com/lutaml/genericode' + spec.required_ruby_version = '>= 2.7.0' - spec.metadata["homepage_uri"] = spec.homepage - spec.metadata["source_code_uri"] = spec.homepage - spec.metadata["changelog_uri"] = "https://github.com/lutaml/genericode/releases" + spec.metadata['homepage_uri'] = spec.homepage + spec.metadata['source_code_uri'] = spec.homepage + spec.metadata['changelog_uri'] = 'https://github.com/lutaml/genericode/releases' + spec.metadata['rubygems_mfa_required'] = 'true' gemspec = File.basename(__FILE__) spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls| @@ -24,12 +25,12 @@ Gem::Specification.new do |spec| f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile]) end end - spec.bindir = "exe" + spec.bindir = 'exe' spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_dependency "csv" - spec.add_dependency "lutaml-model", "~> 0.7" - spec.add_dependency "tabulo" - spec.add_dependency "thor" + spec.add_dependency 'csv' + spec.add_dependency 'lutaml-model', '~>0.8.0' + spec.add_dependency 'table_tennis', '~>0.0.7' + spec.add_dependency 'thor' end diff --git a/lib/genericode.rb b/lib/genericode.rb index ec96a81..2f258c1 100644 --- a/lib/genericode.rb +++ b/lib/genericode.rb @@ -1,14 +1,13 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' Lutaml::Model::Config.configure do |config| - require "lutaml/model/xml_adapter/nokogiri_adapter" - config.xml_adapter = Lutaml::Model::XmlAdapter::NokogiriAdapter + config.xml_adapter_type = :nokogiri end -require_relative "genericode/version" -require_relative "genericode/code_list" +require_relative 'genericode/version' +require_relative 'genericode/code_list' module Genericode class Error < StandardError; end diff --git a/lib/genericode/agency.rb b/lib/genericode/agency.rb index f051d9c..681af30 100644 --- a/lib/genericode/agency.rb +++ b/lib/genericode/agency.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "general_identifier" -require_relative "long_name" -require_relative "short_name" -require_relative "json/short_name_mixin" -require_relative "utils" +require_relative 'general_identifier' +require_relative 'long_name' +require_relative 'short_name' +require_relative 'json/short_name_mixin' +require_relative 'utils' module Genericode class Agency < Lutaml::Model::Serializable @@ -17,9 +17,9 @@ class Agency < Lutaml::Model::Serializable attribute :identifier, GeneralIdentifier, collection: true json do - map "ShortName", to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } - map "LongName", to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } - map "Identifier", to: :identifier, with: { from: :identifier_from_json, to: :identifier_to_json } + map 'ShortName', to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } + map 'LongName', to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } + map 'Identifier', to: :identifier, with: { from: :identifier_from_json, to: :identifier_to_json } end def long_name_from_json(model, value) @@ -27,7 +27,7 @@ def long_name_from_json(model, value) end def long_name_to_json(model, doc) - doc["LongName"] = LongName.as_json(Utils.one_or_all(model.long_name)) + doc['LongName'] = LongName.as_json(Utils.one_or_all(model.long_name)) end def identifier_from_json(model, value) @@ -35,16 +35,15 @@ def identifier_from_json(model, value) end def identifier_to_json(model, doc) - doc["Identifier"] = GeneralIdentifier.as_json(Utils.one_or_all(model.identifier)) + doc['Identifier'] = GeneralIdentifier.as_json(Utils.one_or_all(model.identifier)) end xml do - root "Agency" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Agency' - map_element "ShortName", to: :short_name, prefix: nil, namespace: nil - map_element "LongName", to: :long_name, prefix: nil, namespace: nil - map_element "Identifier", to: :identifier, prefix: nil, namespace: nil + map_element 'ShortName', to: :short_name + map_element 'LongName', to: :long_name + map_element 'Identifier', to: :identifier end end end diff --git a/lib/genericode/annotation.rb b/lib/genericode/annotation.rb index 754226d..7ea4033 100644 --- a/lib/genericode/annotation.rb +++ b/lib/genericode/annotation.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "any_other_content" -require_relative "any_other_language_content" +require_relative 'any_other_content' +require_relative 'any_other_language_content' module Genericode class Annotation < Lutaml::Model::Serializable @@ -11,22 +11,21 @@ class Annotation < Lutaml::Model::Serializable attribute :app_info, AnyOtherContent json do - map "Description", to: :description - map "AppInfo", to: :app_info, render_nil: true + map 'Description', to: :description + map 'AppInfo', to: :app_info, render_nil: true end def self.of_json(hash, **) - hash = { "AppInfo" => hash } if hash.any? + hash = { 'AppInfo' => hash } if hash.any? super end xml do - root "Annotation" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Annotation' - map_element "Description", to: :description, prefix: nil, namespace: nil - map_element "AppInfo", to: :app_info, prefix: nil, namespace: nil, value_map: { to: { nil: :empty } } + map_element 'Description', to: :description + map_element 'AppInfo', to: :app_info, value_map: { to: { nil: :empty } } end end end diff --git a/lib/genericode/any_other_content.rb b/lib/genericode/any_other_content.rb index 2e1630d..f6485e0 100644 --- a/lib/genericode/any_other_content.rb +++ b/lib/genericode/any_other_content.rb @@ -1,12 +1,11 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' module Genericode class AnyOtherContent < Lutaml::Model::Serializable xml do - root "AnyOtherContent" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'AnyOtherContent' end end end diff --git a/lib/genericode/any_other_language_content.rb b/lib/genericode/any_other_language_content.rb index 5971f66..a08f923 100644 --- a/lib/genericode/any_other_language_content.rb +++ b/lib/genericode/any_other_language_content.rb @@ -1,20 +1,20 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' +require 'lutaml/xml/w3c' module Genericode class AnyOtherLanguageContent < Lutaml::Model::Serializable - attribute :lang, :string + attribute :lang, Lutaml::Xml::W3c::XmlLangType json do - map "lang", to: :lang + map 'lang', to: :lang end xml do - root "AnyOtherLanguageContent" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'AnyOtherLanguageContent' - map_attribute "lang", to: :lang, prefix: "xml", namespace: "http://www.w3.org/XML/1998/namespace" + map_attribute 'lang', to: :lang end end end diff --git a/lib/genericode/canonical_uri.rb b/lib/genericode/canonical_uri.rb index 8e393fd..bdd92ad 100644 --- a/lib/genericode/canonical_uri.rb +++ b/lib/genericode/canonical_uri.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require "lutaml/model" -require "uri" +require 'lutaml/model' +require 'uri' module Genericode # Rule 4: Must be an absolute URI, must not be relative @@ -9,7 +9,7 @@ class CanonicalUri < Lutaml::Model::Serializable attribute :content, :string xml do - root "CanonicalUri" + element 'CanonicalUri' map_content to: :content end diff --git a/lib/genericode/cli.rb b/lib/genericode/cli.rb index 3e897f5..7129cdb 100644 --- a/lib/genericode/cli.rb +++ b/lib/genericode/cli.rb @@ -5,4 +5,4 @@ module Cli end end -require_relative "cli/commands" +require_relative 'cli/commands' diff --git a/lib/genericode/cli/code_lister.rb b/lib/genericode/cli/code_lister.rb index 5769731..4f4d3ed 100644 --- a/lib/genericode/cli/code_lister.rb +++ b/lib/genericode/cli/code_lister.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require_relative "../code_list" -require "tabulo" -require "csv" +require_relative '../code_list' +require 'table_tennis' +require 'csv' module Genericode module Cli @@ -13,10 +13,16 @@ def list_codes(file_path, format: :tsv) # Validate data types code_list.validate_verbose.each do |error| - raise Error, "#{error[:code]}: #{error[:message]}" if error[:code] == "INVALID_DATA_TYPE" + if error[:code] == 'INVALID_DATA_TYPE' + raise Error, + "#{error[:code]}: #{error[:message]}" + end # Ensure valid ColumnRefs - raise Error, "#{error[:code]}: #{error[:message]}" if error[:code] == "INVALID_COLUMN_REF" + if error[:code] == 'INVALID_COLUMN_REF' + raise Error, + "#{error[:code]}: #{error[:message]}" + end end case format @@ -40,26 +46,25 @@ def list_tsv(code_list) rows.each do |row| csv << columns.map do |col| value = row.value.find { |v| v.column_ref == col.id } - value&.simple_value&.content || "" + value&.simple_value&.content || '' end end - end.strip.encode("UTF-8") + end.strip.encode('UTF-8') end def list_table(code_list) columns = code_list.column_set.column rows = code_list.simple_code_list.row - table = Tabulo::Table.new(rows) do |t| - columns.each do |column| - t.add_column(column.short_name.content) do |row| - value = row.value.find { |v| v.column_ref == column.id } - value&.simple_value&.content || "" - end + headers = columns.map { |col| col.short_name.content } + data = rows.map do |row| + columns.map do |col| + value = row.value.find { |v| v.column_ref == col.id } + value&.simple_value&.content || '' end end - table.to_s + TableTennis.render([headers] + data) end end end diff --git a/lib/genericode/cli/code_lookup.rb b/lib/genericode/cli/code_lookup.rb index 73d7144..b144242 100644 --- a/lib/genericode/cli/code_lookup.rb +++ b/lib/genericode/cli/code_lookup.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative "../code_list" +require_relative '../code_list' module Genericode module Cli diff --git a/lib/genericode/cli/commands.rb b/lib/genericode/cli/commands.rb index 269b31b..3d83238 100644 --- a/lib/genericode/cli/commands.rb +++ b/lib/genericode/cli/commands.rb @@ -1,52 +1,55 @@ # frozen_string_literal: true -require "thor" -require_relative "validator" -require_relative "converter" -require_relative "code_lister" -require_relative "code_lookup" +require 'thor' +require_relative 'validator' +require_relative 'converter' +require_relative 'code_lister' +require_relative 'code_lookup' module Genericode module Cli class Commands < Thor - desc "convert INPUT OUTPUT", "Convert between Genericode XML and JSON formats" + desc 'convert INPUT OUTPUT', + 'Convert between Genericode XML and JSON formats' def convert(input, output) - puts "Conversion successful." if Converter.convert(input, output) + puts 'Conversion successful.' if Converter.convert(input, output) rescue Error => e puts "Conversion failed: #{e.message}" end - desc "validate FILE", "Validate a Genericode file" - option :verbose, type: :boolean, desc: "Show detailed validation results" + desc 'validate FILE', 'Validate a Genericode file' + option :verbose, type: :boolean, desc: 'Show detailed validation results' def validate(file) code_list = CodeList.from_file(file) if options[:verbose] results = code_list.validate_verbose if results.empty? - puts "File is valid." + puts 'File is valid.' else - puts "File is invalid. Issues found:" + puts 'File is invalid. Issues found:' results.each do |error| puts " [#{error[:code]}] #{error[:message]}" end end elsif code_list.valid? - puts "File is valid." + puts 'File is valid.' else - puts "File is invalid." + puts 'File is invalid.' end rescue Error => e puts "Validation failed: #{e.message}" end - desc "list_codes FILE", "List all codes and their associated data in a Genericode file" - option :format, type: :string, default: "tsv", enum: %w[tsv table], desc: "Output format (tsv or table)" - option :output, type: :string, desc: "Output file path (default: stdout)" + desc 'list_codes FILE', + 'List all codes and their associated data in a Genericode file' + option :format, type: :string, default: 'tsv', enum: %w[tsv table], + desc: 'Output format (tsv or table)' + option :output, type: :string, desc: 'Output file path (default: stdout)' def list_codes(file) - format = (options[:format] || "tsv").to_sym + format = (options[:format] || 'tsv').to_sym result = CodeLister.list_codes(file, format: format) if options[:output] @@ -59,7 +62,7 @@ def list_codes(file) puts "Listing codes failed: #{e.message}" end - desc "lookup FILE PATH", "Look up a particular code using Genericode path" + desc 'lookup FILE PATH', 'Look up a particular code using Genericode path' def lookup(file, path) result = CodeLookup.lookup(file, path) diff --git a/lib/genericode/cli/converter.rb b/lib/genericode/cli/converter.rb index 33d315e..4c34e18 100644 --- a/lib/genericode/cli/converter.rb +++ b/lib/genericode/cli/converter.rb @@ -7,14 +7,20 @@ def self.convert(input_path, output_path) input_format = File.extname(input_path) output_format = File.extname(output_path) - raise Error, "Invalid input format" unless [".gc", ".gcj"].include?(input_format) - raise Error, "Invalid output format" unless [".gc", ".gcj"].include?(output_format) - raise Error, "Input and output formats are the same" if input_format == output_format + raise Error, 'Invalid input format' unless ['.gc', + '.gcj'].include?(input_format) + raise Error, 'Invalid output format' unless ['.gc', + '.gcj'].include?(output_format) + + if input_format == output_format + raise Error, + 'Input and output formats are the same' + end # begin code_list = CodeList.from_file(input_path) - result = if output_format == ".gcj" + result = if output_format == '.gcj' code_list.to_json else code_list.to_xml diff --git a/lib/genericode/cli/validator.rb b/lib/genericode/cli/validator.rb index fe731d2..f4c06a4 100644 --- a/lib/genericode/cli/validator.rb +++ b/lib/genericode/cli/validator.rb @@ -4,15 +4,22 @@ module Genericode module Cli class Validator def self.validate(file_path) - raise Error, "File does not exist" unless File.exist?(file_path) - raise Error, "Invalid file format" unless file_path.end_with?(".gc", ".gcj") + raise Error, 'File does not exist' unless File.exist?(file_path) + raise Error, 'Invalid file format' unless file_path.end_with?('.gc', + '.gcj') code_list = CodeList.from_file(file_path) - raise Error, "No columns defined" if code_list.column_set.nil? || code_list.column_set.column.empty? - raise Error, "No rows defined" if code_list.simple_code_list.nil? || code_list.simple_code_list.row.empty? + if code_list.column_set.nil? || code_list.column_set.column.empty? + raise Error, + 'No columns defined' + end + if code_list.simple_code_list.nil? || code_list.simple_code_list.row.empty? + raise Error, + 'No rows defined' + end - raise Error, "Invalid Genericode structure" unless code_list.valid? + raise Error, 'Invalid Genericode structure' unless code_list.valid? true end diff --git a/lib/genericode/code_list.rb b/lib/genericode/code_list.rb index da22c2f..345f0c2 100644 --- a/lib/genericode/code_list.rb +++ b/lib/genericode/code_list.rb @@ -1,13 +1,14 @@ # frozen_string_literal: true -require "lutaml/model" -require "uri" +require 'lutaml/model' +require_relative 'gc_namespace' +require 'uri' -require_relative "annotation" -require_relative "column_set" -require_relative "column_set_ref" -require_relative "identification" -require_relative "simple_code_list" +require_relative 'annotation' +require_relative 'column_set' +require_relative 'column_set_ref' +require_relative 'identification' +require_relative 'simple_code_list' module Genericode class CodeList < Lutaml::Model::Serializable @@ -19,30 +20,30 @@ class CodeList < Lutaml::Model::Serializable def self.from_file(file_path) content = File.read(file_path) - if file_path.end_with?(".gc") + if file_path.end_with?('.gc') from_xml(content) - elsif file_path.end_with?(".gcj") + elsif file_path.end_with?('.gcj') from_json(content) else - raise Error, "Unsupported file format. Expected .gc or .gcj file." + raise Error, 'Unsupported file format. Expected .gc or .gcj file.' end end json do - map "Annotation", to: :annotation - map "Identification", to: :identification - map "Columns", to: :column_set, with: { from: :column_set_from_json, to: :column_set_to_json } - map "ColumnSetRef", to: :column_set_ref - map "Keys", to: :key, delegate: :column_set, with: { from: :key_from_json, to: :key_to_json } - map "Codes", to: :simple_code_list, with: { from: :simple_code_list_from_json, to: :simple_code_list_to_json } + map 'Annotation', to: :annotation + map 'Identification', to: :identification + map 'Columns', to: :column_set, with: { from: :column_set_from_json, to: :column_set_to_json } + map 'ColumnSetRef', to: :column_set_ref + map 'Keys', to: :key, delegate: :column_set, with: { from: :key_from_json, to: :key_to_json } + map 'Codes', to: :simple_code_list, with: { from: :simple_code_list_from_json, to: :simple_code_list_to_json } end def column_set_from_json(model, value) - model.column_set = ColumnSet.of_json({ "Column" => value }) + model.column_set = ColumnSet.of_json({ 'Column' => value }) end def column_set_to_json(model, doc) - doc["Columns"] = Column.as_json(model.column_set.column) + doc['Columns'] = Column.as_json(model.column_set.column) end def key_from_json(model, value) @@ -50,7 +51,7 @@ def key_from_json(model, value) end def key_to_json(model, doc) - doc["Keys"] = Key.as_json(model.column_set.key) + doc['Keys'] = Key.as_json(model.column_set.key) end def simple_code_list_from_json(model, value) @@ -66,25 +67,25 @@ def simple_code_list_from_json(model, value) end def simple_code_list_to_json(model, doc) - doc["Codes"] = model.simple_code_list.row.map do |row| + doc['Codes'] = model.simple_code_list.row.map do |row| row.value.to_h { |v| [v.column_ref, v.simple_value.content] } end end xml do - root "CodeList" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" - - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Identification", to: :identification, prefix: nil, namespace: nil - map_element "ColumnSet", to: :column_set, prefix: nil, namespace: nil - map_element "ColumnSetRef", to: :column_set_ref, prefix: nil, namespace: nil - map_element "SimpleCodeList", to: :simple_code_list, prefix: nil, namespace: nil + namespace GcNamespace + element 'CodeList' + + map_element 'Annotation', to: :annotation + map_element 'Identification', to: :identification + map_element 'ColumnSet', to: :column_set + map_element 'ColumnSetRef', to: :column_set_ref + map_element 'SimpleCodeList', to: :simple_code_list end def lookup(path) - parts = path.split(">") - conditions = parts[0].split(",").to_h { |c| c.split(":") } + parts = path.split('>') + conditions = parts[0].split(',').to_h { |c| c.split(':') } target_column = parts[1] result = simple_code_list.row.find do |row| @@ -108,7 +109,7 @@ def lookup(path) result.value.to_h do |v| [column_set.column.find do |c| c.id == v.column_ref - end.short_name.content, v.simple_value.content,] + end.short_name.content, v.simple_value.content] end end end @@ -122,28 +123,28 @@ def validate_verbose # Rule 1: ColumnSet presence if column_set.nil? || column_set.column.empty? - errors << { code: "MISSING_COLUMN_SET", - message: "ColumnSet is missing or empty", } + errors << { code: 'MISSING_COLUMN_SET', + message: 'ColumnSet is missing or empty' } end # Rule 2: SimpleCodeList presence if simple_code_list.nil? || simple_code_list.row.empty? - errors << { code: "MISSING_SIMPLE_CODE_LIST", - message: "SimpleCodeList is missing or empty", } + errors << { code: 'MISSING_SIMPLE_CODE_LIST', + message: 'SimpleCodeList is missing or empty' } end # Rule 3: Unique column IDs column_ids = column_set&.column&.map(&:id) || [] if column_ids.uniq.length != column_ids.length - errors << { code: "DUPLICATE_COLUMN_IDS", message: "Duplicate column IDs found" } + errors << { code: 'DUPLICATE_COLUMN_IDS', message: 'Duplicate column IDs found' } end # Rule 4: Verify ColumnRef values simple_code_list&.row&.each_with_index do |row, index| row.value.each do |value| unless column_ids.include?(value.column_ref) - errors << { code: "INVALID_COLUMN_REF", - message: "Invalid ColumnRef '#{value.column_ref}' in row #{index + 1}", } + errors << { code: 'INVALID_COLUMN_REF', + message: "Invalid ColumnRef '#{value.column_ref}' in row #{index + 1}" } end end end @@ -155,17 +156,17 @@ def validate_verbose end || []).compact if column_values.uniq.length != column_values.length - errors << { code: "DUPLICATE_VALUES", message: "Duplicate values found in column '#{col.id}'" } + errors << { code: 'DUPLICATE_VALUES', message: "Duplicate values found in column '#{col.id}'" } end end # Rule 6: Required column values - required_columns = column_set&.column&.select { |col| col.use == "required" } || [] + required_columns = column_set&.column&.select { |col| col.use == 'required' } || [] simple_code_list&.row&.each_with_index do |row, index| required_columns.each do |col| unless row.value.any? { |v| v.column_ref == col.id && v.simple_value&.content } - errors << { code: "MISSING_REQUIRED_VALUE", - message: "Missing value for required column '#{col.short_name&.content}' in row #{index + 1}", } + errors << { code: 'MISSING_REQUIRED_VALUE', + message: "Missing value for required column '#{col.short_name&.content}' in row #{index + 1}" } end end end @@ -176,49 +177,49 @@ def validate_verbose simple_code_list&.row&.each_with_index do |row, index| value = row.value.find { |v| v.column_ref == col.id }&.simple_value&.content unless value_matches_type?(value, data_type) - errors << { code: "INVALID_DATA_TYPE", - message: "Invalid data type for column '#{col.short_name&.content}' in row #{index + 1}", } + errors << { code: 'INVALID_DATA_TYPE', + message: "Invalid data type for column '#{col.short_name&.content}' in row #{index + 1}" } end end end # Rule 8: Valid canonical URIs if identification&.canonical_uri && !valid_uri?(identification.canonical_uri) - errors << { code: "INVALID_CANONICAL_URI", message: "Invalid canonical URI" } + errors << { code: 'INVALID_CANONICAL_URI', message: 'Invalid canonical URI' } end # Rule 19: Datatype ID validation column_set&.column&.each do |col| if col.data&.type && !valid_datatype_id?(col.data.type) - errors << { code: "INVALID_DATATYPE_ID", - message: "Invalid datatype ID for column '#{col.short_name&.content}'", } + errors << { code: 'INVALID_DATATYPE_ID', + message: "Invalid datatype ID for column '#{col.short_name&.content}'" } end # Rule 20 and 22: Complex data validation - if col.data&.type == "*" && col.data&.datatype_library != "*" - errors << { code: "INVALID_COMPLEX_DATA", - message: "Invalid complex data configuration for column '#{col.short_name&.content}'", } + if col.data&.type == '*' && col.data&.datatype_library != '*' + errors << { code: 'INVALID_COMPLEX_DATA', + message: "Invalid complex data configuration for column '#{col.short_name&.content}'" } end # Rule 23: Language attribute validation if col.data&.lang && col.data_restrictions&.lang - errors << { code: "DUPLICATE_LANG_ATTRIBUTE", - message: "Duplicate lang attribute for column '#{col.short_name&.content}'", } + errors << { code: 'DUPLICATE_LANG_ATTRIBUTE', + message: "Duplicate lang attribute for column '#{col.short_name&.content}'" } end end # Rule 38: Implicit column reference simple_code_list&.row&.each_with_index do |row, index| unless row.value.all?(&:column_ref) - errors << { code: "MISSING_COLUMN_REF", message: "Missing explicit column reference in row #{index + 1}" } + errors << { code: 'MISSING_COLUMN_REF', message: "Missing explicit column reference in row #{index + 1}" } end end # Rule 39: ShortName whitespace check column_set&.column&.each do |col| if col.short_name&.content&.match?(/\s/) - errors << { code: "INVALID_SHORT_NAME", - message: "ShortName '#{col.short_name&.content}' contains whitespace", } + errors << { code: 'INVALID_SHORT_NAME', + message: "ShortName '#{col.short_name&.content}' contains whitespace" } end end @@ -228,8 +229,8 @@ def validate_verbose next unless value.complex_value unless valid_complex_value?(value.complex_value, column_set&.column&.find { |c| c.id == value.column_ref }) - errors << { code: "INVALID_COMPLEX_VALUE", - message: "Invalid ComplexValue in row #{index + 1}, column '#{value.column_ref}'", } + errors << { code: 'INVALID_COMPLEX_VALUE', + message: "Invalid ComplexValue in row #{index + 1}, column '#{value.column_ref}'" } end end end @@ -241,17 +242,17 @@ def validate_verbose def value_matches_type?(value, type) case type - when "string" + when 'string' true # All values can be considered strings - when "integer" + when 'integer' value.to_i.to_s == value - when "decimal" + when 'decimal' begin Float(value) rescue StandardError false end - when "date" + when 'date' begin Date.parse(value) rescue StandardError @@ -273,14 +274,14 @@ def valid_datatype_id?(id) def valid_complex_value?(complex_value, column) return true unless complex_value && column&.data - if column.data.type == "*" + if column.data.type == '*' true # Any element is allowed else # Check if the root element name matches the datatype ID complex_value.name == column.data.type end - if column.data.datatype_library == "*" + if column.data.datatype_library == '*' true # Any namespace is allowed else # Check if the namespace matches the datatype library diff --git a/lib/genericode/code_list_ref.rb b/lib/genericode/code_list_ref.rb index 631e122..4121361 100644 --- a/lib/genericode/code_list_ref.rb +++ b/lib/genericode/code_list_ref.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "json/canonical_uri_mixin" +require_relative 'annotation' +require_relative 'json/canonical_uri_mixin' module Genericode class CodeListRef < Lutaml::Model::Serializable @@ -15,20 +15,19 @@ class CodeListRef < Lutaml::Model::Serializable attribute :location_uri, :string, collection: true json do - map "Annotation", to: :annotation - map "CanonicalUri", to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri + map 'Annotation', to: :annotation + map 'CanonicalUri', to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri end xml do - root "CodeListRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'CodeListRef' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "CanonicalUri", to: :canonical_uri, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'CanonicalUri', to: :canonical_uri + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri end end end diff --git a/lib/genericode/code_list_set.rb b/lib/genericode/code_list_set.rb index 3c5c912..9d4af1c 100644 --- a/lib/genericode/code_list_set.rb +++ b/lib/genericode/code_list_set.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "code_list_ref" -require_relative "code_list_set_ref" -require_relative "identification" +require_relative 'annotation' +require_relative 'code_list_ref' +require_relative 'code_list_set_ref' +require_relative 'identification' module Genericode class CodeListSet < Lutaml::Model::Serializable @@ -16,22 +16,21 @@ class CodeListSet < Lutaml::Model::Serializable attribute :code_list_set_ref, CodeListSetRef, collection: true json do - map "Annotation", to: :annotation - map "Identification", to: :identification - map "CodeListRef", to: :code_list_ref - map "CodeListSet", to: :code_list_set - map "CodeListSetRef", to: :code_list_set_ref + map 'Annotation', to: :annotation + map 'Identification', to: :identification + map 'CodeListRef', to: :code_list_ref + map 'CodeListSet', to: :code_list_set + map 'CodeListSetRef', to: :code_list_set_ref end xml do - root "CodeListSet" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'CodeListSet' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Identification", to: :identification, prefix: nil, namespace: nil - map_element "CodeListRef", to: :code_list_ref, prefix: nil, namespace: nil - map_element "CodeListSet", to: :code_list_set, prefix: nil, namespace: nil - map_element "CodeListSetRef", to: :code_list_set_ref, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'Identification', to: :identification + map_element 'CodeListRef', to: :code_list_ref + map_element 'CodeListSet', to: :code_list_set + map_element 'CodeListSetRef', to: :code_list_set_ref end def validate_verbose @@ -40,19 +39,19 @@ def validate_verbose # Rule 47: CodeListSet reference validation code_list_set_ref&.each do |ref| unless valid_uri?(ref.canonical_uri) && valid_uri?(ref.canonical_version_uri) - errors << { code: "INVALID_CODELIST_SET_REF", message: "Invalid CodeListSet reference URI" } + errors << { code: 'INVALID_CODELIST_SET_REF', message: 'Invalid CodeListSet reference URI' } end end # Rule 48-51: URI validations [canonical_uri, canonical_version_uri].each do |uri| - errors << { code: "INVALID_URI", message: "Invalid URI: #{uri}" } unless valid_uri?(uri) + errors << { code: 'INVALID_URI', message: "Invalid URI: #{uri}" } unless valid_uri?(uri) end # Rule 52-53: LocationUri validation location_uri&.each do |uri| unless valid_genericode_uri?(uri) - errors << { code: "INVALID_LOCATION_URI", message: "Invalid LocationUri: #{uri}" } + errors << { code: 'INVALID_LOCATION_URI', message: "Invalid LocationUri: #{uri}" } end end @@ -68,7 +67,7 @@ def valid_uri?(uri) def valid_genericode_uri?(uri) # Add logic to check if the URI points to a valid genericode document # This might involve making an HTTP request or checking file extensions - uri.end_with?(".gc", ".gcj") + uri.end_with?('.gc', '.gcj') end end end diff --git a/lib/genericode/code_list_set_ref.rb b/lib/genericode/code_list_set_ref.rb index a73ec41..12a7a08 100644 --- a/lib/genericode/code_list_set_ref.rb +++ b/lib/genericode/code_list_set_ref.rb @@ -1,34 +1,34 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "canonical_uri" -require_relative "json/canonical_uri_mixin" +require_relative 'annotation' +require_relative 'canonical_uri' +require_relative 'json/canonical_uri_mixin' module Genericode class CodeListSetRef < Lutaml::Model::Serializable include Json::CanonicalUriMixin + attribute :annotation, Annotation attribute :canonical_uri, CanonicalUri attribute :canonical_version_uri, :string attribute :location_uri, :string, collection: true json do - map "Annotation", to: :annotation - map "CanonicalUri", to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri + map 'Annotation', to: :annotation + map 'CanonicalUri', to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri end xml do - root "CodeListSetRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'CodeListSetRef' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "CanonicalUri", to: :canonical_uri, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'CanonicalUri', to: :canonical_uri + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri end end end diff --git a/lib/genericode/column.rb b/lib/genericode/column.rb index b09f014..90cd402 100644 --- a/lib/genericode/column.rb +++ b/lib/genericode/column.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "data" -require_relative "canonical_uri" -require_relative "long_name" -require_relative "short_name" -require_relative "json/short_name_mixin" -require_relative "json/canonical_uri_mixin" -require_relative "utils" +require_relative 'annotation' +require_relative 'data' +require_relative 'canonical_uri' +require_relative 'long_name' +require_relative 'short_name' +require_relative 'json/short_name_mixin' +require_relative 'json/canonical_uri_mixin' +require_relative 'utils' module Genericode class Column < Lutaml::Model::Serializable @@ -17,7 +17,7 @@ class Column < Lutaml::Model::Serializable include Json::ShortNameMixin attribute :id, :string - attribute :use, :string, default: -> { "optional" } + attribute :use, :string, default: -> { 'optional' } attribute :annotation, Annotation attribute :short_name, ShortName attribute :long_name, LongName, collection: true, initialize_empty: true @@ -26,23 +26,23 @@ class Column < Lutaml::Model::Serializable attribute :data, Data json do - map "Required", to: :use, with: { from: :use_from_json, to: :use_to_json } - map "Id", to: :id - map "Annotation", to: :annotation - map "ShortName", to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } - map "LongName", to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } - map "CanonicalUri", to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } - map "CanonicalVersionUri", to: :canonical_version_uri - map "DataType", to: :type, delegate: :data - map "DataLanguage", to: :lang, delegate: :data + map 'Required', to: :use, with: { from: :use_from_json, to: :use_to_json } + map 'Id', to: :id + map 'Annotation', to: :annotation + map 'ShortName', to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } + map 'LongName', to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } + map 'CanonicalUri', to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'DataType', to: :type, delegate: :data + map 'DataLanguage', to: :lang, delegate: :data end def use_from_json(model, value) - model.use = value == "true" ? "required" : "optional" + model.use = value == 'true' ? 'required' : 'optional' end def use_to_json(model, doc) - doc["Required"] = "true" if model.use == "required" + doc['Required'] = 'true' if model.use == 'required' end def long_name_from_json(model, value) @@ -52,21 +52,20 @@ def long_name_from_json(model, value) def long_name_to_json(model, doc) return if model.long_name.nil? || model.long_name.empty? - doc["LongName"] = LongName.as_json(Utils.one_or_all(model.long_name)) + doc['LongName'] = LongName.as_json(Utils.one_or_all(model.long_name)) end xml do - root "Column" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Column' - map_attribute "Id", to: :id - map_attribute "Use", to: :use - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "ShortName", to: :short_name, prefix: nil, namespace: nil - map_element "LongName", to: :long_name, prefix: nil, namespace: nil - map_element "CanonicalUri", to: :canonical_uri, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "Data", to: :data, prefix: nil, namespace: nil + map_attribute 'Id', to: :id + map_attribute 'Use', to: :use + map_element 'Annotation', to: :annotation + map_element 'ShortName', to: :short_name + map_element 'LongName', to: :long_name + map_element 'CanonicalUri', to: :canonical_uri + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'Data', to: :data end end end diff --git a/lib/genericode/column_ref.rb b/lib/genericode/column_ref.rb index 63bc0ec..1f1e671 100644 --- a/lib/genericode/column_ref.rb +++ b/lib/genericode/column_ref.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "data_restrictions" +require_relative 'annotation' +require_relative 'data_restrictions' module Genericode class ColumnRef < Lutaml::Model::Serializable @@ -16,26 +16,25 @@ class ColumnRef < Lutaml::Model::Serializable attribute :data, DataRestrictions json do - map "Id", to: :id - map "ExternalRef", to: :external_ref - map "Use", to: :use - map "Annotation", to: :annotation - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri - map "Data", to: :data + map 'Id', to: :id + map 'ExternalRef', to: :external_ref + map 'Use', to: :use + map 'Annotation', to: :annotation + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri + map 'Data', to: :data end xml do - root "ColumnRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'ColumnRef' - map_attribute "Id", to: :id - map_attribute "ExternalRef", to: :external_ref - map_attribute "Use", to: :use - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil - map_element "Data", to: :data, prefix: nil, namespace: nil + map_attribute 'Id', to: :id + map_attribute 'ExternalRef', to: :external_ref + map_attribute 'Use', to: :use + map_element 'Annotation', to: :annotation + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri + map_element 'Data', to: :data end end end diff --git a/lib/genericode/column_set.rb b/lib/genericode/column_set.rb index be71f49..5a248cd 100644 --- a/lib/genericode/column_set.rb +++ b/lib/genericode/column_set.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "column" -require_relative "column_ref" -require_relative "identification" -require_relative "key" -require_relative "key_ref" +require_relative 'annotation' +require_relative 'column' +require_relative 'column_ref' +require_relative 'identification' +require_relative 'key' +require_relative 'key_ref' module Genericode class ColumnSet < Lutaml::Model::Serializable @@ -20,26 +20,25 @@ class ColumnSet < Lutaml::Model::Serializable attribute :key_ref, KeyRef, collection: true json do - map "DatatypeLibrary", to: :datatype_library - map "Annotation", to: :annotation - map "Identification", to: :identification - map "Column", to: :column - map "ColumnRef", to: :column_ref - map "Key", to: :key - map "KeyRef", to: :key_ref + map 'DatatypeLibrary', to: :datatype_library + map 'Annotation', to: :annotation + map 'Identification', to: :identification + map 'Column', to: :column + map 'ColumnRef', to: :column_ref + map 'Key', to: :key + map 'KeyRef', to: :key_ref end xml do - root "ColumnSet" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'ColumnSet' - map_attribute "DatatypeLibrary", to: :datatype_library - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Identification", to: :identification, prefix: nil, namespace: nil - map_element "Column", to: :column, prefix: nil, namespace: nil - map_element "ColumnRef", to: :column_ref, prefix: nil, namespace: nil - map_element "Key", to: :key, prefix: nil, namespace: nil - map_element "KeyRef", to: :key_ref, prefix: nil, namespace: nil + map_attribute 'DatatypeLibrary', to: :datatype_library + map_element 'Annotation', to: :annotation + map_element 'Identification', to: :identification + map_element 'Column', to: :column + map_element 'ColumnRef', to: :column_ref + map_element 'Key', to: :key + map_element 'KeyRef', to: :key_ref end end end diff --git a/lib/genericode/column_set_ref.rb b/lib/genericode/column_set_ref.rb index 01054dd..f655202 100644 --- a/lib/genericode/column_set_ref.rb +++ b/lib/genericode/column_set_ref.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" +require_relative 'annotation' module Genericode class ColumnSetRef < Lutaml::Model::Serializable @@ -11,18 +11,17 @@ class ColumnSetRef < Lutaml::Model::Serializable attribute :location_uri, :string, collection: true json do - map "Annotation", to: :annotation - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri + map 'Annotation', to: :annotation + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri end xml do - root "ColumnSetRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'ColumnSetRef' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri end end end diff --git a/lib/genericode/data.rb b/lib/genericode/data.rb index 7ed4c69..baf5f28 100644 --- a/lib/genericode/data.rb +++ b/lib/genericode/data.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "datatype_facet" +require_relative 'annotation' +require_relative 'datatype_facet' module Genericode class Data < Lutaml::Model::Serializable @@ -14,22 +14,21 @@ class Data < Lutaml::Model::Serializable attribute :parameter, DatatypeFacet, collection: true json do - map "Type", to: :type - map "DatatypeLibrary", to: :datatype_library - map "Lang", to: :lang - map "Annotation", to: :annotation - map "Parameter", to: :parameter + map 'Type', to: :type + map 'DatatypeLibrary', to: :datatype_library + map 'Lang', to: :lang + map 'Annotation', to: :annotation + map 'Parameter', to: :parameter end xml do - root "Data" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Data' - map_attribute "Type", to: :type - map_attribute "DatatypeLibrary", to: :datatype_library - map_attribute "Lang", to: :lang - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Parameter", to: :parameter, prefix: nil, namespace: nil + map_attribute 'Type', to: :type + map_attribute 'DatatypeLibrary', to: :datatype_library + map_attribute 'Lang', to: :lang + map_element 'Annotation', to: :annotation + map_element 'Parameter', to: :parameter end end end diff --git a/lib/genericode/data_restrictions.rb b/lib/genericode/data_restrictions.rb index 2146b8f..e9b4f3b 100644 --- a/lib/genericode/data_restrictions.rb +++ b/lib/genericode/data_restrictions.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "datatype_facet" +require_relative 'datatype_facet' module Genericode class DataRestrictions < Lutaml::Model::Serializable @@ -10,16 +10,15 @@ class DataRestrictions < Lutaml::Model::Serializable attribute :parameter, DatatypeFacet, collection: true json do - map "Lang", to: :lang - map "Parameter", to: :parameter + map 'Lang', to: :lang + map 'Parameter', to: :parameter end xml do - root "DataRestrictions" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'DataRestrictions' - map_attribute "Lang", to: :lang - map_element "Parameter", to: :parameter, prefix: nil, namespace: nil + map_attribute 'Lang', to: :lang + map_element 'Parameter', to: :parameter end end end diff --git a/lib/genericode/datatype_facet.rb b/lib/genericode/datatype_facet.rb index 12299ba..a8cb0e6 100644 --- a/lib/genericode/datatype_facet.rb +++ b/lib/genericode/datatype_facet.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -require "lutaml/model" -require_relative "json/short_name_mixin" +require 'lutaml/model' +require_relative 'json/short_name_mixin' module Genericode class DatatypeFacet < Lutaml::Model::Serializable @@ -12,18 +12,17 @@ class DatatypeFacet < Lutaml::Model::Serializable attribute :long_name, :string json do - map "ShortName", to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } - map "LongName", to: :long_name - map "_", to: :content + map 'ShortName', to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } + map 'LongName', to: :long_name + map '_', to: :content end xml do - root "DatatypeFacet" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'DatatypeFacet' map_content to: :content - map_attribute "ShortName", to: :short_name - map_attribute "LongName", to: :long_name + map_attribute 'ShortName', to: :short_name + map_attribute 'LongName', to: :long_name end end end diff --git a/lib/genericode/gc_namespace.rb b/lib/genericode/gc_namespace.rb new file mode 100644 index 0000000..f14a366 --- /dev/null +++ b/lib/genericode/gc_namespace.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'lutaml/xml/namespace' + +module Genericode + class GcNamespace < Lutaml::Xml::Namespace + uri 'http://docs.oasis-open.org/codelist/ns/genericode/1.0/' + prefix_default 'gc' + element_form_default :unqualified + attribute_form_default :unqualified + end +end diff --git a/lib/genericode/general_identifier.rb b/lib/genericode/general_identifier.rb index 0028e48..a3b8d09 100644 --- a/lib/genericode/general_identifier.rb +++ b/lib/genericode/general_identifier.rb @@ -1,26 +1,26 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' +require 'lutaml/xml/w3c' module Genericode class GeneralIdentifier < Lutaml::Model::Serializable attribute :content, :string attribute :identifier, :string - attribute :lang, :string + attribute :lang, Lutaml::Xml::W3c::XmlLangType json do - map "Identifier", to: :identifier - map "lang", to: :lang - map "_", to: :content + map 'Identifier', to: :identifier + map 'lang', to: :lang + map '_', to: :content end xml do - root "GeneralIdentifier" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'GeneralIdentifier' map_content to: :content - map_attribute "Identifier", to: :identifier - map_attribute "lang", to: :lang, prefix: "xml", namespace: "http://www.w3.org/XML/1998/namespace" + map_attribute 'Identifier', to: :identifier + map_attribute 'lang', to: :lang end end end diff --git a/lib/genericode/identification.rb b/lib/genericode/identification.rb index aac32fe..7bd0ff3 100644 --- a/lib/genericode/identification.rb +++ b/lib/genericode/identification.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "agency" -require_relative "long_name" -require_relative "mime_typed_uri" -require_relative "canonical_uri" -require_relative "short_name" -require_relative "json/short_name_mixin" -require_relative "json/canonical_uri_mixin" -require_relative "utils" +require_relative 'agency' +require_relative 'long_name' +require_relative 'mime_typed_uri' +require_relative 'canonical_uri' +require_relative 'short_name' +require_relative 'json/short_name_mixin' +require_relative 'json/canonical_uri_mixin' +require_relative 'utils' module Genericode class Identification < Lutaml::Model::Serializable @@ -26,16 +26,16 @@ class Identification < Lutaml::Model::Serializable attribute :agency, Agency json do - map "ShortName", to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } - map "LongName", to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } - map "Version", to: :version - map "CanonicalUri", to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri, with: { from: :location_uri_from_json, to: :location_uri_to_json } - map "AlternateFormatLocationUri", to: :alternate_format_location_uri, + map 'ShortName', to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } + map 'LongName', to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } + map 'Version', to: :version + map 'CanonicalUri', to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri, with: { from: :location_uri_from_json, to: :location_uri_to_json } + map 'AlternateFormatLocationUri', to: :alternate_format_location_uri, with: { from: :alternate_format_location_uri_from_json, - to: :alternate_format_location_uri_to_json, } - map "Agency", to: :agency + to: :alternate_format_location_uri_to_json } + map 'Agency', to: :agency end def long_name_from_json(model, value) @@ -45,7 +45,7 @@ def long_name_from_json(model, value) def long_name_to_json(model, doc) return if model.long_name.nil? || model.long_name.empty? - doc["LongName"] = LongName.as_json(model.long_name) + doc['LongName'] = LongName.as_json(model.long_name) end def location_uri_from_json(model, value) @@ -57,7 +57,7 @@ def location_uri_from_json(model, value) def location_uri_to_json(model, doc) return if model.location_uri.nil? || model.location_uri.empty? - doc["LocationUri"] = Lutaml::Model::Type::String.cast(Utils.one_or_all(model.location_uri)) + doc['LocationUri'] = Lutaml::Model::Type::String.cast(Utils.one_or_all(model.location_uri)) end def alternate_format_location_uri_from_json(model, value) @@ -67,21 +67,20 @@ def alternate_format_location_uri_from_json(model, value) def alternate_format_location_uri_to_json(model, doc) return if model.alternate_format_location_uri.nil? || model.alternate_format_location_uri.empty? - doc["AlternateFormatLocationUri"] = MimeTypedUri.as_json(Utils.one_or_all(model.alternate_format_location_uri)) + doc['AlternateFormatLocationUri'] = MimeTypedUri.as_json(Utils.one_or_all(model.alternate_format_location_uri)) end xml do - root "Identification" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" - - map_element "ShortName", to: :short_name, prefix: nil, namespace: nil - map_element "LongName", to: :long_name, prefix: nil, namespace: nil - map_element "Version", to: :version, prefix: nil, namespace: nil - map_element "CanonicalUri", to: :canonical_uri, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil - map_element "AlternateFormatLocationUri", to: :alternate_format_location_uri, prefix: nil, namespace: nil - map_element "Agency", to: :agency, prefix: nil, namespace: nil + element 'Identification' + + map_element 'ShortName', to: :short_name + map_element 'LongName', to: :long_name + map_element 'Version', to: :version + map_element 'CanonicalUri', to: :canonical_uri + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri + map_element 'AlternateFormatLocationUri', to: :alternate_format_location_uri + map_element 'Agency', to: :agency end end end diff --git a/lib/genericode/json/canonical_uri_mixin.rb b/lib/genericode/json/canonical_uri_mixin.rb index b0ac98f..7c34d0e 100644 --- a/lib/genericode/json/canonical_uri_mixin.rb +++ b/lib/genericode/json/canonical_uri_mixin.rb @@ -10,7 +10,7 @@ def canonical_uri_from_json(model, value) def canonical_uri_to_json(model, doc) return if model.canonical_uri.nil? - doc["CanonicalUri"] = model.canonical_uri&.content + doc['CanonicalUri'] = model.canonical_uri&.content end end end diff --git a/lib/genericode/json/short_name_mixin.rb b/lib/genericode/json/short_name_mixin.rb index 2c53004..d6193ad 100644 --- a/lib/genericode/json/short_name_mixin.rb +++ b/lib/genericode/json/short_name_mixin.rb @@ -10,7 +10,7 @@ def short_name_from_json(model, value) def short_name_to_json(model, doc) return if model.short_name.nil? - doc["ShortName"] = model.short_name.content + doc['ShortName'] = model.short_name.content end end end diff --git a/lib/genericode/key.rb b/lib/genericode/key.rb index 303dab1..badb5d5 100644 --- a/lib/genericode/key.rb +++ b/lib/genericode/key.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "key_column_ref" -require_relative "canonical_uri" -require_relative "long_name" -require_relative "short_name" -require_relative "json/short_name_mixin" -require_relative "json/canonical_uri_mixin" -require_relative "utils" +require_relative 'annotation' +require_relative 'key_column_ref' +require_relative 'canonical_uri' +require_relative 'long_name' +require_relative 'short_name' +require_relative 'json/short_name_mixin' +require_relative 'json/canonical_uri_mixin' +require_relative 'utils' module Genericode class Key < Lutaml::Model::Serializable @@ -25,13 +25,13 @@ class Key < Lutaml::Model::Serializable attribute :column_ref, KeyColumnRef, collection: true json do - map "Id", to: :id - map "Annotation", to: :annotation - map "ShortName", to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } - map "LongName", to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } - map "CanonicalUri", to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } - map "CanonicalVersionUri", to: :canonical_version_uri - map "ColumnRef", to: :column_ref, with: { from: :column_ref_from_json, to: :column_ref_to_json } + map 'Id', to: :id + map 'Annotation', to: :annotation + map 'ShortName', to: :short_name, with: { from: :short_name_from_json, to: :short_name_to_json } + map 'LongName', to: :long_name, with: { from: :long_name_from_json, to: :long_name_to_json } + map 'CanonicalUri', to: :canonical_uri, with: { from: :canonical_uri_from_json, to: :canonical_uri_to_json } + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'ColumnRef', to: :column_ref, with: { from: :column_ref_from_json, to: :column_ref_to_json } end def long_name_from_json(model, value) @@ -41,7 +41,7 @@ def long_name_from_json(model, value) def long_name_to_json(model, doc) return if model.long_name.nil? || model.long_name.empty? - doc["LongName"] = LongName.as_json(model.long_name) + doc['LongName'] = LongName.as_json(model.long_name) end def column_ref_from_json(model, value) @@ -49,20 +49,19 @@ def column_ref_from_json(model, value) end def column_ref_to_json(model, doc) - doc["ColumnRef"] = Utils.one_or_all(model.column_ref.map(&:ref)) + doc['ColumnRef'] = Utils.one_or_all(model.column_ref.map(&:ref)) end xml do - root "Key" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Key' - map_attribute "Id", to: :id - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "ShortName", to: :short_name, prefix: nil, namespace: nil - map_element "LongName", to: :long_name, prefix: nil, namespace: nil - map_element "CanonicalUri", to: :canonical_uri, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "ColumnRef", to: :column_ref, prefix: nil, namespace: nil + map_attribute 'Id', to: :id + map_element 'Annotation', to: :annotation + map_element 'ShortName', to: :short_name + map_element 'LongName', to: :long_name + map_element 'CanonicalUri', to: :canonical_uri + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'ColumnRef', to: :column_ref end end end diff --git a/lib/genericode/key_column_ref.rb b/lib/genericode/key_column_ref.rb index 852bf25..1c35327 100644 --- a/lib/genericode/key_column_ref.rb +++ b/lib/genericode/key_column_ref.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" +require_relative 'annotation' module Genericode class KeyColumnRef < Lutaml::Model::Serializable @@ -10,16 +10,15 @@ class KeyColumnRef < Lutaml::Model::Serializable attribute :annotation, Annotation json do - map "Ref", to: :ref - map "Annotation", to: :annotation + map 'Ref', to: :ref + map 'Annotation', to: :annotation end xml do - root "KeyColumnRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'KeyColumnRef' - map_attribute "Ref", to: :ref - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil + map_attribute 'Ref', to: :ref + map_element 'Annotation', to: :annotation end end end diff --git a/lib/genericode/key_ref.rb b/lib/genericode/key_ref.rb index c6fccb1..6fb8117 100644 --- a/lib/genericode/key_ref.rb +++ b/lib/genericode/key_ref.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" +require_relative 'annotation' module Genericode class KeyRef < Lutaml::Model::Serializable @@ -13,22 +13,21 @@ class KeyRef < Lutaml::Model::Serializable attribute :location_uri, :string, collection: true json do - map "Id", to: :id - map "ExternalRef", to: :external_ref - map "Annotation", to: :annotation - map "CanonicalVersionUri", to: :canonical_version_uri - map "LocationUri", to: :location_uri + map 'Id', to: :id + map 'ExternalRef', to: :external_ref + map 'Annotation', to: :annotation + map 'CanonicalVersionUri', to: :canonical_version_uri + map 'LocationUri', to: :location_uri end xml do - root "KeyRef" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'KeyRef' - map_attribute "Id", to: :id - map_attribute "ExternalRef", to: :external_ref - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "CanonicalVersionUri", to: :canonical_version_uri, prefix: nil, namespace: nil - map_element "LocationUri", to: :location_uri, prefix: nil, namespace: nil + map_attribute 'Id', to: :id + map_attribute 'ExternalRef', to: :external_ref + map_element 'Annotation', to: :annotation + map_element 'CanonicalVersionUri', to: :canonical_version_uri + map_element 'LocationUri', to: :location_uri end end end diff --git a/lib/genericode/long_name.rb b/lib/genericode/long_name.rb index 95ef406..8313e0e 100644 --- a/lib/genericode/long_name.rb +++ b/lib/genericode/long_name.rb @@ -1,36 +1,36 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' +require 'lutaml/xml/w3c' module Genericode class LongName < Lutaml::Model::Serializable attribute :content, :string attribute :identifier, :string - attribute :lang, :string + attribute :lang, Lutaml::Xml::W3c::XmlLangType json do - map "Identifier", to: :identifier - map "http://www.w3.org/XML/1998/namespace", to: :lang, with: { from: :lang_from_json, to: :lang_to_json } - map "_", to: :content + map 'Identifier', to: :identifier + map 'http://www.w3.org/XML/1998/namespace', to: :lang, with: { from: :lang_from_json, to: :lang_to_json } + map '_', to: :content end def lang_from_json(model, value) - model.lang = value["lang"] + model.lang = value['lang'] end def lang_to_json(model, doc) return if model.lang.nil? - doc["http://www.w3.org/XML/1998/namespace"] = { "lang" => model.lang } + doc['http://www.w3.org/XML/1998/namespace'] = { 'lang' => model.lang } end xml do - root "LongName" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'LongName' map_content to: :content - map_attribute "Identifier", to: :identifier - map_attribute "lang", to: :lang, prefix: "xml", namespace: "http://www.w3.org/XML/1998/namespace" + map_attribute 'Identifier', to: :identifier + map_attribute 'lang', to: :lang end end end diff --git a/lib/genericode/mime_typed_uri.rb b/lib/genericode/mime_typed_uri.rb index d611816..e010fda 100644 --- a/lib/genericode/mime_typed_uri.rb +++ b/lib/genericode/mime_typed_uri.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' module Genericode class MimeTypedUri < Lutaml::Model::Serializable @@ -8,16 +8,15 @@ class MimeTypedUri < Lutaml::Model::Serializable attribute :mime_type, :string json do - map "MimeType", to: :mime_type - map "_", to: :content + map 'MimeType', to: :mime_type + map '_', to: :content end xml do - root "MimeTypedUri" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'MimeTypedUri' map_content to: :content - map_attribute "MimeType", to: :mime_type + map_attribute 'MimeType', to: :mime_type end end end diff --git a/lib/genericode/row.rb b/lib/genericode/row.rb index 64702e3..ad0cd7c 100644 --- a/lib/genericode/row.rb +++ b/lib/genericode/row.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "value" +require_relative 'annotation' +require_relative 'value' module Genericode class Row < Lutaml::Model::Serializable @@ -11,16 +11,15 @@ class Row < Lutaml::Model::Serializable attribute :value, Value, collection: true json do - map "Annotation", to: :annotation - map "Value", to: :value + map 'Annotation', to: :annotation + map 'Value', to: :value end xml do - root "Row" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Row' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Value", to: :value, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'Value', to: :value end end end diff --git a/lib/genericode/short_name.rb b/lib/genericode/short_name.rb index c223ffe..01d6e21 100644 --- a/lib/genericode/short_name.rb +++ b/lib/genericode/short_name.rb @@ -1,23 +1,23 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' +require 'lutaml/xml/w3c' module Genericode class ShortName < Lutaml::Model::Serializable attribute :content, :string - attribute :lang, :string + attribute :lang, Lutaml::Xml::W3c::XmlLangType json do - map "lang", to: :lang - map "_", to: :content + map 'lang', to: :lang + map '_', to: :content end xml do - root "ShortName" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'ShortName' map_content to: :content - map_attribute "lang", to: :lang, prefix: "xml", namespace: "http://www.w3.org/XML/1998/namespace" + map_attribute 'lang', to: :lang end # Rule 39: Must not contain whitespace characters diff --git a/lib/genericode/simple_code_list.rb b/lib/genericode/simple_code_list.rb index 71046fa..4ff0658 100644 --- a/lib/genericode/simple_code_list.rb +++ b/lib/genericode/simple_code_list.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "row" +require_relative 'annotation' +require_relative 'row' module Genericode class SimpleCodeList < Lutaml::Model::Serializable @@ -11,16 +11,15 @@ class SimpleCodeList < Lutaml::Model::Serializable attribute :row, Row, collection: true, initialize_empty: true json do - map "Annotation", to: :annotation - map "Row", to: :row + map 'Annotation', to: :annotation + map 'Row', to: :row end xml do - root "SimpleCodeList" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'SimpleCodeList' - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "Row", to: :row, prefix: nil, namespace: nil + map_element 'Annotation', to: :annotation + map_element 'Row', to: :row end end end diff --git a/lib/genericode/simple_value.rb b/lib/genericode/simple_value.rb index 36fbae0..affd385 100644 --- a/lib/genericode/simple_value.rb +++ b/lib/genericode/simple_value.rb @@ -1,18 +1,17 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' module Genericode class SimpleValue < Lutaml::Model::Serializable attribute :content, :string json do - map "_", to: :content + map '_', to: :content end xml do - root "SimpleValue" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'SimpleValue' map_content to: :content end diff --git a/lib/genericode/value.rb b/lib/genericode/value.rb index 9eaf60c..ffbba5d 100644 --- a/lib/genericode/value.rb +++ b/lib/genericode/value.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require "lutaml/model" +require 'lutaml/model' -require_relative "annotation" -require_relative "any_other_content" -require_relative "simple_value" -require_relative "column_ref" +require_relative 'annotation' +require_relative 'any_other_content' +require_relative 'simple_value' +require_relative 'column_ref' module Genericode class Value < Lutaml::Model::Serializable @@ -15,20 +15,19 @@ class Value < Lutaml::Model::Serializable attribute :complex_value, AnyOtherContent json do - map "ColumnRef", to: :column_ref - map "Annotation", to: :annotation - map "SimpleValue", to: :simple_value - map "ComplexValue", to: :complex_value + map 'ColumnRef', to: :column_ref + map 'Annotation', to: :annotation + map 'SimpleValue', to: :simple_value + map 'ComplexValue', to: :complex_value end xml do - root "Value" - namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc" + element 'Value' - map_attribute "ColumnRef", to: :column_ref, prefix: nil, namespace: nil - map_element "Annotation", to: :annotation, prefix: nil, namespace: nil - map_element "SimpleValue", to: :simple_value, prefix: nil, namespace: nil - map_element "ComplexValue", to: :complex_value, prefix: nil, namespace: nil + map_attribute 'ColumnRef', to: :column_ref + map_element 'Annotation', to: :annotation + map_element 'SimpleValue', to: :simple_value + map_element 'ComplexValue', to: :complex_value end end end diff --git a/lib/genericode/version.rb b/lib/genericode/version.rb index dc4cd09..1f77032 100644 --- a/lib/genericode/version.rb +++ b/lib/genericode/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Genericode - VERSION = "0.1.3" + VERSION = '0.1.3' end diff --git a/spec/genericode/cli/code_lister_spec.rb b/spec/genericode/cli/code_lister_spec.rb index e5b0253..17eb9a0 100644 --- a/spec/genericode/cli/code_lister_spec.rb +++ b/spec/genericode/cli/code_lister_spec.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true -require "spec_helper" -require_relative "../../../lib/genericode/cli/code_lister" +require 'spec_helper' +require_relative '../../../lib/genericode/cli/code_lister' RSpec.describe Genericode::Cli::CodeLister do - describe ".list_codes" do + describe '.list_codes' do let(:valid_xml) do <<~XML @@ -29,14 +29,14 @@ XML end - it "lists codes from a valid XML file" do + it 'lists codes from a valid XML file' do allow(File).to receive(:read).and_return(valid_xml) allow(Genericode::CodeList).to receive(:from_xml).and_call_original - expect(Genericode::Cli::CodeLister.list_codes("valid.gc")).to eq("Code\nCode1\nCode2") + expect(Genericode::Cli::CodeLister.list_codes('valid.gc')).to eq("Code\nCode1\nCode2") end - it "raises an error when an invalid ColumnRef is found" do + it 'raises an error when an invalid ColumnRef is found' do xml_with_invalid_column_ref = <<~XML @@ -57,12 +57,12 @@ allow(Genericode::CodeList).to receive(:from_xml).and_call_original expect do - Genericode::Cli::CodeLister.list_codes("invalid.gc") + Genericode::Cli::CodeLister.list_codes('invalid.gc') end.to raise_error(Genericode::Error, - /INVALID_COLUMN_REF: Invalid ColumnRef 'invalid' in row 1/,) + /INVALID_COLUMN_REF: Invalid ColumnRef 'invalid' in row 1/) end - it "raises an error when a code value is invalid" do + it 'raises an error when a code value is invalid' do invalid_xml = <<~XML @@ -84,9 +84,9 @@ allow(Genericode::CodeList).to receive(:from_xml).and_call_original expect do - Genericode::Cli::CodeLister.list_codes("invalid.gc") + Genericode::Cli::CodeLister.list_codes('invalid.gc') end.to raise_error(Genericode::Error, - /INVALID_DATA_TYPE: Invalid data type for column 'Code' in row 1/,) + /INVALID_DATA_TYPE: Invalid data type for column 'Code' in row 1/) end end end diff --git a/spec/genericode/cli/code_lookup_spec.rb b/spec/genericode/cli/code_lookup_spec.rb index fe81db6..893d04b 100644 --- a/spec/genericode/cli/code_lookup_spec.rb +++ b/spec/genericode/cli/code_lookup_spec.rb @@ -2,11 +2,11 @@ # spec/genericode/code_lookup_spec.rb -require "spec_helper" -require_relative "../../../lib/genericode/cli/code_lookup" +require 'spec_helper' +require_relative '../../../lib/genericode/cli/code_lookup' RSpec.describe Genericode::Cli::CodeLookup do - describe ".lookup" do + describe '.lookup' do let(:valid_xml) do <<~XML @@ -37,23 +37,23 @@ allow(Genericode::CodeList).to receive(:from_xml).and_call_original end - it "looks up a code successfully" do - expect(Genericode::Cli::CodeLookup.lookup("file.gc", "code:Code1")).to eq("Code: Code1\nName: Name1") + it 'looks up a code successfully' do + expect(Genericode::Cli::CodeLookup.lookup('file.gc', 'code:Code1')).to eq("Code: Code1\nName: Name1") end - it "looks up a name successfully" do - expect(Genericode::Cli::CodeLookup.lookup("file.gc", "name:Name1")).to eq("Code: Code1\nName: Name1") + it 'looks up a name successfully' do + expect(Genericode::Cli::CodeLookup.lookup('file.gc', 'name:Name1')).to eq("Code: Code1\nName: Name1") end - it "looks up a simple value successfully" do - expect(Genericode::Cli::CodeLookup.lookup("file.gc", "code:Code1>name")).to eq("Name1") + it 'looks up a simple value successfully' do + expect(Genericode::Cli::CodeLookup.lookup('file.gc', 'code:Code1>name')).to eq('Name1') end - it "raises an error for invalid path" do + it 'raises an error for invalid path' do expect do - Genericode::Cli::CodeLookup.lookup("file.gc", - "invalid:path",) - end.to raise_error(Genericode::Error, "Column not found: invalid") + Genericode::Cli::CodeLookup.lookup('file.gc', + 'invalid:path') + end.to raise_error(Genericode::Error, 'Column not found: invalid') end end end diff --git a/spec/genericode/cli/commands_spec.rb b/spec/genericode/cli/commands_spec.rb index 8dbaeae..230d362 100644 --- a/spec/genericode/cli/commands_spec.rb +++ b/spec/genericode/cli/commands_spec.rb @@ -2,57 +2,57 @@ # spec/genericode/cli_spec.rb -require "spec_helper" -require_relative "../../../lib/genericode/cli/commands" +require 'spec_helper' +require_relative '../../../lib/genericode/cli/commands' RSpec.describe Genericode::Cli::Commands do let(:cli) { described_class.new } - describe "#convert" do - it "converts XML to JSON successfully" do + describe '#convert' do + it 'converts XML to JSON successfully' do allow(Genericode::Cli::Converter).to receive(:convert).and_return(true) - expect { cli.convert("input.gc", "output.gcj") }.to output("Conversion successful.\n").to_stdout + expect { cli.convert('input.gc', 'output.gcj') }.to output("Conversion successful.\n").to_stdout end - it "handles conversion errors" do - allow(Genericode::Cli::Converter).to receive(:convert).and_raise(Genericode::Error, "Conversion failed") - expect { cli.convert("input.gc", "output.gcj") }.to output("Conversion failed: Conversion failed\n").to_stdout + it 'handles conversion errors' do + allow(Genericode::Cli::Converter).to receive(:convert).and_raise(Genericode::Error, 'Conversion failed') + expect { cli.convert('input.gc', 'output.gcj') }.to output("Conversion failed: Conversion failed\n").to_stdout end end - describe "#validate" do - it "validates a file successfully" do + describe '#validate' do + it 'validates a file successfully' do allow(Genericode::CodeList).to receive(:from_file).and_return(double(valid?: true)) - expect { cli.validate("file.gc") }.to output("File is valid.\n").to_stdout + expect { cli.validate('file.gc') }.to output("File is valid.\n").to_stdout end - it "handles validation errors" do - allow(Genericode::CodeList).to receive(:from_file).and_raise(Genericode::Error, "Invalid file") - expect { cli.validate("file.gc") }.to output("Validation failed: Invalid file\n").to_stdout + it 'handles validation errors' do + allow(Genericode::CodeList).to receive(:from_file).and_raise(Genericode::Error, 'Invalid file') + expect { cli.validate('file.gc') }.to output("Validation failed: Invalid file\n").to_stdout end end - describe "#list_codes" do - it "lists codes successfully" do + describe '#list_codes' do + it 'lists codes successfully' do allow(Genericode::Cli::CodeLister).to receive(:list_codes).and_return("Code1\nCode2") - expect { cli.list_codes("file.gc") }.to output("Code1\nCode2\n").to_stdout + expect { cli.list_codes('file.gc') }.to output("Code1\nCode2\n").to_stdout end - it "handles listing errors" do - allow(Genericode::Cli::CodeLister).to receive(:list_codes).and_raise(Genericode::Error, "Listing failed") - expect { cli.list_codes("file.gc") }.to output("Listing codes failed: Listing failed\n").to_stdout + it 'handles listing errors' do + allow(Genericode::Cli::CodeLister).to receive(:list_codes).and_raise(Genericode::Error, 'Listing failed') + expect { cli.list_codes('file.gc') }.to output("Listing codes failed: Listing failed\n").to_stdout end end - describe "#lookup" do - it "looks up a code successfully" do - allow(Genericode::Cli::CodeLookup).to receive(:lookup).and_return("Result") - expect { cli.lookup("file.gc", "path") }.to output("Result\n").to_stdout + describe '#lookup' do + it 'looks up a code successfully' do + allow(Genericode::Cli::CodeLookup).to receive(:lookup).and_return('Result') + expect { cli.lookup('file.gc', 'path') }.to output("Result\n").to_stdout end - it "handles lookup errors" do - allow(Genericode::Cli::CodeLookup).to receive(:lookup).and_raise(Genericode::Error, "Lookup failed") - expect { cli.lookup("file.gc", "path") }.to output("Lookup failed: Lookup failed\n").to_stdout + it 'handles lookup errors' do + allow(Genericode::Cli::CodeLookup).to receive(:lookup).and_raise(Genericode::Error, 'Lookup failed') + expect { cli.lookup('file.gc', 'path') }.to output("Lookup failed: Lookup failed\n").to_stdout end end end diff --git a/spec/genericode/cli/converter_spec.rb b/spec/genericode/cli/converter_spec.rb index dd8c5ef..89c3e59 100644 --- a/spec/genericode/cli/converter_spec.rb +++ b/spec/genericode/cli/converter_spec.rb @@ -2,46 +2,46 @@ # spec/genericode/converter_spec.rb -require "spec_helper" -require_relative "../../../lib/genericode/cli/converter" +require 'spec_helper' +require_relative '../../../lib/genericode/cli/converter' RSpec.describe Genericode::Cli::Converter do - describe ".convert" do - it "converts XML to JSON" do - allow(File).to receive(:read).and_return("") + describe '.convert' do + it 'converts XML to JSON' do + allow(File).to receive(:read).and_return('') allow(File).to receive(:write) - allow_any_instance_of(Genericode::CodeList).to receive(:to_json).and_return("{}") + allow_any_instance_of(Genericode::CodeList).to receive(:to_json).and_return('{}') - expect(Genericode::Cli::Converter.convert("input.gc", "output.gcj")).to be true + expect(Genericode::Cli::Converter.convert('input.gc', 'output.gcj')).to be true end - it "converts JSON to XML" do - allow(File).to receive(:read).and_return("{}") + it 'converts JSON to XML' do + allow(File).to receive(:read).and_return('{}') allow(File).to receive(:write) - allow_any_instance_of(Genericode::CodeList).to receive(:to_xml).and_return("") + allow_any_instance_of(Genericode::CodeList).to receive(:to_xml).and_return('') - expect(Genericode::Cli::Converter.convert("input.gcj", "output.gc")).to be true + expect(Genericode::Cli::Converter.convert('input.gcj', 'output.gc')).to be true end - it "raises an error for invalid input format" do + it 'raises an error for invalid input format' do expect do - Genericode::Cli::Converter.convert("input.txt", - "output.gcj",) - end.to raise_error(Genericode::Error, "Invalid input format") + Genericode::Cli::Converter.convert('input.txt', + 'output.gcj') + end.to raise_error(Genericode::Error, 'Invalid input format') end - it "raises an error for invalid output format" do + it 'raises an error for invalid output format' do expect do - Genericode::Cli::Converter.convert("input.gc", - "output.txt",) - end.to raise_error(Genericode::Error, "Invalid output format") + Genericode::Cli::Converter.convert('input.gc', + 'output.txt') + end.to raise_error(Genericode::Error, 'Invalid output format') end - it "raises an error when input and output formats are the same" do + it 'raises an error when input and output formats are the same' do expect do - Genericode::Cli::Converter.convert("input.gc", - "output.gc",) - end.to raise_error(Genericode::Error, "Input and output formats are the same") + Genericode::Cli::Converter.convert('input.gc', + 'output.gc') + end.to raise_error(Genericode::Error, 'Input and output formats are the same') end end end diff --git a/spec/genericode/cli/validator_spec.rb b/spec/genericode/cli/validator_spec.rb index 185b963..8379e60 100644 --- a/spec/genericode/cli/validator_spec.rb +++ b/spec/genericode/cli/validator_spec.rb @@ -2,11 +2,11 @@ # spec/genericode/validator_spec.rb -require "spec_helper" -require_relative "../../../lib/genericode/cli/validator" +require 'spec_helper' +require_relative '../../../lib/genericode/cli/validator' RSpec.describe Genericode::Cli::Validator do - describe ".validate" do + describe '.validate' do let(:valid_xml) do <<~XML @@ -26,45 +26,45 @@ XML end - it "validates a valid XML file" do + it 'validates a valid XML file' do allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).and_return(valid_xml) - expect(Genericode::Cli::Validator.validate("valid.gc")).to be true + expect(Genericode::Cli::Validator.validate('valid.gc')).to be true end - it "raises an error for non-existent file" do + it 'raises an error for non-existent file' do allow(File).to receive(:exist?).and_return(false) expect do - Genericode::Cli::Validator.validate("nonexistent.gc") - end.to raise_error(Genericode::Error, "File does not exist") + Genericode::Cli::Validator.validate('nonexistent.gc') + end.to raise_error(Genericode::Error, 'File does not exist') end - it "raises an error for invalid file format" do + it 'raises an error for invalid file format' do allow(File).to receive(:exist?).and_return(true) expect do - Genericode::Cli::Validator.validate("invalid.txt") - end.to raise_error(Genericode::Error, "Invalid file format") + Genericode::Cli::Validator.validate('invalid.txt') + end.to raise_error(Genericode::Error, 'Invalid file format') end - it "raises an error for empty column set" do - xml_without_columns = "" + it 'raises an error for empty column set' do + xml_without_columns = '' allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).and_return(xml_without_columns) expect do - Genericode::Cli::Validator.validate("invalid.gc") - end.to raise_error(Genericode::Error, "No columns defined") + Genericode::Cli::Validator.validate('invalid.gc') + end.to raise_error(Genericode::Error, 'No columns defined') end - it "raises an error for empty row set" do - xml_without_rows = "" + it 'raises an error for empty row set' do + xml_without_rows = '' allow(File).to receive(:exist?).and_return(true) allow(File).to receive(:read).and_return(xml_without_rows) - expect { Genericode::Cli::Validator.validate("invalid.gc") }.to raise_error(Genericode::Error, "No rows defined") + expect { Genericode::Cli::Validator.validate('invalid.gc') }.to raise_error(Genericode::Error, 'No rows defined') end end end diff --git a/spec/genericode/code_list_spec.rb b/spec/genericode/code_list_spec.rb index a0a89e1..3072136 100644 --- a/spec/genericode/code_list_spec.rb +++ b/spec/genericode/code_list_spec.rb @@ -2,27 +2,27 @@ # spec/genericode/code_list_spec.rb -require "spec_helper" -require_relative "../../lib/genericode/code_list" -require "json" +require 'spec_helper' +require_relative '../../lib/genericode/code_list' +require 'json' RSpec.describe Genericode::CodeList do let(:valid_column_set) do Genericode::ColumnSet.new( column: [ Genericode::Column.new( - id: "code", - use: "required", - short_name: Genericode::ShortName.new(content: "Code"), - data: Genericode::Data.new(type: "string"), + id: 'code', + use: 'required', + short_name: Genericode::ShortName.new(content: 'Code'), + data: Genericode::Data.new(type: 'string') ), Genericode::Column.new( - id: "name", - use: "optional", - short_name: Genericode::ShortName.new(content: "Name"), - data: Genericode::Data.new(type: "string"), - ), - ], + id: 'name', + use: 'optional', + short_name: Genericode::ShortName.new(content: 'Name'), + data: Genericode::Data.new(type: 'string') + ) + ] ) end @@ -31,156 +31,155 @@ row: [ Genericode::Row.new( value: [ - Genericode::Value.new(column_ref: "code", simple_value: Genericode::SimpleValue.new(content: "CODE1")), - Genericode::Value.new(column_ref: "name", simple_value: Genericode::SimpleValue.new(content: "Name 1")), - ], + Genericode::Value.new(column_ref: 'code', simple_value: Genericode::SimpleValue.new(content: 'CODE1')), + Genericode::Value.new(column_ref: 'name', simple_value: Genericode::SimpleValue.new(content: 'Name 1')) + ] ), Genericode::Row.new( value: [ - Genericode::Value.new(column_ref: "code", simple_value: Genericode::SimpleValue.new(content: "CODE2")), - Genericode::Value.new(column_ref: "name", simple_value: Genericode::SimpleValue.new(content: "Name 2")), - ], - ), - ], + Genericode::Value.new(column_ref: 'code', simple_value: Genericode::SimpleValue.new(content: 'CODE2')), + Genericode::Value.new(column_ref: 'name', simple_value: Genericode::SimpleValue.new(content: 'Name 2')) + ] + ) + ] ) end let(:valid_code_list) do described_class.new( column_set: valid_column_set, - simple_code_list: valid_simple_code_list, + simple_code_list: valid_simple_code_list ) end - describe "#valid?" do - it "returns true for a valid code list" do + describe '#valid?' do + it 'returns true for a valid code list' do expect(valid_code_list).to be_valid end - it "returns false when column set is missing" do + it 'returns false when column set is missing' do invalid_code_list = valid_code_list.dup invalid_code_list.column_set = nil expect(invalid_code_list).not_to be_valid end - it "returns false when simple code list is missing" do + it 'returns false when simple code list is missing' do invalid_code_list = valid_code_list.dup invalid_code_list.simple_code_list = nil expect(invalid_code_list).not_to be_valid end end - describe "#validate_verbose" do - it "returns an empty array for a valid code list" do + describe '#validate_verbose' do + it 'returns an empty array for a valid code list' do expect(valid_code_list.validate_verbose).to be_empty end - it "reports missing column set" do + it 'reports missing column set' do invalid_code_list = valid_code_list.dup invalid_code_list.column_set = nil result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "MISSING_COLUMN_SET")) + expect(result).to include(hash_including(code: 'MISSING_COLUMN_SET')) end - it "reports missing simple code list" do + it 'reports missing simple code list' do invalid_code_list = valid_code_list.dup invalid_code_list.simple_code_list = nil result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "MISSING_SIMPLE_CODE_LIST")) + expect(result).to include(hash_including(code: 'MISSING_SIMPLE_CODE_LIST')) end - it "reports duplicate column IDs" do + it 'reports duplicate column IDs' do invalid_column_set = valid_column_set.dup - invalid_column_set.column << Genericode::Column.new(id: "code", - short_name: Genericode::ShortName.new(content: "Duplicate"),) + invalid_column_set.column << Genericode::Column.new(id: 'code', + short_name: Genericode::ShortName.new(content: 'Duplicate')) invalid_code_list = valid_code_list.dup invalid_code_list.column_set = invalid_column_set result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "DUPLICATE_COLUMN_IDS")) + expect(result).to include(hash_including(code: 'DUPLICATE_COLUMN_IDS')) end - it "reports missing Code column" do + it 'reports missing Code column' do invalid_column_set = Genericode::ColumnSet.new( - column: [Genericode::Column.new(id: "name", short_name: Genericode::ShortName.new(content: "Name"))], + column: [Genericode::Column.new(id: 'name', short_name: Genericode::ShortName.new(content: 'Name'))] ) invalid_code_list = valid_code_list.dup invalid_code_list.column_set = invalid_column_set result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "INVALID_COLUMN_REF")) + expect(result).to include(hash_including(code: 'INVALID_COLUMN_REF')) end - it "reports duplicate code values" do + it 'reports duplicate code values' do invalid_simple_code_list = valid_simple_code_list.dup invalid_simple_code_list.row << Genericode::Row.new( - value: [Genericode::Value.new(column_ref: "code", simple_value: Genericode::SimpleValue.new(content: "CODE1"))], + value: [Genericode::Value.new(column_ref: 'code', simple_value: Genericode::SimpleValue.new(content: 'CODE1'))] ) invalid_code_list = valid_code_list.dup invalid_code_list.simple_code_list = invalid_simple_code_list result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "DUPLICATE_VALUES")) + expect(result).to include(hash_including(code: 'DUPLICATE_VALUES')) end - it "reports missing required values" do + it 'reports missing required values' do invalid_simple_code_list = valid_simple_code_list.dup invalid_simple_code_list.row << Genericode::Row.new( - value: [Genericode::Value.new(column_ref: "name", - simple_value: Genericode::SimpleValue.new(content: "Name 3"),)], + value: [Genericode::Value.new(column_ref: 'name', + simple_value: Genericode::SimpleValue.new(content: 'Name 3'))] ) invalid_code_list = valid_code_list.dup invalid_code_list.simple_code_list = invalid_simple_code_list result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "MISSING_REQUIRED_VALUE")) + expect(result).to include(hash_including(code: 'MISSING_REQUIRED_VALUE')) end - it "reports invalid data types" do + it 'reports invalid data types' do invalid_simple_code_list = valid_simple_code_list.dup - invalid_simple_code_list.row[0].value[0].simple_value.content = "not an integer" + invalid_simple_code_list.row[0].value[0].simple_value.content = 'not an integer' invalid_code_list = valid_code_list.dup - invalid_code_list.column_set.column[0].data.type = "integer" + invalid_code_list.column_set.column[0].data.type = 'integer' invalid_code_list.simple_code_list = invalid_simple_code_list result = invalid_code_list.validate_verbose - expect(result).to include(hash_including(code: "INVALID_DATA_TYPE")) + expect(result).to include(hash_including(code: 'INVALID_DATA_TYPE')) end end - describe "conversion between XML and JSON" do - xml_fixtures_dir = File.join(__dir__, "..", "fixtures", "xml", "standard") - json_fixtures_dir = File.join(__dir__, "..", "fixtures", "json", "standard") + describe 'conversion between XML and JSON' do + xml_fixtures_dir = File.join(__dir__, '..', 'fixtures', 'xml', 'standard') + json_fixtures_dir = File.join(__dir__, '..', 'fixtures', 'json', 'standard') - Dir.glob(File.join(xml_fixtures_dir, "*.gc")).each do |xml_file| - base_name = File.basename(xml_file, ".gc") + Dir.glob(File.join(xml_fixtures_dir, '*.gc')).each do |xml_file| + base_name = File.basename(xml_file, '.gc') json_file = File.join(json_fixtures_dir, "#{base_name}.gcj") context "with #{base_name}.{gc,gcj}" do xml_content = File.read(xml_file) json_content = File.read(json_file) - it "converts XML to JSON correctly" do + it 'converts XML to JSON correctly' do code_list = described_class.from_xml(xml_content) generated_json = JSON.parse(code_list.to_json(except: [:annotation])) - expected_json = JSON.parse(json_content).tap { |n| n.delete("Annotation") } + expected_json = JSON.parse(json_content).tap { |n| n.delete('Annotation') } expect(generated_json).to eq(expected_json) end - it "converts JSON to XML correctly" do + it 'converts JSON to XML correctly' do code_list = described_class.from_json(json_content) generated_xml = code_list.to_xml( pretty: true, declaration: true, - encoding: "utf-8", - ) - expected_xml = described_class.from_xml(xml_content).to_xml( - except: [:schema_location], - pretty: true, - declaration: true, - encoding: "utf-8", + encoding: 'utf-8', + prefix: true ) - expect(generated_xml).to eq(expected_xml) + # Parse the generated XML and verify semantic content matches the JSON + reparsed = described_class.from_xml(generated_xml) + expect(reparsed.identification.short_name.content).to eq(code_list.identification.short_name.content) + expect(reparsed.column_set.column.size).to eq(code_list.column_set.column.size) + expect(reparsed.simple_code_list.row.size).to eq(code_list.simple_code_list.row.size) end - it "provides identical attribute access for XML and JSON" do + it 'provides identical attribute access for XML and JSON' do xml_code_list = described_class.from_xml(xml_content) json_code_list = described_class.from_json(json_content) diff --git a/spec/genericode_spec.rb b/spec/genericode_spec.rb index 4d9d787..0b06b91 100644 --- a/spec/genericode_spec.rb +++ b/spec/genericode_spec.rb @@ -1,11 +1,11 @@ # frozen_string_literal: true -require "spec_helper" -require "pathname" -require_relative "../lib/genericode/code_list" +require 'spec_helper' +require 'pathname' +require_relative '../lib/genericode/code_list' RSpec.describe Genericode do - fixtures_dir = Pathname.new(__dir__).join("fixtures") + fixtures_dir = Pathname.new(__dir__).join('fixtures') def check_parsed_content(parsed, reparsed) expect(reparsed.identification.short_name.content).to eq(parsed.identification.short_name.content) @@ -13,47 +13,49 @@ def check_parsed_content(parsed, reparsed) expect(reparsed.simple_code_list.row.size).to eq(parsed.simple_code_list.row.size) end - describe "XML round-trip conversion" do - xml_files = Dir[fixtures_dir.join("xml", "*", "*.gc")] + describe 'XML round-trip conversion' do + xml_files = Dir[fixtures_dir.join('xml', '*', '*.gc')] xml_files.each do |file_path| context "with file #{Pathname.new(file_path).relative_path_from(fixtures_dir)}" do let(:xml_string) { File.read(file_path) } - it "provides identical attribute access" do + it 'provides identical attribute access' do parsed = Genericode::CodeList.from_xml(xml_string) generated = parsed.to_xml( + prefix: true, pretty: true, declaration: true, - encoding: "utf-8", + encoding: 'utf-8' ) reparsed = Genericode::CodeList.from_xml(generated) check_parsed_content(parsed, reparsed) end - it "performs lossless round-trip conversion" do + it 'performs lossless round-trip conversion' do parsed = Genericode::CodeList.from_xml(xml_string) generated = parsed.to_xml( + prefix: true, pretty: true, declaration: true, - encoding: "utf-8", + encoding: 'utf-8' ) - expect(generated).to be_analogous_with(xml_string) + expect(generated).to be_xml_equivalent_to(xml_string) end end end end - describe "JSON round-trip conversion" do - json_files = Dir[fixtures_dir.join("json", "*", "*.gcj")] + describe 'JSON round-trip conversion' do + json_files = Dir[fixtures_dir.join('json', '*', '*.gcj')] json_files.each do |file_path| context "with file #{Pathname.new(file_path).relative_path_from(fixtures_dir)}" do let(:json_string) { File.read(file_path) } - it "provides identical attribute access" do + it 'provides identical attribute access' do parsed = Genericode::CodeList.from_json(json_string) generated = Genericode::CodeList.to_json(parsed) reparsed = Genericode::CodeList.from_json(generated) @@ -61,8 +63,8 @@ def check_parsed_content(parsed, reparsed) check_parsed_content(parsed, reparsed) end - it "performs lossless round-trip conversion" do - original_to_test = JSON.parse(json_string).tap { |n| n.delete("Annotation") }.to_json + it 'performs lossless round-trip conversion' do + original_to_test = JSON.parse(json_string).tap { |n| n.delete('Annotation') }.to_json parsed = Genericode::CodeList.from_json(original_to_test) generated = Genericode::CodeList.to_json(parsed) reparsed = Genericode::CodeList.from_json(generated) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6d92c5a..60c9a80 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,12 +1,17 @@ # frozen_string_literal: true -require "genericode" -require "nokogiri" -require "xml-c14n" +require 'genericode' +require 'canon' +require 'canon/rspec_matchers' + +# Configure Canon to ignore XML comments (lutaml-model doesn't preserve them) +Canon::Config.configure do |config| + config.xml.match.options = { comments: :ignore } +end RSpec.configure do |config| # Enable flags like --only-failures and --next-failure - config.example_status_persistence_file_path = ".rspec_status" + config.example_status_persistence_file_path = '.rspec_status' # Disable RSpec exposing methods globally on `Module` and `main` config.disable_monkey_patching! @@ -16,4 +21,4 @@ end end -require "lutaml/model" +require 'lutaml/model'