lib/dry/struct/class_interface.rb in dry-struct-1.4.0 vs lib/dry/struct/class_interface.rb in dry-struct-1.5.0
- old
+ new
@@ -1,20 +1,13 @@
# frozen_string_literal: true
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"
-
module Dry
class Struct
# Class-level interface of {Struct} and {Value}
- module ClassInterface
+ module ClassInterface # rubocop:disable Metrics/ModuleLength
include Core::ClassAttributes
include Types::Type
include Types::Builder
@@ -47,12 +40,16 @@
# attribute :details, Dry::Struct do
# attribute :type, Types::String
# end
# end
#
- # Language.schema
- # # => #<Dry::Types[Constructor<Schema<keys={name: Constrained<Nominal<String> rule=[type?(String)]> details: Language::Details}> fn=Kernel.Hash>]>
+ # Language.schema # new lines for readability
+ # # => #<Dry::Types[
+ # Constructor<Schema<keys={
+ # name: Constrained<Nominal<String> rule=[type?(String)]>
+ # details: Language::Details
+ # }> fn=Kernel.Hash>]>
#
# ruby = Language.new(name: 'Ruby', details: { type: 'OO' })
# ruby.name #=> 'Ruby'
# ruby.details #=> #<Language::Details type="OO">
# ruby.details.type #=> 'OO'
@@ -65,14 +62,16 @@
# attribute :name, Types::String
# attribute :pseudonym, Types::String
# end
# end
#
- # Language.schema
+ # Language.schema # new lines for readability
# => #<Dry::Types[Constructor<Schema<keys={
# name: Constrained<Nominal<String> rule=[type?(String)]>
- # versions: Constrained<Array<Constrained<Nominal<String> rule=[type?(String)]>> rule=[type?(Array)]>
+ # versions: Constrained<
+ # Array<Constrained<Nominal<String> rule=[type?(String)]>
+ # > rule=[type?(Array)]>
# celebrities: Constrained<Array<Language::Celebrity> rule=[type?(Array)]>
# }> fn=Kernel.Hash>]>
#
# ruby = Language.new(
# name: 'Ruby',
@@ -149,11 +148,11 @@
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",
- tag: :'dry-struct'
+ tag: :"dry-struct"
)
has_attribute?(args[0])
else
name, * = args
@@ -245,11 +244,11 @@
end
private :check_schema_duplication
# @param [Hash{Symbol => Object},Dry::Struct] attributes
# @raise [Struct::Error] if the given attributes don't conform {#schema}
- def new(attributes = default_attributes, safe = false, &block)
+ def new(attributes = default_attributes, safe = false, &block) # rubocop:disable Style/OptionalBooleanParameter
if attributes.is_a?(Struct)
if equal?(attributes.class)
attributes
else
# This implicit coercion is arguable but makes sense overall
@@ -479,13 +478,13 @@
def define_accessors(keys)
keys.each do |key|
next if instance_methods.include?(key)
- class_eval(<<-RUBY)
- def #{key}
- @attributes[#{key.inspect}]
- end
+ class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
+ def #{key} # def email
+ @attributes[#{key.inspect}] # @attributes[:email]
+ end # end
RUBY
end
end
private :define_accessors
end