From a77393f9dfd05484e93bb8574b8d30be90c5f719 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Sat, 4 Jan 2025 13:43:19 +0100 Subject: [PATCH] Address offenses --- Gemfile | 2 +- bin/console | 16 +++++----- lib/dry/validation.rb | 4 +-- lib/dry/validation/constants.rb | 12 +++---- lib/dry/validation/contract.rb | 10 +++--- .../validation/contract/class_interface.rb | 16 +++++----- lib/dry/validation/evaluator.rb | 9 +++--- lib/dry/validation/extensions/hints.rb | 2 +- lib/dry/validation/extensions/monads.rb | 2 +- lib/dry/validation/failures.rb | 2 +- lib/dry/validation/macros.rb | 8 ++--- lib/dry/validation/message.rb | 2 +- lib/dry/validation/messages/resolver.rb | 10 +++--- lib/dry/validation/result.rb | 2 +- lib/dry/validation/rule.rb | 4 +-- lib/dry/validation/schema_ext.rb | 2 +- lib/dry/validation/values.rb | 31 ++++++++----------- spec/spec_helper.rb | 2 +- spec/unit/values_spec.rb | 2 +- 19 files changed, 66 insertions(+), 72 deletions(-) diff --git a/Gemfile b/Gemfile index 56f6e7ec..99936f4c 100644 --- a/Gemfile +++ b/Gemfile @@ -6,8 +6,8 @@ eval_gemfile "Gemfile.devtools" gemspec -gem "dry-core", github: "dry-rb/dry-core", branch: "main" gem "dry-configurable", github: "dry-rb/dry-configurable", branch: "main" +gem "dry-core", github: "dry-rb/dry-core", branch: "main" gem "dry-schema", github: "dry-rb/dry-schema", branch: "main" gem "dry-types", github: "dry-rb/dry-types", branch: "main" diff --git a/bin/console b/bin/console index 06d8dfa8..b8c3be87 100755 --- a/bin/console +++ b/bin/console @@ -13,20 +13,20 @@ module Types end class Context - def schema(&block) - Dry::Schema.define(&block) + def schema(&) + Dry::Schema.define(&) end - def params(&block) - Dry::Schema.Params(&block) + def params(&) + Dry::Schema.Params(&) end - def json(&block) - Dry::Schema.JSON(&block) + def json(&) + Dry::Schema.JSON(&) end - def contract(&block) - Dry::Validation::Contract.build(&block) + def contract(&) + Dry::Validation::Contract.build(&) end end diff --git a/lib/dry/validation.rb b/lib/dry/validation.rb index 81fdadd4..0a22765d 100644 --- a/lib/dry/validation.rb +++ b/lib/dry/validation.rb @@ -64,8 +64,8 @@ def self.loader # # @api public # - def self.Contract(options = EMPTY_HASH, &block) - Contract.build(options, &block) + def self.Contract(options = EMPTY_HASH, &) + Contract.build(options, &) end # This is needed by Macros::Registrar diff --git a/lib/dry/validation/constants.rb b/lib/dry/validation/constants.rb index 5344b096..ab7f4be9 100644 --- a/lib/dry/validation/constants.rb +++ b/lib/dry/validation/constants.rb @@ -5,7 +5,7 @@ module Dry module Validation - include Dry::Core::Constants + include ::Dry::Core::Constants DOT = "." @@ -21,19 +21,19 @@ module Validation # Mapping for block kwarg options used by block_options # # @see Rule#block_options - BLOCK_OPTIONS_MAPPINGS = Hash.new { |_, key| key }.update(context: :_context).freeze + BLOCK_OPTIONS_MAPPINGS = ::Hash.new { |_, key| key }.update(context: :_context).freeze # Error raised when `rule` specifies one or more keys that the schema doesn't specify - InvalidKeysError = Class.new(StandardError) + InvalidKeysError = ::Class.new(::StandardError) # Error raised when a localized message was not found - MissingMessageError = Class.new(StandardError) + MissingMessageError = ::Class.new(::StandardError) # Error raised when trying to define a schema in a contract class that already has a schema - DuplicateSchemaError = Class.new(StandardError) + DuplicateSchemaError = ::Class.new(::StandardError) # Error raised during initialization of a contract that has no schema defined - SchemaMissingError = Class.new(StandardError) do + SchemaMissingError = ::Class.new(::StandardError) do # @api private def initialize(klass) super("#{klass} cannot be instantiated without a schema defined") diff --git a/lib/dry/validation/contract.rb b/lib/dry/validation/contract.rb index 15cf5ee5..fe5fad86 100644 --- a/lib/dry/validation/contract.rb +++ b/lib/dry/validation/contract.rb @@ -41,9 +41,9 @@ module Validation # # @api public class Contract - include Dry::Equalizer(:schema, :rules, :messages, inspect: false) + include ::Dry::Equalizer(:schema, :rules, :messages, inspect: false) - extend Dry::Initializer + extend ::Dry::Initializer extend ClassInterface config.messages.top_namespace = DEFAULT_ERRORS_NAMESPACE @@ -92,7 +92,7 @@ class Contract def call(input, context = EMPTY_HASH) validate_input_type(input) - context_map = Concurrent::Map.new.tap do |map| + context_map = ::Concurrent::Map.new.tap do |map| default_context.each { |key, value| map[key] = value } context.each { |key, value| map[key] = value } end @@ -165,9 +165,9 @@ def messages end def validate_input_type(input) - return if input.is_a?(Hash) + return if input.is_a?(::Hash) - raise ArgumentError, "Input must be a Hash. #{input.class} was given." + raise ::ArgumentError, "Input must be a Hash. #{input.class} was given." end end end diff --git a/lib/dry/validation/contract/class_interface.rb b/lib/dry/validation/contract/class_interface.rb index d6220130..d3d98765 100644 --- a/lib/dry/validation/contract/class_interface.rb +++ b/lib/dry/validation/contract/class_interface.rb @@ -50,8 +50,8 @@ def macros # @see https://dry-rb.org/gems/dry-schema/params/ # # @api public - def params(*external_schemas, &block) - define(:Params, external_schemas, &block) + def params(*external_schemas, &) + define(:Params, external_schemas, &) end # Define a JSON schema for your contract @@ -62,8 +62,8 @@ def params(*external_schemas, &block) # @see https://dry-rb.org/gems/dry-schema/json/ # # @api public - def json(*external_schemas, &block) - define(:JSON, external_schemas, &block) + def json(*external_schemas, &) + define(:JSON, external_schemas, &) end # Define a plain schema for your contract @@ -74,8 +74,8 @@ def json(*external_schemas, &block) # @see https://dry-rb.org/gems/dry-schema/ # # @api public - def schema(*external_schemas, &block) - define(:schema, external_schemas, &block) + def schema(*external_schemas, &) + define(:schema, external_schemas, &) end # Define a rule for your contract @@ -115,8 +115,8 @@ def rule(*keys, &block) # @return [Contract] # # @api public - def build(options = EMPTY_HASH, &block) - Class.new(self, &block).new(**options) + def build(options = EMPTY_HASH, &) + Class.new(self, &).new(**options) end # @api private diff --git a/lib/dry/validation/evaluator.rb b/lib/dry/validation/evaluator.rb index 443531da..d6fc2386 100644 --- a/lib/dry/validation/evaluator.rb +++ b/lib/dry/validation/evaluator.rb @@ -117,8 +117,8 @@ def failures end # @api private - def with(new_opts, &block) - self.class.new(_contract, **_options, **new_opts, &block) + def with(new_opts, &) + self.class.new(_contract, **_options, **new_opts, &) end # Return default (first) key name @@ -216,15 +216,14 @@ def respond_to_missing?(meth, include_private = false) # Forward to the underlying contract # # @api private - def method_missing(meth, *args, &block) + def method_missing(meth, ...) # yes, we do want to delegate to private methods too if _contract.respond_to?(meth, true) - _contract.__send__(meth, *args, &block) + _contract.__send__(meth, ...) else super end end - ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true) end end end diff --git a/lib/dry/validation/extensions/hints.rb b/lib/dry/validation/extensions/hints.rb index 883f43fa..1388202b 100644 --- a/lib/dry/validation/extensions/hints.rb +++ b/lib/dry/validation/extensions/hints.rb @@ -59,7 +59,7 @@ def hints(new_options = EMPTY_HASH) end end - Dry::Schema.load_extensions(:hints) + ::Dry::Schema.load_extensions(:hints) Result.prepend(ResultExtensions) end diff --git a/lib/dry/validation/extensions/monads.rb b/lib/dry/validation/extensions/monads.rb index 80e0b7cf..16e0c894 100644 --- a/lib/dry/validation/extensions/monads.rb +++ b/lib/dry/validation/extensions/monads.rb @@ -24,7 +24,7 @@ module Validation # # @api public class Result - include Dry::Monads::Result::Mixin + include ::Dry::Monads::Result::Mixin # Returns a result monad # diff --git a/lib/dry/validation/failures.rb b/lib/dry/validation/failures.rb index 0406ad83..472b5246 100644 --- a/lib/dry/validation/failures.rb +++ b/lib/dry/validation/failures.rb @@ -27,7 +27,7 @@ class Failures # @api private def initialize(path = ROOT_PATH) - @path = Dry::Schema::Path[path] + @path = ::Dry::Schema::Path[path] @opts = EMPTY_ARRAY.dup end diff --git a/lib/dry/validation/macros.rb b/lib/dry/validation/macros.rb index dccb2973..0729e250 100644 --- a/lib/dry/validation/macros.rb +++ b/lib/dry/validation/macros.rb @@ -29,8 +29,8 @@ module Registrar # @see Macro # # @api public - def register_macro(name, *args, &block) - macros.register(name, *args, &block) + def register_macro(name, ...) + macros.register(name, ...) self end end @@ -80,8 +80,8 @@ def self.[](name) # @return [Macros] # # @api public - def self.register(name, *args, &block) - container.register(name, *args, &block) + def self.register(name, ...) + container.register(name, ...) self end diff --git a/lib/dry/validation/message.rb b/lib/dry/validation/message.rb index 0d7704df..198c35e3 100644 --- a/lib/dry/validation/message.rb +++ b/lib/dry/validation/message.rb @@ -6,7 +6,7 @@ module Validation # # @api public class Message < Schema::Message - include Dry::Equalizer(:text, :path, :meta) + include ::Dry::Equalizer(:text, :path, :meta) # The error message text # diff --git a/lib/dry/validation/messages/resolver.rb b/lib/dry/validation/messages/resolver.rb index 11739e5f..9c8bdeaa 100644 --- a/lib/dry/validation/messages/resolver.rb +++ b/lib/dry/validation/messages/resolver.rb @@ -3,7 +3,7 @@ module Dry module Validation module Messages - FULL_MESSAGE_WHITESPACE = Dry::Schema::MessageCompiler::FULL_MESSAGE_WHITESPACE + FULL_MESSAGE_WHITESPACE = ::Dry::Schema::MessageCompiler::FULL_MESSAGE_WHITESPACE # Resolve translated messages from failure arguments # @@ -30,11 +30,11 @@ def initialize(messages) # @api public def call(message:, tokens:, path:, meta: EMPTY_HASH) case message - when Symbol + when ::Symbol Message[->(**opts) { message(message, path: path, tokens: tokens, **opts) }, path, meta] - when String + when ::String Message[->(**opts) { [message_text(message, path: path, **opts), meta] }, path, meta] - when Hash + when ::Hash meta = message.dup text = meta.delete(:text) { |key| raise ArgumentError, <<~STR @@ -114,7 +114,7 @@ def parse_tokens(tokens) def parse_token(token) case token - when Array + when ::Array token.join(", ") else token diff --git a/lib/dry/validation/result.rb b/lib/dry/validation/result.rb index 2b65c09c..8fd4d92c 100644 --- a/lib/dry/validation/result.rb +++ b/lib/dry/validation/result.rb @@ -10,7 +10,7 @@ module Validation # # @api public class Result - include Dry::Equalizer(:schema_result, :context, :errors, inspect: false) + include ::Dry::Equalizer(:schema_result, :context, :errors, inspect: false) # Build a new result # diff --git a/lib/dry/validation/rule.rb b/lib/dry/validation/rule.rb index 4e1b8fb3..a1c42e1d 100644 --- a/lib/dry/validation/rule.rb +++ b/lib/dry/validation/rule.rb @@ -13,7 +13,7 @@ module Validation # # @api public class Rule < Function - include Dry::Equalizer(:keys, :block, inspect: false) + include ::Dry::Equalizer(:keys, :block, inspect: false) # @!attribute [r] keys # @return [Array] @@ -127,7 +127,7 @@ def parse_macros(*args) def add_macro_from_hash(macros, spec) spec.each do |k, v| - macros << [k, v.is_a?(Array) ? v : [v]] + macros << [k, v.is_a?(::Array) ? v : [v]] end end end diff --git a/lib/dry/validation/schema_ext.rb b/lib/dry/validation/schema_ext.rb index f9ffa8df..d8a1654d 100644 --- a/lib/dry/validation/schema_ext.rb +++ b/lib/dry/validation/schema_ext.rb @@ -7,7 +7,7 @@ module Schema class Path # @api private def multi_value? - last.is_a?(Array) + last.is_a?(::Array) end # @api private diff --git a/lib/dry/validation/values.rb b/lib/dry/validation/values.rb index 11a68247..161a21ba 100644 --- a/lib/dry/validation/values.rb +++ b/lib/dry/validation/values.rb @@ -12,8 +12,8 @@ module Validation # # @api public class Values - include Enumerable - include Dry::Equalizer(:data) + include ::Enumerable + include ::Dry::Equalizer(:data) # Schema's result output # @@ -46,8 +46,8 @@ def [](*args) return data.dig(*args) if args.size > 1 case (key = args[0]) - when Symbol, String, Array, Hash - keys = Schema::Path[key].to_a + when ::Symbol, ::String, ::Array, ::Hash + keys = ::Dry::Schema::Path[key].to_a return data.dig(*keys) unless keys.last.is_a?(Array) @@ -55,28 +55,24 @@ def [](*args) vals = self.class.new(data.dig(*keys)) vals.fetch_values(*last) { nil } else - raise ArgumentError, "+key+ must be a valid path specification" + raise ::ArgumentError, "+key+ must be a valid path specification" end end # @api public # rubocop: disable Metrics/PerceivedComplexity def key?(key, hash = data) - return hash.key?(key) if key.is_a?(Symbol) + return hash.key?(key) if key.is_a?(::Symbol) Schema::Path[key].reduce(hash) do |a, e| - if e.is_a?(Array) - result = e.all? { |k| key?(k, a) } - return result - elsif e.is_a?(Symbol) && a.is_a?(Array) - return false - elsif a.nil? - return false - elsif a.is_a?(String) + if e.is_a?(::Array) + return e.all? { |k| key?(k, a) } + elsif (e.is_a?(::Symbol) && a.is_a?(::Array)) || a.nil? || a.is_a?(::String) return false else - return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e) + return false unless a.is_a?(::Array) ? (e >= 0 && e < a.size) : a.key?(e) end + a[e] end true @@ -91,14 +87,13 @@ def respond_to_missing?(meth, include_private = false) private # @api private - def method_missing(meth, *args, &block) + def method_missing(meth, ...) if data.respond_to?(meth) - data.public_send(meth, *args, &block) + data.public_send(meth, ...) else super end end - ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 03d3a8d4..7f113cdb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,7 +16,7 @@ SPEC_ROOT = Pathname(__dir__) -Dir[SPEC_ROOT.join("support/**/*.rb")].sort.each(&method(:require)) +Dir[SPEC_ROOT.join("support/**/*.rb")].each(&method(:require)) RSpec.configure do |config| config.before do diff --git a/spec/unit/values_spec.rb b/spec/unit/values_spec.rb index 65b2f264..511b4cd6 100644 --- a/spec/unit/values_spec.rb +++ b/spec/unit/values_spec.rb @@ -114,7 +114,7 @@ it "forwards to data" do result = [] - values.each do |k, v| + values.each do |k, v| # rubocop:disable Style/MapIntoArray result << [k, v] end