Skip to content

Commit

Permalink
chore: rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Feb 8, 2025
1 parent c5543f5 commit 972896e
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 57 deletions.
27 changes: 24 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-02-08 00:51:12 UTC using RuboCop version 1.71.2.
# on 2025-02-08 01:06:17 UTC using RuboCop version 1.71.2.
# 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: 633
# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
Layout/ElseAlignment:
Exclude:
- 'lib/lutaml/model/schema/xml_compiler.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleAlignWith, Severity.
# SupportedStylesAlignWith: keyword, variable, start_of_line
Layout/EndAlignment:
Exclude:
- 'lib/lutaml/model/schema/xml_compiler.rb'

# Offense count: 2
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Width, AllowedPatterns.
Layout/IndentationWidth:
Exclude:
- 'lib/lutaml/model/schema/xml_compiler.rb'

# Offense count: 596
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Expand Down Expand Up @@ -80,7 +101,7 @@ Metrics/CyclomaticComplexity:
# Offense count: 87
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 44
Max: 46

# Offense count: 8
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
4 changes: 3 additions & 1 deletion lib/lutaml/model/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def attribute(name, type, options = {})
end

def choice(min: 1, max: 1, &block)
@attributes << Choice.new(@model, min, max).tap { |c| c.instance_eval(&block) }
@attributes << Choice.new(@model, min, max).tap do |c|
c.instance_eval(&block)
end
end

def validate_content!(object)
Expand Down
37 changes: 24 additions & 13 deletions lib/lutaml/model/schema/xml_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,20 @@ def to_models(schema, options = {})
if options[:create_files]
dir = options.fetch(:output_dir, "lutaml_models_#{Time.now.to_i}")
FileUtils.mkdir_p(dir)
@data_types_classes.each { |name, content| create_file(name, content, dir) }
@complex_types.each { |name, content| create_file(name, MODEL_TEMPLATE.result(binding), dir) }
@data_types_classes.each do |name, content|
create_file(name, content, dir)
end
@complex_types.each do |name, content|
create_file(name, MODEL_TEMPLATE.result(binding), dir)
end
nil
else
simple_types = @data_types_classes.transform_keys { |key| Utils.camel_case(key.to_s) }
complex_types = @complex_types.to_h { |name, content| [Utils.camel_case(name), MODEL_TEMPLATE.result(binding)] }
simple_types = @data_types_classes.transform_keys do |key|
Utils.camel_case(key.to_s)
end
complex_types = @complex_types.to_h do |name, content|
[Utils.camel_case(name), MODEL_TEMPLATE.result(binding)]
end
classes_hash = simple_types.merge(complex_types)
require_classes(classes_hash) if options[:load_classes]
classes_hash
Expand Down Expand Up @@ -255,18 +263,18 @@ def setup_sequence(sequence)
hash[:sequences] << setup_sequence(instance)
when Xsd::Element
hash[:elements] << if instance.name
setup_element(instance)
else
create_mapping_hash(instance.ref, hash_key: :ref_class)
end
setup_element(instance)
else
create_mapping_hash(instance.ref, hash_key: :ref_class)
end
when Xsd::Choice
hash[:choice] << setup_choice(instance)
when Xsd::Group
hash[:groups] << if instance.name
setup_group_type(instance)
else
create_mapping_hash(instance.ref, hash_key: :ref_class)
end
setup_group_type(instance)
else
create_mapping_hash(instance.ref, hash_key: :ref_class)
end
when Xsd::Any
# No implementation yet!
end
Expand Down Expand Up @@ -381,7 +389,7 @@ def setup_restriction(restriction, hash)
def restriction_patterns(patterns, hash)
return if patterns.empty?

hash[:pattern] = patterns.map { |pattern| "(#{pattern.value})" }.join("|")
hash[:pattern] = patterns.map { |p| "(#{p.value})" }.join("|")
hash
end

Expand Down Expand Up @@ -438,6 +446,7 @@ def resolved_element_order(object)
end
end
end

# END: STRUCTURE SETUP METHODS

# START: TEMPLATE RESOLVER METHODS
Expand Down Expand Up @@ -611,6 +620,7 @@ def resolve_required_files(content)
end
@required_files.uniq.sort_by(&:length)
end

# END: TEMPLATE RESOLVER METHODS

# START: REQUIRED FILES LIST COMPILER METHODS
Expand Down Expand Up @@ -744,6 +754,7 @@ def required_files_elements(elements)
@required_files << Utils.snake_case(element_class)
end
end

# END: REQUIRED FILES LIST COMPILER METHODS
end
end
Expand Down
12 changes: 9 additions & 3 deletions lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ def cast(value)
end

def choice(min: 1, max: 1, &block)
@choice_attributes << Choice.new(self, min, max).tap { |c| c.instance_eval(&block) }
@choice_attributes << Choice.new(self, min, max).tap do |c|
c.instance_eval(&block)
end
end

# Define an attribute for the model
Expand Down Expand Up @@ -497,7 +499,9 @@ def apply_xml_mapping(doc, instance, options = {})
instance.mixed = mappings_for(:xml).mixed_content? || options[:mixed_content]
end

schema_location = doc.attributes.values.find { |a| a.unprefixed_name == "schemaLocation" }
schema_location = doc.attributes.values.find do |a|
a.unprefixed_name == "schemaLocation"
end

if !schema_location.nil?
instance.schema_location = Lutaml::Model::SchemaLocation.new(
Expand Down Expand Up @@ -558,7 +562,9 @@ def value_for_rule(doc, rule, options, instance)
end

if rule.cdata
values = children.map { |child| child.cdata_children&.map(&:text) }.flatten
values = children.map do |child|
child.cdata_children&.map(&:text)
end.flatten
return children.count > 1 ? values : values.first
end

Expand Down
4 changes: 3 additions & 1 deletion lib/lutaml/model/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def validate!
end

def validate_helper(errors)
self.class.choice_attributes.each { |attribute| attribute.validate_content!(self) }
self.class.choice_attributes.each do |attribute|
attribute.validate_content!(self)
end
errors
rescue Lutaml::Model::ChoiceUpperBoundError,
Lutaml::Model::ChoiceLowerBoundError => e
Expand Down
8 changes: 6 additions & 2 deletions lib/lutaml/model/xml_adapter/xml_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ def text_children

def find_attribute_value(attribute_name)
if attribute_name.is_a?(Array)
attributes.values.find { |attr| attribute_name.include?(attr.namespaced_name) }&.value
attributes.values.find do |attr|
attribute_name.include?(attr.namespaced_name)
end&.value
else
attributes.values.find { |attr| attribute_name == attr.namespaced_name }&.value
attributes.values.find do |attr|
attribute_name == attr.namespaced_name
end&.value
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/lutaml/model/xml_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ def map_all(
alias map_all_content map_all

def sequence(&block)
@element_sequence << Sequence.new(self).tap { |s| s.instance_eval(&block) }
@element_sequence << Sequence.new(self).tap do |s|
s.instance_eval(&block)
end
end

def import_model_mappings(model)
Expand Down
12 changes: 9 additions & 3 deletions spec/lutaml/model/choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class PersonDetails < Lutaml::Model::Serializable
encrypted: true,
)

expect { valid_instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
valid_instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("Attributes `[:signed, :unsigned, :watermarked, :encrypted]` count exceeds the upper bound `3`")
end
end
Expand Down Expand Up @@ -132,7 +134,9 @@ class PersonDetails < Lutaml::Model::Serializable
insta: "insta",
)

expect { valid_instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
valid_instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to eq("Attributes `[:email, :phone, :check]` count exceeds the upper bound `2`")
end
end
Expand All @@ -143,7 +147,9 @@ class PersonDetails < Lutaml::Model::Serializable
insta: "insta",
)

expect { valid_instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
valid_instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to eq("Attributes `[]` count is less than the lower bound `1`")
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lutaml/model/defaults_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ class CustomModelWithDefaultValue < Lutaml::Model::Serializable
model Lang

attribute :lang, :string, default: -> { "en" }
attribute :content, :string, default: -> { "default value not render when render_default is false" }
attribute :content, :string, default: -> {
"default value not render when render_default is false"
}

xml do
root "CustomModelWithDefaultValue"
Expand Down
8 changes: 6 additions & 2 deletions spec/lutaml/model/liquefiable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def display_name
describe ".register_liquid_drop_class" do
context "when drop class does not exist" do
it "creates a new drop class" do
expect { dummy.class.register_liquid_drop_class }.to change { dummy.class.const_defined?(:DummyModelDrop) }
expect { dummy.class.register_liquid_drop_class }.to change {
dummy.class.const_defined?(:DummyModelDrop)
}
.from(false)
.to(true)
end
Expand Down Expand Up @@ -67,7 +69,9 @@ def display_name
end

it "defines a method on the drop class" do
expect { dummy.class.register_drop_method(:display_name) }.to change { dummy.to_liquid.respond_to?(:display_name) }
expect { dummy.class.register_drop_method(:display_name) }.to change {
dummy.to_liquid.respond_to?(:display_name)
}
.from(false)
.to(true)
end
Expand Down
56 changes: 42 additions & 14 deletions spec/lutaml/model/mixed_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,9 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".from_xml" do
let(:expected_content) { "Moon&Mars Distanced©its — surface covered & processed" }
let(:expected_content) do
"Moon&Mars Distanced©its — surface covered & processed"
end

it "deserializes special char mixed content correctly" do
parsed = MixedContentSpec::SpecialCharContentWithMixedTrue.from_xml(xml)
Expand All @@ -476,7 +478,9 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".to_xml" do
let(:expected_xml) { "Moon&amp;Mars Distanced©its — surface covered &amp; processed" }
let(:expected_xml) do
"Moon&amp;Mars Distanced©its — surface covered &amp; processed"
end

it "serializes special char mixed content correctly" do
parsed = MixedContentSpec::SpecialCharContentWithMixedTrue.from_xml(xml)
Expand Down Expand Up @@ -522,9 +526,15 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".from_xml" do
let(:expected_nokogiri_content) { "B <p>R&amp;C</p>\n C <p>J&#x2014;C</p>\n O <p>A &amp; B </p>\n F <p>Z &#xA9;S</p>" }
let(:expected_ox_content) { "B <p>R&amp;C</p> C <p>J—C</p> O <p>A &amp; B </p> F <p>Z ©S</p>" }
let(:expected_oga_content) { "B <p>R&amp;C</p>\n C <p>J—C</p>\n O <p>A &amp; B </p>\n F <p>Z ©S</p>" }
let(:expected_nokogiri_content) do
"B <p>R&amp;C</p>\n C <p>J&#x2014;C</p>\n O <p>A &amp; B </p>\n F <p>Z &#xA9;S</p>"
end
let(:expected_ox_content) do
"B <p>R&amp;C</p> C <p>J—C</p> O <p>A &amp; B </p> F <p>Z ©S</p>"
end
let(:expected_oga_content) do
"B <p>R&amp;C</p>\n C <p>J—C</p>\n O <p>A &amp; B </p>\n F <p>Z ©S</p>"
end

it "deserializes special char mixed content correctly" do
parsed = MixedContentSpec::SpecialCharContentWithRawAndMixedOption.from_xml(xml)
Expand Down Expand Up @@ -627,7 +637,9 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".from_xml" do
let(:expected_content) { "<computer security> type of operation specified by an access right" }
let(:expected_content) do
"<computer security> type of operation specified by an access right"
end

it "deserializes special char mixed content correctly" do
parsed = MixedContentSpec::TextualSupport.from_xml(xml)
Expand All @@ -637,8 +649,12 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".to_xml" do
let(:expected_xml) { "<TextualSupport>\n <value>&lt;computer security&gt; type of operation specified by an access right</value>\n</TextualSupport>" }
let(:expected_oga_xml) { "<TextualSupport><value>&lt;computer security&gt; type of operation specified by an access right</value></TextualSupport>" }
let(:expected_xml) do
"<TextualSupport>\n <value>&lt;computer security&gt; type of operation specified by an access right</value>\n</TextualSupport>"
end
let(:expected_oga_xml) do
"<TextualSupport><value>&lt;computer security&gt; type of operation specified by an access right</value></TextualSupport>"
end

it "serializes special char mixed content correctly" do
parsed = MixedContentSpec::TextualSupport.from_xml(xml)
Expand All @@ -659,7 +675,9 @@ class HexCode < Lutaml::Model::Serializable
end

describe ".from_xml" do
let(:expected_content) { "∑computer security∏ type of ​ operation specified µ by an access right" }
let(:expected_content) do
"∑computer security∏ type of ​ operation specified µ by an access right"
end

it "deserializes special char mixed content correctly" do
parsed = MixedContentSpec::HexCode.from_xml(xml)
Expand All @@ -670,7 +688,9 @@ class HexCode < Lutaml::Model::Serializable

describe ".to_xml" do
context "when default encoding xml" do
let(:expected_default_encoding_xml) { "∑computer security∏ type of ​ operation specified µ by an access right" }
let(:expected_default_encoding_xml) do
"∑computer security∏ type of ​ operation specified µ by an access right"
end

it "serializes special char mixed content correctly with default encoding: UTF-8" do
parsed = MixedContentSpec::HexCode.from_xml(xml)
Expand All @@ -681,9 +701,15 @@ class HexCode < Lutaml::Model::Serializable
end

context "when encoding: nil xml" do
let(:expected_encoding_nil_nokogiri_xml) { "&#x2211;computer security&#x220F; type of &#x200B; operation specified &#xB5; by an access right" }
let(:expected_encoding_nil_ox_xml) { "<HexCode> \xE2\x88\x91computer security\xE2\x88\x8F type of \xE2\x80\x8B operation specified \xC2\xB5 by an access right </HexCode>\n".force_encoding("ASCII-8BIT") }
let(:expected_encoding_nil_oga_xml) { "<HexCode>\n ∑computer security∏ type of ​ operation specified µ by an access right\n</HexCode>" }
let(:expected_encoding_nil_nokogiri_xml) do
"&#x2211;computer security&#x220F; type of &#x200B; operation specified &#xB5; by an access right"
end
let(:expected_encoding_nil_ox_xml) do
"<HexCode> \xE2\x88\x91computer security\xE2\x88\x8F type of \xE2\x80\x8B operation specified \xC2\xB5 by an access right </HexCode>\n".force_encoding("ASCII-8BIT")
end
let(:expected_encoding_nil_oga_xml) do
"<HexCode>\n ∑computer security∏ type of ​ operation specified µ by an access right\n</HexCode>"
end

it "serializes special char mixed content correctly with encoding: nil to get hexcode" do
parsed = MixedContentSpec::HexCode.from_xml(xml, encoding: nil)
Expand Down Expand Up @@ -712,7 +738,9 @@ class HexCode < Lutaml::Model::Serializable

context "when use encoding in parsing" do
context "when use SHIFT-JIS encoding" do
let(:fixture) { File.read(fixture_path("xml/shift_jis.xml"), encoding: "Shift_JIS") }
let(:fixture) do
File.read(fixture_path("xml/shift_jis.xml"), encoding: "Shift_JIS")
end

describe ".from_xml" do
it "verifies the encoding of file read" do
Expand Down
Loading

0 comments on commit 972896e

Please sign in to comment.