lib/dry/struct/class_interface.rb in dry-struct-1.1.1 vs lib/dry/struct/class_interface.rb in dry-struct-1.2.0

- old
+ new

@@ -10,24 +10,24 @@ class Struct # Class-level interface of {Struct} and {Value} module ClassInterface include Core::ClassAttributes - include Dry::Types::Type - include Dry::Types::Builder + include Types::Type + include Types::Builder # @param [Class] klass def inherited(klass) super base = self klass.class_eval do @meta = base.meta - unless equal?(Value) - extend Dry::Core::DescendantsTracker + unless name.eql?('Dry::Struct::Value') + extend Core::DescendantsTracker end end end # Adds an attribute for this {Struct} with given `name` and `type` @@ -114,11 +114,11 @@ # @param [Dry::Types::Type, nil] type or superclass of nested type # @return [Dry::Struct] # def attribute?(*args, &block) if args.size == 1 && block.nil? - Dry::Core::Deprecations.warn( + Core::Deprecations.warn( 'Dry::Struct.attribute? is deprecated for checking attribute presence, '\ 'use has_attribute? instead', tag: :'dry-struct' ) @@ -229,11 +229,11 @@ load(schema.call_safe(attributes) { |output = attributes| return yield output }) else load(schema.call_unsafe(attributes)) end rescue Types::CoercionError => error - raise Struct::Error, "[#{self}.new] #{error}" + raise Error, "[#{self}.new] #{error}" end # @api private def call_safe(input, &block) if input.is_a?(self) @@ -262,20 +262,20 @@ # @param [#call,nil] constructor # @param [Hash] _options # @param [#call,nil] block # @return [Dry::Struct::Constructor] def constructor(constructor = nil, **_options, &block) - Struct::Constructor.new(self, fn: constructor || block) + Constructor.new(self, fn: constructor || block) end # @param [Hash{Symbol => Object},Dry::Struct] input # @yieldparam [Dry::Types::Result::Failure] failure # @yieldreturn [Dry::Types::ResultResult] # @return [Dry::Types::Result] def try(input) success(self[input]) - rescue Struct::Error => e + rescue Error => e failure_result = failure(input, e.message) block_given? ? yield(failure_result) : failure_result end # @param [Hash{Symbol => Object},Dry::Struct] input @@ -296,11 +296,11 @@ end # @param [({Symbol => Object})] args # @return [Dry::Types::Result::Failure] def failure(*args) - result(::Dry::Types::Result::Failure, *args) + result(Types::Result::Failure, *args) end # @param [Class] klass # @param [({Symbol => Object})] args def result(klass, *args) @@ -357,22 +357,22 @@ # @return [{Symbol => Object}] def meta(meta = Undefined) if meta.equal?(Undefined) @meta else - Class.new(self) do + ::Class.new(self) do @meta = @meta.merge(meta) unless meta.empty? end end end # Build a sum type # @param [Dry::Types::Type] type # @return [Dry::Types::Sum] def |(type) - if type.is_a?(Class) && type <= Struct - Struct::Sum.new(self, type) + if type.is_a?(::Class) && type <= Struct + Sum.new(self, type) else super end end @@ -397,24 +397,24 @@ # Checks if the given type is a Dry::Struct # # @param [Dry::Types::Type] type # @return [Boolean] def struct?(type) - type.is_a?(Class) && type <= Struct + type.is_a?(::Class) && type <= Struct end private :struct? # Constructs a type # # @return [Dry::Types::Type, Dry::Struct] def build_type(name, type, &block) type_object = - if type.is_a?(String) - Dry::Types[type] + if type.is_a?(::String) + Types[type] elsif block.nil? && type.nil? raise( - ArgumentError, + ::ArgumentError, 'you must supply a type or a block to `Dry::Struct.attribute`' ) else type end @@ -424,8 +424,14 @@ else type_object end end private :build_type + + # @api private + # @return [Boolean] + def value? + false + end end end end