From 65e9ae0eeff89da04922c795205e564b42de6d4b Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Mon, 6 Jan 2025 14:52:31 +0100 Subject: [PATCH] Use concise syntax for methods where it makes sense I'm not a fan, it's just the way you're getting used to it :) --- lib/dry/types/any.rb | 12 ++--- lib/dry/types/array/constructor.rb | 12 ++--- lib/dry/types/array/member.rb | 4 +- lib/dry/types/builder.rb | 30 +++-------- lib/dry/types/builder_methods.rb | 20 ++------ lib/dry/types/coercions.rb | 4 +- lib/dry/types/compiler.rb | 4 +- lib/dry/types/composition.rb | 36 ++++---------- lib/dry/types/constrained.rb | 20 ++------ lib/dry/types/constructor.rb | 16 ++---- lib/dry/types/constructor/function.rb | 24 +++------ lib/dry/types/constructor/wrapper.rb | 12 ++--- lib/dry/types/decorator.rb | 20 ++------ lib/dry/types/default.rb | 16 ++---- lib/dry/types/enum.rb | 20 ++------ lib/dry/types/errors.rb | 8 +-- lib/dry/types/extensions/maybe.rb | 12 ++--- lib/dry/types/hash/constructor.rb | 12 ++--- lib/dry/types/implication.rb | 4 +- lib/dry/types/intersection.rb | 8 +-- lib/dry/types/lax.rb | 12 ++--- lib/dry/types/map.rb | 72 ++++++++++++--------------- lib/dry/types/meta.rb | 8 +-- lib/dry/types/nominal.rb | 44 ++++------------ lib/dry/types/predicate_inferrer.rb | 4 +- lib/dry/types/predicate_registry.rb | 4 +- lib/dry/types/primitive_inferrer.rb | 16 ++---- lib/dry/types/printable.rb | 4 +- lib/dry/types/printer.rb | 4 +- lib/dry/types/result.rb | 20 ++------ lib/dry/types/schema.rb | 4 +- lib/dry/types/schema/key.rb | 32 +++--------- lib/dry/types/sum.rb | 8 +-- 33 files changed, 145 insertions(+), 381 deletions(-) diff --git a/lib/dry/types/any.rb b/lib/dry/types/any.rb index 98e58716..08861c9c 100644 --- a/lib/dry/types/any.rb +++ b/lib/dry/types/any.rb @@ -9,9 +9,7 @@ module Types # # @api public class AnyClass < Nominal - def self.name - "Any" - end + def self.name = "Any" # @api private def initialize(**options) @@ -21,9 +19,7 @@ def initialize(**options) # @return [String] # # @api public - def name - "Any" - end + def name = "Any" # @param [Hash] new_options # @@ -37,9 +33,7 @@ def with(**new_options) # @return [Array] # # @api public - def to_ast(meta: true) - [:any, meta ? self.meta : EMPTY_HASH] - end + def to_ast(meta: true) = [:any, meta ? self.meta : EMPTY_HASH] end Any = AnyClass.new diff --git a/lib/dry/types/array/constructor.rb b/lib/dry/types/array/constructor.rb index e3e184b9..fb810dcd 100644 --- a/lib/dry/types/array/constructor.rb +++ b/lib/dry/types/array/constructor.rb @@ -7,23 +7,17 @@ class Array < Nominal # @api private class Constructor < ::Dry::Types::Constructor # @api private - def constructor_type - ::Dry::Types::Array::Constructor - end + def constructor_type = ::Dry::Types::Array::Constructor # @return [Lax] # # @api public - def lax - Lax.new(type.lax.constructor(fn, meta: meta)) - end + def lax = Lax.new(type.lax.constructor(fn, meta: meta)) # @see Dry::Types::Array#of # # @api public - def of(member) - type.of(member).constructor(fn, meta: meta) - end + def of(member) = type.of(member).constructor(fn, meta: meta) end end end diff --git a/lib/dry/types/array/member.rb b/lib/dry/types/array/member.rb index e984a745..b5963d81 100644 --- a/lib/dry/types/array/member.rb +++ b/lib/dry/types/array/member.rb @@ -113,9 +113,7 @@ def to_ast(meta: true) end # @api private - def constructor_type - ::Dry::Types::Array::Constructor - end + def constructor_type = ::Dry::Types::Array::Constructor end end end diff --git a/lib/dry/types/builder.rb b/lib/dry/types/builder.rb index 25b85866..ab60823c 100644 --- a/lib/dry/types/builder.rb +++ b/lib/dry/types/builder.rb @@ -5,23 +5,18 @@ module Types # Common API for building types and composition # # @api public - # rubocop:disable Metrics/ModuleLength module Builder include ::Dry::Core::Constants # @return [Class] # # @api private - def constrained_type - Constrained - end + def constrained_type = Constrained # @return [Class] # # @api private - def constructor_type - Constructor - end + def constructor_type = Constructor # Compose two types into a Sum type # @@ -30,9 +25,7 @@ def constructor_type # @return [Sum, Sum::Constrained] # # @api private - def |(other) - compose(other, Sum) - end + def |(other) = compose(other, Sum) # Compose two types into an Intersection type # @@ -41,9 +34,7 @@ def |(other) # @return [Intersection, Intersection::Constrained] # # @api private - def &(other) - compose(other, Intersection) - end + def &(other) = compose(other, Intersection) # Compose two types into an Implication type # @@ -52,18 +43,14 @@ def &(other) # @return [Implication, Implication::Constrained] # # @api private - def >(other) - compose(other, Implication) - end + def >(other) = compose(other, Implication) # Turn a type into an optional type # # @return [Sum] # # @api public - def optional - Types["nil"] | self - end + def optional = Types["nil"] | self # Turn a type into a constrained type # @@ -137,9 +124,7 @@ def enum(*values) # @return [Lax] # # @api public - def lax - Lax.new(self) - end + def lax = Lax.new(self) # Define a constructor for the type # @@ -216,6 +201,5 @@ def compose(other, composition_class) klass.new(self, other) end end - # rubocop:enable Metrics/ModuleLength end end diff --git a/lib/dry/types/builder_methods.rb b/lib/dry/types/builder_methods.rb index 69371e53..6b31ae4f 100644 --- a/lib/dry/types/builder_methods.rb +++ b/lib/dry/types/builder_methods.rb @@ -23,18 +23,14 @@ def included(base) # @param [Dry::Types::Type] type # # @return [Dry::Types::Array] - def Array(type) - Strict(::Array).of(type) - end + def Array(type) = Strict(::Array).of(type) # Build a hash schema # # @param [Hash{Symbol => Dry::Types::Type}] type_map # # @return [Dry::Types::Array] - def Hash(type_map) - Strict(::Hash).schema(type_map) - end + def Hash(type_map) = Strict(::Hash).schema(type_map) # Build a type which values are instances of a given class # Values are checked using `is_a?` call @@ -47,9 +43,7 @@ def Hash(type_map) # @param [Class,Module] klass Class or module # # @return [Dry::Types::Type] - def Instance(klass) - Nominal(klass).constrained(type: klass) - end + def Instance(klass) = Nominal(klass).constrained(type: klass) alias_method :Strict, :Instance # Build a type with a single value @@ -58,9 +52,7 @@ def Instance(klass) # @param [Object] value # # @return [Dry::Types::Type] - def Value(value) - Nominal(value.class).constrained(eql: value) - end + def Value(value) = Nominal(value.class).constrained(eql: value) # Build a type with a single value # The equality check done with `equal?` @@ -68,9 +60,7 @@ def Value(value) # @param [Object] object # # @return [Dry::Types::Type] - def Constant(object) - Nominal(object.class).constrained(is: object) - end + def Constant(object) = Nominal(object.class).constrained(is: object) # Build a constructor type # If no constructor block given it uses .new method diff --git a/lib/dry/types/coercions.rb b/lib/dry/types/coercions.rb index 5eacc029..e7ba9cad 100644 --- a/lib/dry/types/coercions.rb +++ b/lib/dry/types/coercions.rb @@ -99,9 +99,7 @@ def to_symbol(input, &) # @return [Boolean] # # @api private - def empty_str?(value) - EMPTY_STRING.eql?(value) - end + def empty_str?(value) = EMPTY_STRING.eql?(value) end end end diff --git a/lib/dry/types/compiler.rb b/lib/dry/types/compiler.rb index a55b6f24..21051111 100644 --- a/lib/dry/types/compiler.rb +++ b/lib/dry/types/compiler.rb @@ -12,9 +12,7 @@ def initialize(registry) @registry = registry end - def call(ast) - visit(ast) - end + def call(ast) = visit(ast) def visit(node) type, body = node diff --git a/lib/dry/types/composition.rb b/lib/dry/types/composition.rb index a2941ebb..7057b336 100644 --- a/lib/dry/types/composition.rb +++ b/lib/dry/types/composition.rb @@ -56,55 +56,41 @@ def initialize(left, right, **options) # @return [String] # # @api public - def name - [left, right].map(&:name).join(" #{self.class.operator} ") - end + def name = "#{left.name} #{self.class.operator} #{right.name}" # @return [false] # # @api public - def default? - false - end + def default? = false # @return [false] # # @api public - def constrained? - false - end + def constrained? = false # @return [Boolean] # # @api public - def optional? - false - end + def optional? = false # @param [Object] input # # @return [Object] # # @api private - def call_unsafe(input) - raise ::NotImplementedError - end + def call_unsafe(input) = raise ::NotImplementedError # @param [Object] input # # @return [Object] # # @api private - def call_safe(input, &) - raise ::NotImplementedError - end + def call_safe(input, &) = raise ::NotImplementedError # @param [Object] input # # @api public - def try(input) - raise ::NotImplementedError - end + def try(input) = raise ::NotImplementedError # @api private def success(input) @@ -131,9 +117,7 @@ def failure(input, _error = nil) # @return [Boolean] # # @api private - def primitive?(value) - raise ::NotImplementedError - end + def primitive?(value) = raise ::NotImplementedError # @see Nominal#to_ast # @@ -148,9 +132,7 @@ def to_ast(meta: true) # @return [Proc] # # @api public - def to_proc - proc { |value| self.(value) } - end + def to_proc = proc { |value| self.(value) } end end end diff --git a/lib/dry/types/constrained.rb b/lib/dry/types/constrained.rb index 88431c88..9888e9f0 100644 --- a/lib/dry/types/constrained.rb +++ b/lib/dry/types/constrained.rb @@ -98,26 +98,20 @@ def constrained(*nullary_rules, **unary_rules) # @return [true] # # @api public - def constrained? - true - end + def constrained? = true # @param [Object] value # # @return [Boolean] # # @api public - def ===(value) - valid?(value) - end + def ===(value) = valid?(value) # Build lax type. Constraints are not applicable to lax types hence unwrapping # # @return [Lax] # @api public - def lax - type.lax - end + def lax = type.lax # @see Nominal#to_ast # @api public @@ -126,9 +120,7 @@ def to_ast(meta: true) end # @api private - def constructor_type - type.constructor_type - end + def constructor_type = type.constructor_type private @@ -137,9 +129,7 @@ def constructor_type # @return [Boolean] # # @api private - def decorate?(response) - super || response.is_a?(Constructor) - end + def decorate?(response) = super || response.is_a?(Constructor) # @param [Array] positional_args # diff --git a/lib/dry/types/constructor.rb b/lib/dry/types/constructor.rb index e050dbc6..40e58741 100644 --- a/lib/dry/types/constructor.rb +++ b/lib/dry/types/constructor.rb @@ -77,9 +77,7 @@ def call_safe(input) # @return [Object] # # @api private - def call_unsafe(input) - type.call_unsafe(fn.(input)) - end + def call_unsafe(input) = type.call_unsafe(fn.(input)) # @param [Object] input # @param [#call,nil] block @@ -121,9 +119,7 @@ def constructor(new_fn = nil, **options, &block) # @return [Class] # # @api private - def constrained_type - Constrained::Coercible - end + def constrained_type = Constrained::Coercible # @see Nominal#to_ast # @@ -150,18 +146,14 @@ def prepend(new_fn = nil, **options, &block) # # @return [Lax] # @api public - def lax - Lax.new(constructor_type[type.lax, **options]) - end + def lax = Lax.new(constructor_type[type.lax, **options]) # Wrap the type with a proc # # @return [Proc] # # @api public - def to_proc - proc { self.(_1) } - end + def to_proc = proc { self.(_1) } private diff --git a/lib/dry/types/constructor/function.rb b/lib/dry/types/constructor/function.rb index d4ac641a..d811197d 100644 --- a/lib/dry/types/constructor/function.rb +++ b/lib/dry/types/constructor/function.rb @@ -83,9 +83,7 @@ def call(input, &) # def call(input, # # @api private class PrivateCall < MethodCall - def call(input, &) - @target.send(@name, input, &) - end + def call(input, &) = @target.send(@name, input, &) end # Coercion via an unsafe private method call @@ -115,9 +113,7 @@ def initialize(fn) @name = fn.name end - def to_ast - [:method, target, name] - end + def to_ast = [:method, target, name] end class Wrapper < Function @@ -129,9 +125,7 @@ def call(input, type, &) end alias_method :[], :call - def arity - 2 - end + def arity = 2 end # Choose or build specialized invokation code for a callable @@ -175,19 +169,13 @@ def initialize(fn) end # @return [Object] - def call(input, &) - @fn.(input, &) - end + def call(input, &) = @fn.(input, &) alias_method :[], :call # @return [Integer] - def arity - 1 - end + def arity = 1 - def wrapper? - arity.equal?(2) - end + def wrapper? = arity.equal?(2) # @return [Array] def to_ast diff --git a/lib/dry/types/constructor/wrapper.rb b/lib/dry/types/constructor/wrapper.rb index 650eac1d..bae6a8d1 100644 --- a/lib/dry/types/constructor/wrapper.rb +++ b/lib/dry/types/constructor/wrapper.rb @@ -8,16 +8,12 @@ module Wrapper # @return [Object] # # @api private - def call_safe(input, &) - fn.(input, type, &) - end + def call_safe(input, &) = fn.(input, type, &) # @return [Object] # # @api private - def call_unsafe(input) - fn.(input, type) - end + def call_unsafe(input) = fn.(input, type) # @param [Object] input # @param [#call,nil] block @@ -85,9 +81,7 @@ def lax # Replace underlying type # # @api private - def __new__(type) - self.class.new(type, *@__args__.drop(1), **@options) - end + def __new__(type) = self.class.new(type, *@__args__.drop(1), **@options) end end end diff --git a/lib/dry/types/decorator.rb b/lib/dry/types/decorator.rb index 1c9726ce..e01c12a3 100644 --- a/lib/dry/types/decorator.rb +++ b/lib/dry/types/decorator.rb @@ -24,23 +24,17 @@ def initialize(type, *, **) # @return [Object] if block given and try fails # # @api public - def try(input, &) - type.try(input, &) - end + def try(input, &) = type.try(input, &) # @return [Boolean] # # @api public - def default? - type.default? - end + def default? = type.default? # @return [Boolean] # # @api public - def constrained? - type.constrained? - end + def constrained? = type.constrained? # @param [Symbol] meth # @param [Boolean] include_private @@ -57,9 +51,7 @@ def respond_to_missing?(meth, include_private = false) # @return [Proc] # # @api public - def to_proc - proc { |value| self.(value) } - end + def to_proc = proc { |value| self.(value) } private @@ -68,9 +60,7 @@ def to_proc # @return [Boolean] # # @api private - def decorate?(response) - response.is_a?(type.class) - end + def decorate?(response) = response.is_a?(type.class) # Delegates missing methods to {#type} # diff --git a/lib/dry/types/default.rb b/lib/dry/types/default.rb index 3baab3c3..cfcd1bec 100644 --- a/lib/dry/types/default.rb +++ b/lib/dry/types/default.rb @@ -62,16 +62,12 @@ def initialize(type, value, **) # @return [Default] # # @api public - def constrained(...) - type.constrained(...).default(value) - end + def constrained(...) = type.constrained(...).default(value) # @return [true] # # @api public - def default? - true - end + def default? = true # @param [Object] input # @@ -89,9 +85,7 @@ def try(input = Undefined) # @return [Boolean] # # @api public - def valid?(value = Undefined) - Undefined.equal?(value) || super - end + def valid?(value = Undefined) = Undefined.equal?(value) || super # @param [Object] input # @@ -122,9 +116,7 @@ def call_safe(input = Undefined, &block) # @return [false] # # @api private - def callable? - false - end + def callable? = false end end end diff --git a/lib/dry/types/enum.rb b/lib/dry/types/enum.rb index 97a2d648..a1489165 100644 --- a/lib/dry/types/enum.rb +++ b/lib/dry/types/enum.rb @@ -36,23 +36,17 @@ def initialize(type, **options) # @return [Object] # # @api private - def call_unsafe(input) - type.call_unsafe(map_value(input)) - end + def call_unsafe(input) = type.call_unsafe(map_value(input)) # @return [Object] # # @api private - def call_safe(input, &) - type.call_safe(map_value(input), &) - end + def call_safe(input, &) = type.call_safe(map_value(input), &) # @see Dry::Types::Constrained#try # # @api public - def try(input) - super(map_value(input)) - end + def try(input) = super(map_value(input)) # @api private def default(*) @@ -73,9 +67,7 @@ def to_ast(meta: true) # @return [String] # # @api public - def to_s - PRINTER.(self) - end + def to_s = PRINTER.(self) # Iterate over each enum value # @@ -91,9 +83,7 @@ def each_value(&) # @return [String] # # @api public - def name - "#{super}(#{joined_values})" - end + def name = "#{super}(#{joined_values})" # @return [String] # diff --git a/lib/dry/types/errors.rb b/lib/dry/types/errors.rb index cbd2394e..3a69306c 100644 --- a/lib/dry/types/errors.rb +++ b/lib/dry/types/errors.rb @@ -56,14 +56,10 @@ def initialize(errors) end # @return string - def message - errors.map(&:message).join(", ") - end + def message = errors.map(&:message).join(", ") # @return [Array] - def meta - errors.map(&:meta) - end + def meta = errors.map(&:meta) end class SchemaError < CoercionError diff --git a/lib/dry/types/extensions/maybe.rb b/lib/dry/types/extensions/maybe.rb index 3c88b5dd..41e0841b 100644 --- a/lib/dry/types/extensions/maybe.rb +++ b/lib/dry/types/extensions/maybe.rb @@ -70,9 +70,7 @@ def try(input = Undefined) # @return [true] # # @api public - def default? - true - end + def default? = true # @param [Object] value # @@ -96,17 +94,13 @@ module Builder # @return [Maybe] # # @api public - def maybe - Maybe.new(Types["nil"] | self) - end + def maybe = Maybe.new(Types["nil"] | self) end # @api private class Schema::Key # rubocop:disable Style/ClassAndModuleChildren # @api private - def maybe - __new__(type.maybe) - end + def maybe = __new__(type.maybe) end # @api private diff --git a/lib/dry/types/hash/constructor.rb b/lib/dry/types/hash/constructor.rb index cc43e3bb..159cc26e 100644 --- a/lib/dry/types/hash/constructor.rb +++ b/lib/dry/types/hash/constructor.rb @@ -8,23 +8,17 @@ module Types class Hash < Nominal class Constructor < ::Dry::Types::Constructor # @api private - def constructor_type - ::Dry::Types::Hash::Constructor - end + def constructor_type = ::Dry::Types::Hash::Constructor # @return [Lax] # # @api public - def lax - type.lax.constructor(fn, meta: meta) - end + def lax = Lax.new(type.lax.constructor(fn, meta: meta)) # @see Dry::Types::Array#of # # @api public - def schema(...) - type.schema(...).constructor(fn, meta: meta) - end + def schema(...) = type.schema(...).constructor(fn, meta: meta) end end end diff --git a/lib/dry/types/implication.rb b/lib/dry/types/implication.rb index 8bd3d6a0..33b593f1 100644 --- a/lib/dry/types/implication.rb +++ b/lib/dry/types/implication.rb @@ -8,9 +8,7 @@ module Types class Implication include Composition - def self.operator - :> - end + def self.operator = :> # @param [Object] input # diff --git a/lib/dry/types/intersection.rb b/lib/dry/types/intersection.rb index bd3f3283..64c43f07 100644 --- a/lib/dry/types/intersection.rb +++ b/lib/dry/types/intersection.rb @@ -12,9 +12,7 @@ module Types class Intersection include Composition - def self.operator - :& - end + def self.operator = :& # @param [Object] input # @@ -30,9 +28,7 @@ def call_unsafe(input) # @return [Object] # # @api private - def call_safe(input, &) - try_sides(input, &).input - end + def call_safe(input, &) = try_sides(input, &).input # @param [Object] input # diff --git a/lib/dry/types/lax.rb b/lib/dry/types/lax.rb index b2ce26a1..bec2d67d 100644 --- a/lib/dry/types/lax.rb +++ b/lib/dry/types/lax.rb @@ -35,23 +35,17 @@ def call(input) # @return [Result,Logic::Result] # # @api public - def try(input, &) - type.try(input, &) - end + def try(input, &) = type.try(input, &) # @see Nominal#to_ast # # @api public - def to_ast(meta: true) - [:lax, type.to_ast(meta: meta)] - end + def to_ast(meta: true) = [:lax, type.to_ast(meta: meta)] # @return [Lax] # # @api public - def lax - self - end + def lax = self private diff --git a/lib/dry/types/map.rb b/lib/dry/types/map.rb index 28021139..74027e1b 100644 --- a/lib/dry/types/map.rb +++ b/lib/dry/types/map.rb @@ -30,23 +30,17 @@ def initialize(primitive, key_type: Types["any"], value_type: Types["any"], meta # @return [Type] # # @api public - def key_type - options[:key_type] - end + def key_type = options[:key_type] # @return [Type] # # @api public - def value_type - options[:value_type] - end + def value_type = options[:value_type] # @return [String] # # @api public - def name - "Map" - end + def name = "Map" # @param [Hash] hash # @@ -64,9 +58,7 @@ def call_unsafe(hash) # @return [Hash] # # @api private - def call_safe(hash) - try(hash) { return yield }.input - end + def call_safe(hash) = try(hash) { return yield }.input # @param [Hash] hash # @@ -95,48 +87,48 @@ def to_ast(meta: true) # @return [Boolean] # # @api public - def constrained? - value_type.constrained? - end + def constrained? = value_type.constrained? private # @api private - # rubocop:disable Metrics/PerceivedComplexity # rubocop:disable Metrics/AbcSize def coerce(input) - unless primitive?(input) - return failure( - input, CoercionError.new("#{input.inspect} must be an instance of #{primitive}") - ) - end - - output = {} - failures = [] - - input.each do |k, v| - res_k = key_type.try(k) - res_v = value_type.try(v) + assert_primitive(input) do + output = {} + failures = [] + + input.each do |k, v| + res_k = key_type.try(k) + res_v = value_type.try(v) + + if res_k.failure? + failures << res_k.error + elsif output.key?(res_k.input) + failures << CoercionError.new("duplicate coerced hash key #{res_k.input.inspect}") + elsif res_v.failure? + failures << res_v.error + else + output[res_k.input] = res_v.input + end + end - if res_k.failure? - failures << res_k.error - elsif output.key?(res_k.input) - failures << CoercionError.new("duplicate coerced hash key #{res_k.input.inspect}") - elsif res_v.failure? - failures << res_v.error + if failures.empty? + success(output) else - output[res_k.input] = res_v.input + failure(input, MultipleError.new(failures)) end end + end + # rubocop:enable Metrics/AbcSize - if failures.empty? - success(output) + def assert_primitive(input) + if primitive?(input) + yield else - failure(input, MultipleError.new(failures)) + failure(input, CoercionError.new("#{input.inspect} must be an instance of #{primitive}")) end end - # rubocop:enable Metrics/PerceivedComplexity - # rubocop:enable Metrics/AbcSize end end end diff --git a/lib/dry/types/meta.rb b/lib/dry/types/meta.rb index c2f7980f..d88726bd 100644 --- a/lib/dry/types/meta.rb +++ b/lib/dry/types/meta.rb @@ -16,9 +16,7 @@ def initialize(*args, meta: EMPTY_HASH, **options) # @return [Type] # # @api public - def with(**options) - super(meta: @meta, **options) - end + def with(**options) = super(meta: @meta, **options) # @overload meta # @return [Hash] metadata associated with type @@ -43,9 +41,7 @@ def meta(data = Undefined) # @return [Dry::Types::Type] # # @api public - def pristine - with(meta: EMPTY_HASH) - end + def pristine = with(meta: EMPTY_HASH) end end end diff --git a/lib/dry/types/nominal.rb b/lib/dry/types/nominal.rb index 36e43f63..2d0e9a1a 100644 --- a/lib/dry/types/nominal.rb +++ b/lib/dry/types/nominal.rb @@ -48,48 +48,36 @@ def initialize(primitive, **options) # @return [String] # # @api public - def name - primitive.name - end + def name = primitive.name # @return [false] # # @api public - def default? - false - end + def default? = false # @return [false] # # @api public - def constrained? - false - end + def constrained? = false # @return [false] # # @api public - def optional? - false - end + def optional? = false # @param [BasicObject] input # # @return [BasicObject] # # @api private - def call_unsafe(input) - input - end + def call_unsafe(input) = input # @param [BasicObject] input # # @return [BasicObject] # # @api private - def call_safe(input) - input - end + def call_safe(input) = input # @param [Object] input # @@ -100,18 +88,14 @@ def call_safe(input) # @return [nil] otherwise # # @api public - def try(input) - success(input) - end + def try(input) = success(input) # @param (see Dry::Types::Success#initialize) # # @return [Result::Success] # # @api public - def success(input) - Result::Success.new(input) - end + def success(input) = Result::Success.new(input) # @param (see Failure#initialize) # @@ -131,9 +115,7 @@ def failure(input, error) # @return [Boolean] # # @api public - def primitive?(value) - value.is_a?(primitive) - end + def primitive?(value) = value.is_a?(primitive) # @api private def coerce(input, &) @@ -178,18 +160,14 @@ def to_ast(meta: true) # @return [Nominal] # # @api public - def lax - self - end + def lax = self # Wrap the type with a proc # # @return [Proc] # # @api public - def to_proc - ALWAYS - end + def to_proc = ALWAYS end extend ::Dry::Core::Deprecations[:"dry-types"] diff --git a/lib/dry/types/predicate_inferrer.rb b/lib/dry/types/predicate_inferrer.rb index b3e157aa..6fb6c582 100644 --- a/lib/dry/types/predicate_inferrer.rb +++ b/lib/dry/types/predicate_inferrer.rb @@ -157,9 +157,7 @@ def visit_constrained(node) end # @api private - def visit_any(_) - EMPTY_ARRAY - end + def visit_any(_) = EMPTY_ARRAY # @api private def visit_and(node) diff --git a/lib/dry/types/predicate_registry.rb b/lib/dry/types/predicate_registry.rb index 51e63751..2c1b8fe2 100644 --- a/lib/dry/types/predicate_registry.rb +++ b/lib/dry/types/predicate_registry.rb @@ -26,9 +26,7 @@ def key?(name) end # @api private - def [](name) - predicates[name] - end + def [](name) = predicates[name] end end end diff --git a/lib/dry/types/primitive_inferrer.rb b/lib/dry/types/primitive_inferrer.rb index 3ff9122c..ac3bc17d 100644 --- a/lib/dry/types/primitive_inferrer.rb +++ b/lib/dry/types/primitive_inferrer.rb @@ -25,20 +25,14 @@ def visit_nominal(node) end # @api private - def visit_hash(_) - ::Hash - end + def visit_hash(_) = ::Hash alias_method :visit_schema, :visit_hash # @api private - def visit_array(_) - ::Array - end + def visit_array(_) = ::Array # @api private - def visit_lax(node) - visit(node) - end + def visit_lax(node) = visit(node) # @api private def visit_constructor(node) @@ -66,9 +60,7 @@ def visit_constrained(node) end # @api private - def visit_any(_) - ::Object - end + def visit_any(_) = ::Object end # @return [Compiler] diff --git a/lib/dry/types/printable.rb b/lib/dry/types/printable.rb index 30218824..23f9d41c 100644 --- a/lib/dry/types/printable.rb +++ b/lib/dry/types/printable.rb @@ -7,9 +7,7 @@ module Printable # @return [String] # # @api private - def to_s - PRINTER.(self) { super } - end + def to_s = PRINTER.(self) { super } alias_method :inspect, :to_s end end diff --git a/lib/dry/types/printer.rb b/lib/dry/types/printer.rb index 937cb9d9..a0f293de 100644 --- a/lib/dry/types/printer.rb +++ b/lib/dry/types/printer.rb @@ -59,9 +59,7 @@ def visit(type, &) send(print_with, type, &) end - def visit_any(_) - yield "Any" - end + def visit_any(_) = yield "Any" def visit_array(type) visit_options(EMPTY_HASH, type.meta) do |opts| diff --git a/lib/dry/types/result.rb b/lib/dry/types/result.rb index b155adc2..fc9aa721 100644 --- a/lib/dry/types/result.rb +++ b/lib/dry/types/result.rb @@ -25,16 +25,12 @@ class Success < Result # @return [true] # # @api public - def success? - true - end + def success? = true # @return [false] # # @api public - def failure? - false - end + def failure? = false end # Failure result @@ -59,23 +55,17 @@ def initialize(input, error) # @return [String] # # @api private - def to_s - error.to_s - end + def to_s = error.to_s # @return [false] # # @api public - def success? - false - end + def success? = false # @return [true] # # @api public - def failure? - true - end + def failure? = true end end end diff --git a/lib/dry/types/schema.rb b/lib/dry/types/schema.rb index 494551e5..ad1ecc8e 100644 --- a/lib/dry/types/schema.rb +++ b/lib/dry/types/schema.rb @@ -77,9 +77,7 @@ def call_safe(hash, options = EMPTY_HASH) # @return [Hash{Symbol => Object}] # # @api public - def apply(hash, options = EMPTY_HASH) - call_unsafe(hash, options) - end + def apply(hash, options = EMPTY_HASH) = call_unsafe(hash, options) # @param input [Hash] hash # diff --git a/lib/dry/types/schema/key.rb b/lib/dry/types/schema/key.rb index 55999b40..084c0815 100644 --- a/lib/dry/types/schema/key.rb +++ b/lib/dry/types/schema/key.rb @@ -38,30 +38,22 @@ def initialize(type, name, required: Undefined, **options) end # @api private - def call_safe(input, &) - type.call_safe(input, &) - end + def call_safe(input, &) = type.call_safe(input, &) # @api private - def call_unsafe(input) - type.call_unsafe(input) - end + def call_unsafe(input) = type.call_unsafe(input) # @see Dry::Types::Nominal#try # # @api public - def try(input, &) - type.try(input, &) - end + def try(input, &) = type.try(input, &) # Whether the key is required in schema input # # @return [Boolean] # # @api public - def required? - options.fetch(:required) - end + def required? = options.fetch(:required) # Control whether the key is required # @@ -88,27 +80,21 @@ def required(required = Undefined) # @return [Dry::Types::Schema::Key] # # @api public - def omittable - required(false) - end + def omittable = required(false) # Turn key into a lax type. Lax types are not strict hence such keys are not required # # @return [Lax] # # @api public - def lax - __new__(type.lax).required(false) - end + def lax = __new__(type.lax).required(false) # Make wrapped type optional # # @return [Key] # # @api public - def optional - __new__(type.optional) - end + def optional = __new__(type.optional) # Dump to internal AST representation # @@ -145,9 +131,7 @@ def meta(data = Undefined) private # @api private - def decorate?(response) - response.is_a?(Type) - end + def decorate?(response) = response.is_a?(Type) end end end diff --git a/lib/dry/types/sum.rb b/lib/dry/types/sum.rb index 506ab313..7b612ca9 100644 --- a/lib/dry/types/sum.rb +++ b/lib/dry/types/sum.rb @@ -8,16 +8,12 @@ module Types class Sum include Composition - def self.operator - :| - end + def self.operator = :| # @return [Boolean] # # @api public - def optional? - primitive?(nil) - end + def optional? = primitive?(nil) # @param [Object] input #