lib/dry/struct/class_interface.rb in dry-struct-1.3.0 vs lib/dry/struct/class_interface.rb in dry-struct-1.4.0
- old
+ new
@@ -1,15 +1,15 @@
# frozen_string_literal: true
-require 'weakref'
-require 'dry/core/class_attributes'
-require 'dry/core/inflector'
-require 'dry/core/descendants_tracker'
+require "weakref"
+require "dry/core/class_attributes"
+require "dry/core/inflector"
+require "dry/core/descendants_tracker"
-require 'dry/struct/errors'
-require 'dry/struct/constructor'
-require 'dry/struct/sum'
+require "dry/struct/errors"
+require "dry/struct/constructor"
+require "dry/struct/sum"
module Dry
class Struct
# Class-level interface of {Struct} and {Value}
module ClassInterface
@@ -20,11 +20,11 @@
# @param [Class] klass
def inherited(klass)
super
- unless klass.name.eql?('Dry::Struct::Value')
+ unless klass.name.eql?("Dry::Struct::Value")
klass.extend(Core::DescendantsTracker)
end
end
# Adds an attribute for this {Struct} with given `name` and `type`
@@ -147,12 +147,12 @@
# @return [Dry::Struct]
#
def attribute?(*args, &block)
if args.size == 1 && block.nil?
Core::Deprecations.warn(
- 'Dry::Struct.attribute? is deprecated for checking attribute presence, '\
- 'use has_attribute? instead',
+ "Dry::Struct.attribute? is deprecated for checking attribute presence, "\
+ "use has_attribute? instead",
tag: :'dry-struct'
)
has_attribute?(args[0])
else
@@ -179,22 +179,22 @@
# # => #<Dry::Types[Constructor<Schema<keys={
# # title: Constrained<Nominal<String> rule=[type?(String)]>
# # author: Constrained<Nominal<String> rule=[type?(String)]>
# # }> fn=Kernel.Hash>]>
def attributes(new_schema)
- keys = new_schema.keys.map { |k| k.to_s.chomp('?').to_sym }
+ keys = new_schema.keys.map { |k| k.to_s.chomp("?").to_sym }
check_schema_duplication(keys)
schema schema.schema(new_schema)
define_accessors(keys)
@attribute_names = nil
direct_descendants = descendants.select { |d| d.superclass == self }
direct_descendants.each do |d|
- inherited_attrs = new_schema.reject { |k, _| d.has_attribute?(k.to_s.chomp('?').to_sym) }
+ inherited_attrs = new_schema.reject { |k, _| d.has_attribute?(k.to_s.chomp("?").to_sym) }
d.attributes(inherited_attrs)
end
self
end
@@ -231,11 +231,11 @@
# # => #<Book title="The Old Man and the Sea">
def transform_keys(proc = nil, &block)
schema schema.with_key_transform(proc || block)
end
- # @param [Hash{Symbol => Dry::Types::Type, Dry::Struct}] new_schema
+ # @param [Hash{Symbol => Dry::Types::Type, Dry::Struct}] new_keys
# @raise [RepeatedAttributeError] when trying to define attribute with the
# same name as previously defined one
def check_schema_duplication(new_keys)
overlapping_keys = new_keys & (attribute_names - superclass.attribute_names)
@@ -262,12 +262,12 @@
elsif safe
load(schema.call_safe(attributes) { |output = attributes| return yield output })
else
load(schema.call_unsafe(attributes))
end
- rescue Types::CoercionError => error
- raise Error, "[#{self}.new] #{error}", error.backtrace
+ rescue Types::CoercionError => e
+ raise Error, "[#{self}.new] #{e}", e.backtrace
end
# @api private
def call_safe(input, &block)
if input.is_a?(self)
@@ -287,19 +287,19 @@
end
# @api private
def load(attributes)
struct = allocate
- struct.send(:initialize, attributes)
+ struct.__send__(:initialize, attributes)
struct
end
# @param [#call,nil] constructor
# @param [#call,nil] block
# @return [Dry::Struct::Constructor]
def constructor(constructor = nil, **, &block)
- Constructor.new(self, fn: constructor || block)
+ Constructor[self, fn: constructor || block]
end
# @param [Hash{Symbol => Object},Dry::Struct] input
# @yieldparam [Dry::Types::Result::Failure] failure
# @yieldreturn [Dry::Types::ResultResult]
@@ -343,11 +343,11 @@
# @return [false]
def default?
false
end
- # @param [Object, Dry::Struct] value
+ # @param [Object, Dry::Struct] other
# @return [Boolean]
def ===(other)
other.is_a?(self)
end
alias_method :primitive?, :===
@@ -461,10 +461,10 @@
if type.is_a?(::String)
Types[type]
elsif block.nil? && Undefined.equal?(type)
raise(
::ArgumentError,
- 'you must supply a type or a block to `Dry::Struct.attribute`'
+ "you must supply a type or a block to `Dry::Struct.attribute`"
)
else
type
end