Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Lutaml::Model #13

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inherit_from: .rubocop_todo.yml

AllCops:
NewCops: enable
SuggestExtensions: false
Expand Down
51 changes: 51 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-08-27 09:46:41 UTC using RuboCop version 1.65.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: 1
# Configuration parameters: IgnoreLiteralBranches, IgnoreConstantBranches.
Lint/DuplicateBranch:
Exclude:
- 'lib/genericode/code_list.rb'

# Offense count: 8
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 182

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 34

# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 220

# Offense count: 5
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 92

# Offense count: 10
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 86

# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 92

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 121
1 change: 1 addition & 0 deletions exe/genericode
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative "../lib/genericode"
require_relative "../lib/genericode/cli"
Expand Down
4 changes: 2 additions & 2 deletions genericode.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "shale", "~> 1.0"
spec.add_dependency "thor"
spec.add_dependency "lutaml-model"
spec.add_dependency "tabulo"
spec.add_dependency "thor"

spec.add_development_dependency "nokogiri"
spec.add_development_dependency "rake"
Expand Down
9 changes: 5 additions & 4 deletions lib/genericode.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# frozen_string_literal: true

require "shale"
unless Shale.xml_adapter
require "shale/adapter/nokogiri"
Shale.xml_adapter = Shale::Adapter::Nokogiri
require "lutaml/model"

Lutaml::Model::Config.configure do |config|
require "lutaml/model/xml_adapter/nokogiri_adapter"
config.xml_adapter = Lutaml::Model::XmlAdapter::NokogiriAdapter
end

require_relative "genericode/version"
Expand Down
10 changes: 5 additions & 5 deletions lib/genericode/agency.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "shale"
require "lutaml/model"

require_relative "general_identifier"
require_relative "long_name"
Expand All @@ -9,17 +9,17 @@
require_relative "utils"

module Genericode
class Agency < Shale::Mapper
class Agency < Lutaml::Model::Serializable
include Json::ShortNameMixin

attribute :short_name, ShortName
attribute :long_name, LongName, collection: true
attribute :identifier, GeneralIdentifier, collection: true

json do
map "ShortName", to: :short_name, using: { from: :short_name_from_json, to: :short_name_to_json }
map "LongName", to: :long_name, using: { from: :long_name_from_json, to: :long_name_to_json }
map "Identifier", to: :identifier, using: { 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)
Expand Down
8 changes: 4 additions & 4 deletions lib/genericode/annotation.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# frozen_string_literal: true

require "shale"
require "lutaml/model"

require_relative "any_other_content"
require_relative "any_other_language_content"

module Genericode
class Annotation < Shale::Mapper
class Annotation < Lutaml::Model::Serializable
attribute :description, AnyOtherLanguageContent, collection: true
attribute :app_info, AnyOtherContent

json do
map "Description", to: :description
map "AppInfo", to: :app_info
map "AppInfo", to: :app_info, render_nil: true
end

def self.of_json(hash, **)
Expand All @@ -26,7 +26,7 @@ def self.of_json(hash, **)
namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc"

map_element "Description", to: :description, prefix: nil, namespace: nil
map_element "AppInfo", to: :app_info, prefix: nil, namespace: nil
map_element "AppInfo", to: :app_info, prefix: nil, namespace: nil, render_nil: true
end
end
end
4 changes: 2 additions & 2 deletions lib/genericode/any_other_content.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

require "shale"
require "lutaml/model"

module Genericode
class AnyOtherContent < Shale::Mapper
class AnyOtherContent < Lutaml::Model::Serializable
xml do
root "AnyOtherContent"
namespace "http://docs.oasis-open.org/codelist/ns/genericode/1.0/", "gc"
Expand Down
6 changes: 3 additions & 3 deletions lib/genericode/any_other_language_content.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require "shale"
require "lutaml/model"

module Genericode
class AnyOtherLanguageContent < Shale::Mapper
attribute :lang, Shale::Type::String
class AnyOtherLanguageContent < Lutaml::Model::Serializable
attribute :lang, :string

json do
map "lang", to: :lang
Expand Down
6 changes: 3 additions & 3 deletions lib/genericode/canonical_uri.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

require "shale"
require "lutaml/model"
require "uri"

module Genericode
# Rule 4: Must be an absolute URI, must not be relative
class CanonicalUri < Shale::Mapper
attribute :content, Shale::Type::String
class CanonicalUri < Lutaml::Model::Serializable
attribute :content, :string

xml do
root "CanonicalUri"
Expand Down
2 changes: 2 additions & 0 deletions lib/genericode/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Genericode
module Cli
end
Expand Down
6 changes: 3 additions & 3 deletions lib/genericode/cli/code_lister.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../code_list"
require "tabulo"
require "csv"
Expand All @@ -12,10 +14,8 @@ 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"
end

# Ensure valid ColumnRefs
code_list.validate_verbose.each do |error|
# Ensure valid ColumnRefs
raise Error, "#{error[:code]}: #{error[:message]}" if error[:code] == "INVALID_COLUMN_REF"
end

Expand Down
2 changes: 2 additions & 0 deletions lib/genericode/cli/code_lookup.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative "../code_list"

module Genericode
Expand Down
16 changes: 7 additions & 9 deletions lib/genericode/cli/commands.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "thor"
require_relative "validator"
require_relative "converter"
Expand All @@ -10,9 +12,7 @@ class Commands < Thor
desc "convert INPUT OUTPUT", "Convert between Genericode XML and JSON formats"

def convert(input, output)
if Converter.convert(input, output)
puts "Conversion successful."
end
puts "Conversion successful." if Converter.convert(input, output)
rescue Error => e
puts "Conversion failed: #{e.message}"
end
Expand All @@ -32,19 +32,17 @@ def validate(file)
puts " [#{error[:code]}] #{error[:message]}"
end
end
elsif code_list.valid?
puts "File is valid."
else
if code_list.valid?
puts "File is valid."
else
puts "File is invalid."
end
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: ["tsv", "table"], desc: "Output format (tsv or table)"
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)
Expand Down
10 changes: 6 additions & 4 deletions lib/genericode/cli/converter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Genericode
module Cli
class Converter
Expand All @@ -13,10 +15,10 @@ def self.convert(input_path, output_path)
code_list = CodeList.from_file(input_path)

result = if output_format == ".gcj"
code_list.to_json
else
code_list.to_xml
end
code_list.to_json
else
code_list.to_xml
end

File.write(output_path, result)
true
Expand Down
2 changes: 2 additions & 0 deletions lib/genericode/cli/validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Genericode
module Cli
class Validator
Expand Down
Loading
Loading