Skip to content

Commit

Permalink
Address offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 4, 2025
1 parent 8a569ec commit a77393f
Show file tree
Hide file tree
Showing 19 changed files with 66 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
16 changes: 8 additions & 8 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/dry/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lib/dry/validation/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Dry
module Validation
include Dry::Core::Constants
include ::Dry::Core::Constants

DOT = "."

Expand All @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions lib/dry/validation/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/dry/validation/contract/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions lib/dry/validation/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/dry/validation/extensions/hints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/validation/extensions/monads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Validation
#
# @api public
class Result
include Dry::Monads::Result::Mixin
include ::Dry::Monads::Result::Mixin

# Returns a result monad
#
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/validation/failures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/dry/validation/macros.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/dry/validation/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
10 changes: 5 additions & 5 deletions lib/dry/validation/messages/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
Expand Down Expand Up @@ -114,7 +114,7 @@ def parse_tokens(tokens)

def parse_token(token)
case token
when Array
when ::Array
token.join(", ")
else
token
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/validation/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
4 changes: 2 additions & 2 deletions lib/dry/validation/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol, String, Hash>]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/validation/schema_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Schema
class Path
# @api private
def multi_value?
last.is_a?(Array)
last.is_a?(::Array)
end

# @api private
Expand Down
31 changes: 13 additions & 18 deletions lib/dry/validation/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -46,37 +46,33 @@ 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)

last = keys.pop
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
Expand All @@ -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
Loading

0 comments on commit a77393f

Please sign in to comment.