From b39c7fe919ad13d90fb904516690637d6de44b27 Mon Sep 17 00:00:00 2001 From: Daniel Dye Date: Sun, 27 Oct 2024 19:26:18 +0000 Subject: [PATCH] Fix Rubocop offences --- .rubocop.yml | 2 +- lib/transmutation/class_attributes.rb | 4 ++-- lib/transmutation/object_serializer.rb | 4 ++++ lib/transmutation/serialization.rb | 8 ++++---- lib/transmutation/serialization/lookup.rb | 6 +++--- lib/transmutation/serialization/rendering.rb | 4 ++-- lib/transmutation/serializer.rb | 8 ++++---- spec/support/markdown_helper.rb | 4 ++-- spec/system/dummy/models/post.rb | 2 +- spec/system/dummy/models/product.rb | 10 +++++----- spec/system/dummy/models/user.rb | 6 +++--- transmutation.gemspec | 2 +- 12 files changed, 32 insertions(+), 28 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c656467..29c385e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ inherit_from: .rubocop_todo.yml AllCops: - TargetRubyVersion: 3.0 + TargetRubyVersion: 3.2.5 NewCops: disable SuggestExtensions: false diff --git a/lib/transmutation/class_attributes.rb b/lib/transmutation/class_attributes.rb index c57d57b..22190dd 100644 --- a/lib/transmutation/class_attributes.rb +++ b/lib/transmutation/class_attributes.rb @@ -10,8 +10,8 @@ def class_attribute( instance_writer: instance_accessor, default: nil ) - class_attribute_reader(*names, instance_reader: instance_reader, default: default) - class_attribute_writer(*names, instance_writer: instance_writer, default: default) + class_attribute_reader(*names, instance_reader:, default:) + class_attribute_writer(*names, instance_writer:, default:) end def class_attribute_reader(*names, instance_reader: true, default: nil) diff --git a/lib/transmutation/object_serializer.rb b/lib/transmutation/object_serializer.rb index c64f691..6bb16fd 100644 --- a/lib/transmutation/object_serializer.rb +++ b/lib/transmutation/object_serializer.rb @@ -1,4 +1,8 @@ +# frozen_string_literal: true + module Transmutation + # Default object serializer. + # This is used when no serializer is found for the given object. class ObjectSerializer < Serializer def as_json(options = {}) object.as_json(options) diff --git a/lib/transmutation/serialization.rb b/lib/transmutation/serialization.rb index 420a4c8..a5589c8 100644 --- a/lib/transmutation/serialization.rb +++ b/lib/transmutation/serialization.rb @@ -13,12 +13,12 @@ module Serialization def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1) if object.respond_to?(:map) && !object.respond_to?(:to_hash) return object.map do |item| - serialize(item, namespace: namespace, serializer: serializer, depth: depth, max_depth: max_depth) + serialize(item, namespace:, serializer:, depth:, max_depth:) end end - lookup_serializer(object, namespace: namespace, serializer: serializer) - .new(object, depth: depth, max_depth: max_depth) + lookup_serializer(object, namespace:, serializer:) + .new(object, depth:, max_depth:) end # Lookup the serializer for the given object. @@ -32,7 +32,7 @@ def serialize(object, namespace: nil, serializer: nil, depth: 0, max_depth: 1) # @return [Class] The serializer for the given object. # def lookup_serializer(object, namespace: nil, serializer: nil) - Lookup.new(self, namespace: namespace).serializer_for(object, serializer: serializer) + Lookup.new(self, namespace:).serializer_for(object, serializer:) end private_class_method def self.included(base) diff --git a/lib/transmutation/serialization/lookup.rb b/lib/transmutation/serialization/lookup.rb index cdaadb8..f7dcfc8 100644 --- a/lib/transmutation/serialization/lookup.rb +++ b/lib/transmutation/serialization/lookup.rb @@ -26,9 +26,9 @@ def initialize(caller, namespace: nil) # - Api::Chat::UserSerializer # - Chat::UserSerializer def serializer_for(object, serializer: nil) - serializer_name = serializer_name_for(object, serializer: serializer) + serializer_name = serializer_name_for(object, serializer:) - return constantize_serializer!(Object, serializer_name, object: object) if serializer_name.start_with?("::") + return constantize_serializer!(Object, serializer_name, object:) if serializer_name.start_with?("::") potential_namespaces.each do |potential_namespace| return potential_namespace.const_get(serializer_name) if potential_namespace.const_defined?(serializer_name) @@ -76,7 +76,7 @@ def caller_namespace end def constantize_serializer!(namespace, name, object:) - raise SerializerNotFound.new(object, namespace: namespace, name: name) unless namespace.const_defined?(name) + raise SerializerNotFound.new(object, namespace:, name:) unless namespace.const_defined?(name) namespace.const_get(name) end diff --git a/lib/transmutation/serialization/rendering.rb b/lib/transmutation/serialization/rendering.rb index abade09..d6ca2f3 100644 --- a/lib/transmutation/serialization/rendering.rb +++ b/lib/transmutation/serialization/rendering.rb @@ -5,9 +5,9 @@ module Serialization module Rendering def render(json: nil, serialize: true, namespace: nil, serializer: nil, max_depth: 1, **args) return super(**args) unless json - return super(**args, json: json) unless serialize + return super(**args, json:) unless serialize - super(**args, json: serialize(json, namespace: namespace, serializer: serializer, max_depth: max_depth)) + super(**args, json: serialize(json, namespace:, serializer:, max_depth:)) end end end diff --git a/lib/transmutation/serializer.rb b/lib/transmutation/serializer.rb index 63fdcf5..453f503 100644 --- a/lib/transmutation/serializer.rb +++ b/lib/transmutation/serializer.rb @@ -56,7 +56,7 @@ class << self # end # end def attribute(attribute_name, &block) - attributes_config[attribute_name] = { block: block } + attributes_config[attribute_name] = { block: } end # Define an association to be serialized @@ -76,14 +76,14 @@ def association(association_name, namespace: nil, serializer: nil) block = lambda do serialize( object.send(association_name), - namespace: namespace, - serializer: serializer, + namespace:, + serializer:, depth: @depth + 1, max_depth: @max_depth ) end - attributes_config[association_name] = { block: block, association: true } + attributes_config[association_name] = { block:, association: true } end alias belongs_to association diff --git a/spec/support/markdown_helper.rb b/spec/support/markdown_helper.rb index ee2f6d5..3a80d70 100644 --- a/spec/support/markdown_helper.rb +++ b/spec/support/markdown_helper.rb @@ -6,7 +6,7 @@ def parse_markdown_table(markdown, nil_value: "nil") header_names = header_row.delete_prefix("|").split("|")&.map(&:strip) rows.map do |raw_row| - parse_markdown_row(raw_row, nil_value: nil_value, header_names: header_names) + parse_markdown_row(raw_row, nil_value:, header_names:) end end @@ -20,7 +20,7 @@ def parse_markdown_row(raw_row, nil_value:, header_names:) row = raw_row.delete_prefix("|").split("|")&.map(&:strip) row.each_with_object({}).with_index do |(value, hash), index| - hash[header_names[index].to_sym] = cast_nil_value(value, nil_value: nil_value) + hash[header_names[index].to_sym] = cast_nil_value(value, nil_value:) end end end diff --git a/spec/system/dummy/models/post.rb b/spec/system/dummy/models/post.rb index 08e9719..a23d920 100644 --- a/spec/system/dummy/models/post.rb +++ b/spec/system/dummy/models/post.rb @@ -40,7 +40,7 @@ def to_json(options = {}) def as_json(_options = {}) { - id: id, + id:, title: first_name, body: last_name } diff --git a/spec/system/dummy/models/product.rb b/spec/system/dummy/models/product.rb index 14bbc28..994d312 100644 --- a/spec/system/dummy/models/product.rb +++ b/spec/system/dummy/models/product.rb @@ -39,11 +39,11 @@ def to_json(options = {}) def as_json(_options = {}) { - id: id, - name: name, - description: description, - price_subunit: price_subunit, - price_currency: price_currency + id:, + name:, + description:, + price_subunit:, + price_currency: } end end diff --git a/spec/system/dummy/models/user.rb b/spec/system/dummy/models/user.rb index 6a32c80..753285d 100644 --- a/spec/system/dummy/models/user.rb +++ b/spec/system/dummy/models/user.rb @@ -40,9 +40,9 @@ def to_json(options = {}) def as_json(_options = {}) { - id: id, - first_name: first_name, - last_name: last_name + id:, + first_name:, + last_name: } end end diff --git a/transmutation.gemspec b/transmutation.gemspec index 50b002e..afd7203 100644 --- a/transmutation.gemspec +++ b/transmutation.gemspec @@ -29,8 +29,8 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency "zeitwerk", "~> 2.6.15" spec.add_dependency "activesupport", "~> 7.2" + spec.add_dependency "zeitwerk", "~> 2.6.15" # For more information and examples about making a new gem, check out our # guide at: https://bundler.io/guides/creating_gem.html