Skip to content

Commit

Permalink
chore: rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Sep 10, 2024
1 parent 274466a commit 509a4d9
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 71 deletions.
37 changes: 22 additions & 15 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-09-08 11:19:50 UTC using RuboCop version 1.66.1.
# on 2024-09-10 23:53:08 UTC using RuboCop version 1.66.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
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Severity, Include.
# Include: **/*.gemspec
Gemspec/RequireMFA:
Exclude:
- 'lutaml-model.gemspec'

# Offense count: 66
# Offense count: 88
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
# URISchemes: http, https
Layout/LineLength:
Enabled: false

# Offense count: 10
# Offense count: 1
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'lib/lutaml/model/schema_location.rb'

# Offense count: 11
# Configuration parameters: AllowedMethods.
# AllowedMethods: enums
Lint/ConstantDefinitionInBlock:
Expand All @@ -30,6 +29,7 @@ Lint/ConstantDefinitionInBlock:
- 'spec/lutaml/model/schema/relaxng_schema_spec.rb'
- 'spec/lutaml/model/schema/xsd_schema_spec.rb'
- 'spec/lutaml/model/schema/yaml_schema_spec.rb'
- 'spec/lutaml/model/validation_spec.rb'
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'

# Offense count: 1
Expand Down Expand Up @@ -100,7 +100,7 @@ RSpec/ContextWording:
- 'spec/lutaml/model/xml_adapter/ox_adapter_spec.rb'
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'

# Offense count: 89
# Offense count: 101
# Configuration parameters: CountAsOne.
RSpec/ExampleLength:
Max: 57
Expand All @@ -111,13 +111,14 @@ RSpec/IndexedLet:
Exclude:
- 'spec/address_spec.rb'

# Offense count: 18
# Offense count: 19
RSpec/LeakyConstantDeclaration:
Exclude:
- 'spec/lutaml/model/schema/json_schema_spec.rb'
- 'spec/lutaml/model/schema/relaxng_schema_spec.rb'
- 'spec/lutaml/model/schema/xsd_schema_spec.rb'
- 'spec/lutaml/model/schema/yaml_schema_spec.rb'
- 'spec/lutaml/model/validation_spec.rb'
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'

# Offense count: 4
Expand All @@ -128,7 +129,7 @@ RSpec/MultipleDescribes:
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
- 'spec/lutaml/model/xml_adapter_spec.rb'

# Offense count: 75
# Offense count: 88
RSpec/MultipleExpectations:
Max: 11

Expand All @@ -137,10 +138,11 @@ RSpec/MultipleExpectations:
RSpec/MultipleMemoizedHelpers:
Max: 9

# Offense count: 4
# Offense count: 7
RSpec/PendingWithoutReason:
Exclude:
- 'spec/lutaml/model/mixed_content_spec.rb'
- 'spec/lutaml/model/validation_spec.rb'
- 'spec/lutaml/model/xml_adapter/oga_adapter_spec.rb'
- 'spec/lutaml/model/xml_adapter/xml_namespace_spec.rb'
- 'spec/lutaml/model/xml_adapter_spec.rb'
Expand All @@ -163,6 +165,11 @@ Security/CompoundHash:
Exclude:
- 'lib/lutaml/model/comparable_model.rb'

# Offense count: 1
Style/MissingRespondToMissing:
Exclude:
- 'lib/lutaml/model/serialize.rb'

# Offense count: 1
# Configuration parameters: AllowedMethods.
# AllowedMethods: respond_to_missing?
Expand Down
2 changes: 1 addition & 1 deletion lib/lutaml/model/error/validation_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(errors)
end

def include?(error_class)
errors.any? { |error| error.is_a?(error_class) }
errors.any?(error_class)
end

def error_messages
Expand Down
3 changes: 2 additions & 1 deletion lib/lutaml/model/schema_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class SchemaLocation

attr_reader :namespace, :prefix, :schema_location

def initialize(schema_location:, prefix: "xsi", namespace: DEFAULT_NAMESPACE)
def initialize(schema_location:, prefix: "xsi",
namespace: DEFAULT_NAMESPACE)
@original_schema_location = schema_location
@schema_location = parsed_schema_locations(schema_location)
@prefix = prefix
Expand Down
78 changes: 39 additions & 39 deletions lib/lutaml/model/serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def hash_representation(instance, format, options = {})
attribute = attributes[name]

hash[rule.from] = if rule.child_mappings
generate_hash_from_child_mappings(value, rule.child_mappings)
else
attribute.serialize(value, format, options)
end
generate_hash_from_child_mappings(value, rule.child_mappings)
else
attribute.serialize(value, format, options)
end
end
end

Expand All @@ -159,12 +159,12 @@ def mappings_for(format)

def attr_value(attrs, name, attr_rule)
value = if attrs.key?(name.to_sym)
attrs[name.to_sym]
elsif attrs.key?(name.to_s)
attrs[name.to_s]
else
attr_rule.default
end
attrs[name.to_sym]
elsif attrs.key?(name.to_s)
attrs[name.to_s]
else
attr_rule.default
end

if attr_rule.collection? || value.is_a?(Array)
(value || []).map do |v|
Expand Down Expand Up @@ -202,13 +202,13 @@ def apply_child_mappings(hash, child_mappings)
hash.map do |key, value|
child_mappings.to_h do |attr_name, path|
attr_value = if path == :key
key
elsif path == :value
value
else
path = [path] unless path.is_a?(Array)
value.dig(*path.map(&:to_s))
end
key
elsif path == :value
value
else
path = [path] unless path.is_a?(Array)
value.dig(*path.map(&:to_s))
end

[attr_name, attr_value]
end
Expand Down Expand Up @@ -250,18 +250,18 @@ def apply_mappings(doc, format, options = {})
mappings = mappings_for(format).mappings
mappings.each do |rule|
attr = if rule.delegate
attributes[rule.delegate].type.attributes[rule.to]
else
attributes[rule.to]
end
attributes[rule.delegate].type.attributes[rule.to]
else
attributes[rule.to]
end

raise "Attribute '#{rule.to}' not found in #{self}" unless attr

value = if doc.key?(rule.name) || doc.key?(rule.name.to_sym)
doc[rule.name] || doc[rule.name.to_sym]
else
attr.default
end
doc[rule.name] || doc[rule.name.to_sym]
else
attr.default
end

if rule.custom_methods[:from]
if value && !value.empty?
Expand Down Expand Up @@ -319,10 +319,10 @@ def apply_xml_mapping(doc, instance, options = {})
is_content_mapping = rule.name.nil?

value = if is_content_mapping
doc["text"]
else
doc[rule.name.to_s] || doc[rule.name.to_sym]
end
doc["text"]
else
doc[rule.name.to_s] || doc[rule.name.to_sym]
end

value = [value].compact if attr.collection? && !value.is_a?(Array)

Expand Down Expand Up @@ -390,10 +390,10 @@ def initialize(attrs = {})

self.class.attributes.each do |name, attr|
value = if attrs.key?(name) || attrs.key?(name.to_s)
self.class.attr_value(attrs, name, attr)
else
attr.default
end
self.class.attr_value(attrs, name, attr)
else
attr.default
end

# Initialize collections with an empty array if no value is provided
if attr.collection? && value.nil?
Expand All @@ -407,7 +407,7 @@ def initialize(attrs = {})
def method_missing(method_name, *args)
if method_name.to_s.end_with?("=") && self.class.attributes.key?(method_name.to_s.chomp("=").to_sym)
define_singleton_method(method_name) do |value|
instance_variable_set(:"@#{method_name.to_s.chomp("=")}", value)
instance_variable_set(:"@#{method_name.to_s.chomp('=')}", value)
end
send(method_name, *args)
else
Expand Down Expand Up @@ -442,11 +442,11 @@ def key_value(hash, key)
validate!
adapter = Lutaml::Model::Config.public_send(:"#{format}_adapter")
representation = if format == :xml
self
else
self.class.hash_representation(self, format,
options)
end
self
else
self.class.hash_representation(self, format,
options)
end

adapter.new(representation).public_send(:"to_#{format}", options)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/lutaml/model/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def validate
value = instance_variable_get(:"@#{name}")
begin
attr.validate_value!(value)
rescue Lutaml::Model::InvalidValueError, Lutaml::Model::CollectionCountOutOfRangeError => e
rescue Lutaml::Model::InvalidValueError,
Lutaml::Model::CollectionCountOutOfRangeError => e
errors << e
end
end
Expand Down
1 change: 1 addition & 0 deletions lutaml-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "bigdecimal"
spec.add_dependency "thor"
spec.metadata["rubygems_mfa_required"] = "true"
end
12 changes: 9 additions & 3 deletions spec/lutaml/model/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,19 @@ class Kiln < Lutaml::Model::Serializable

it "raises ValidationError containing CollectionCountOutOfRangeError for operators" do
kiln = CollectionTests::Kiln.new(invalid_attributes)
expect { kiln.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
kiln.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error).to include(Lutaml::Model::CollectionCountOutOfRangeError)
expect(error.error_messages).to include(a_string_matching(/operators count is 0, must be at least 1/))
end
end

it "raises ValidationError containing CollectionCountOutOfRangeError for sensors" do
kiln = CollectionTests::Kiln.new(attributes.merge(sensors: []))
expect { kiln.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
kiln.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error).to include(Lutaml::Model::CollectionCountOutOfRangeError)
expect(error.error_messages).to include(a_string_matching(/sensors count is 0, must be between 1 and 3/))
end
Expand All @@ -138,7 +142,9 @@ class Kiln < Lutaml::Model::Serializable

it "raises CollectionCountOutOfRangeError" do
kiln = CollectionTests::Kiln.new(invalid_attributes)
expect { kiln.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
kiln.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error).to include(Lutaml::Model::CollectionCountOutOfRangeError)
expect(error.error_messages).to include(a_string_matching(/operators count is 0, must be at least 1/))
end
Expand Down
12 changes: 8 additions & 4 deletions spec/lutaml/model/serializable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class GlazeTechnique < Lutaml::Model::Serializable
describe ".model" do
it "sets the model for the class" do
expect { described_class.model("Foo") }.to change(described_class, :model)
.from(nil)
.to("Foo")
.from(nil)
.to("Foo")
end
end

Expand Down Expand Up @@ -223,7 +223,9 @@ class GlazeTechnique < Lutaml::Model::Serializable
it "raises an error after creation after validate" do
glaze = GlazeTechnique.new(name: "Celadon")
glaze.name = "Tenmoku"
expect { glaze.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
glaze.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error).to include(Lutaml::Model::InvalidValueError)
expect(error.error_messages).to include("name is `Tenmoku`, must be one of the following [Celadon, Raku, Majolica]")
end
Expand All @@ -249,7 +251,9 @@ class GlazeTechnique < Lutaml::Model::Serializable
it "raises ValidationError containing InvalidValueError after creation" do
glaze = GlazeTechnique.new(name: "Celadon")
glaze.name = "Tenmoku"
expect { glaze.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
glaze.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error).to include(Lutaml::Model::InvalidValueError)
expect(error.error_messages).to include(a_string_matching(/name is `Tenmoku`, must be one of the following/))
end
Expand Down
25 changes: 18 additions & 7 deletions spec/lutaml/model/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,31 @@ class ValidationTestClass < Lutaml::Model::Serializable
end

it "returns errors for value not in allowed set" do
instance = ValidationTestClass.new(email: "invalid@example.com", role: ["admin"])
expect { instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
instance = ValidationTestClass.new(email: "invalid@example.com",
role: ["admin"])
expect do
instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("email is `invalid@example.com`, must be one of the following [test@example.com, user@example.com]")
end
end

it "returns errors for invalid collection count" do
instance = ValidationTestClass.new(role: ["admin", "user", "manager", "guest"])
expect { instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
instance = ValidationTestClass.new(role: ["admin", "user", "manager",
"guest"])
expect do
instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("role count is 4, must be between 1 and 3")
end
end

xit "returns multiple errors for multiple invalid attributes" do
instance = ValidationTestClass.new(name: "123", age: "thirty", email: "invalid@example.com", role: [])
expect { instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
instance = ValidationTestClass.new(name: "123", age: "thirty",
email: "invalid@example.com", role: [])
expect do
instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("Invalid value for attribute age: thirty")
expect(error.error_messages.join("\n")).to include("email is `invalid@example.com`, must be one of the following [test@example.com, user@example.com]")
expect(error.error_messages.join("\n")).to include("role count is 0, must be between 1 and 3")
Expand All @@ -63,7 +72,9 @@ class ValidationTestClass < Lutaml::Model::Serializable

xit "raises a ValidationError with all error messages for an invalid instance" do
instance = ValidationTestClass.new(name: "test", age: "thirty")
expect { instance.validate! }.to raise_error(Lutaml::Model::ValidationError) do |error|
expect do
instance.validate!
end.to raise_error(Lutaml::Model::ValidationError) do |error|
expect(error.error_messages.join("\n")).to include("role count is 0, must be between 1 and 3")
expect(error.error_messages.join("\n")).to include("Invalid value for attribute age: thirty")
end
Expand Down

0 comments on commit 509a4d9

Please sign in to comment.