From 95e339515fb61cdffeb09a0846d9e92291881483 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Mon, 6 Jan 2025 15:03:39 +0100 Subject: [PATCH] Update syntax --- lib/dry/struct.rb | 4 +-- lib/dry/struct/class_interface.rb | 41 ++++++----------------- lib/dry/struct/extensions/pretty_print.rb | 2 +- lib/dry/struct/hashify.rb | 6 ++-- lib/dry/struct/struct_builder.rb | 12 ++----- lib/dry/struct/sum.rb | 4 +-- lib/dry/struct/value.rb | 4 +-- 7 files changed, 20 insertions(+), 53 deletions(-) diff --git a/lib/dry/struct.rb b/lib/dry/struct.rb index 1629fa8..f6569b1 100644 --- a/lib/dry/struct.rb +++ b/lib/dry/struct.rb @@ -221,9 +221,7 @@ def inspect # Pattern matching support # # @api private - def deconstruct_keys(_keys) - attributes - end + def deconstruct_keys(_keys) = attributes end end diff --git a/lib/dry/struct/class_interface.rb b/lib/dry/struct/class_interface.rb index afbc680..93db76c 100644 --- a/lib/dry/struct/class_interface.rb +++ b/lib/dry/struct/class_interface.rb @@ -313,48 +313,32 @@ def try_struct(input) # @param [({Symbol => Object})] args # @return [Dry::Types::Result::Success] - def success(*args) - result(Types::Result::Success, *args) - end + def success(*args) = result(Types::Result::Success, *args) # @param [({Symbol => Object})] args # @return [Dry::Types::Result::Failure] - def failure(*args) - result(Types::Result::Failure, *args) - end + def failure(*args) = result(Types::Result::Failure, *args) # @param [Class] klass # @param [({Symbol => Object})] args - def result(klass, *args) - klass.new(*args) - end + def result(klass, *args) = klass.new(*args) # @return [false] - def default? - false - end + def default? = false # @param [Object, Dry::Struct] other # @return [Boolean] - def ===(other) - other.is_a?(self) - end + def ===(other) = other.is_a?(self) alias_method :primitive?, :=== # @return [true] - def constrained? - true - end + def constrained? = true # @return [self] - def primitive - self - end + def primitive = self # @return [false] - def optional? - false - end + def optional? = false # @return [Proc] def to_proc @@ -365,9 +349,7 @@ def to_proc # # @param [Symbol] key Attribute name # @return [Boolean] - def has_attribute?(key) - schema.key?(key) - end + def has_attribute?(key) = schema.key?(key) # Gets the list of attribute names # @@ -483,10 +465,7 @@ def #{key} # def email private :define_accessors # @api private - def valid_method_name?(key) - key.to_s.match?(/\A[a-zA-Z_]\w*\z/) - end - private :valid_method_name? + private def valid_method_name?(key) = key.to_s.match?(/\A[a-zA-Z_]\w*\z/) end end end diff --git a/lib/dry/struct/extensions/pretty_print.rb b/lib/dry/struct/extensions/pretty_print.rb index ae63e8d..262a5a9 100644 --- a/lib/dry/struct/extensions/pretty_print.rb +++ b/lib/dry/struct/extensions/pretty_print.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "pp" # rubocop:disable Lint/RedundantRequireStatement +require "pp" module Dry class Struct diff --git a/lib/dry/struct/hashify.rb b/lib/dry/struct/hashify.rb index 75ad871..e52a423 100644 --- a/lib/dry/struct/hashify.rb +++ b/lib/dry/struct/hashify.rb @@ -9,11 +9,11 @@ module Hashify # @return [Hash, Array] def self.[](value) if value.is_a?(Struct) - value.to_h.transform_values { |current| self[current] } + value.to_h.transform_values { self[_1] } elsif value.respond_to?(:to_hash) - value.to_hash.transform_values { |current| self[current] } + value.to_hash.transform_values { self[_1] } elsif value.respond_to?(:to_ary) - value.to_ary.map { |item| self[item] } + value.to_ary.map { self[_1] } else value end diff --git a/lib/dry/struct/struct_builder.rb b/lib/dry/struct/struct_builder.rb index d6dfe15..5b84039 100644 --- a/lib/dry/struct/struct_builder.rb +++ b/lib/dry/struct/struct_builder.rb @@ -43,17 +43,13 @@ def call(attr_name, type, &) private - def type?(type) - type.is_a?(Types::Type) - end + def type?(type) = type.is_a?(Types::Type) def array?(type) type?(type) && !type.optional? && type.primitive.equal?(::Array) end - def optional?(type) - type?(type) && type.optional? - end + def optional?(type) = type?(type) && type.optional? def parent(type) if array?(type) @@ -95,9 +91,7 @@ def visit_array(node) visit(member) end - def visit_nominal(*) - Undefined - end + def visit_nominal(*) = Undefined def visit_constructor(node) definition, * = node diff --git a/lib/dry/struct/sum.rb b/lib/dry/struct/sum.rb index 8af8d70..14357a4 100644 --- a/lib/dry/struct/sum.rb +++ b/lib/dry/struct/sum.rb @@ -36,9 +36,7 @@ def |(type) end # @return [boolean] - def ===(value) - left === value || right === value - end + def ===(value) = left === value || right === value protected diff --git a/lib/dry/struct/value.rb b/lib/dry/struct/value.rb index fab9fd4..82e4544 100644 --- a/lib/dry/struct/value.rb +++ b/lib/dry/struct/value.rb @@ -29,9 +29,7 @@ class Value < self # @param (see ClassInterface#new) # @return [Value] # @see https://github.com/dkubb/ice_nine - def self.new(*) - ::IceNine.deep_freeze(super) - end + def self.new(*) = ::IceNine.deep_freeze(super) end deprecate_constant :Value