lib/dry/monads/validated.rb in dry-monads-1.3.5 vs lib/dry/monads/validated.rb in dry-monads-1.4.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true -require 'dry/monads/conversion_stubs' -require 'dry/monads/constants' -require 'dry/monads/right_biased' +require "dry/monads/conversion_stubs" +require "dry/monads/constants" +require "dry/monads/right_biased" module Dry module Monads # Validated is similar to Result and represents an outcome of a validation. # The difference between Validated and Result is that the former implements @@ -49,19 +49,22 @@ # Bind/flat_map is not implemented # def bind(*) # See https://typelevel.org/cats/datatypes/validated.html for details on why - raise NotImplementedError, 'Validated is not a monad because it would violate the monad laws' + raise NotImplementedError, + "Validated is not a monad because it would violate the monad laws" end # Valid result # class Valid < Validated include Dry::Equalizer(:value!) def initialize(value) + super() + @value = value end # Extracts the value # @@ -84,13 +87,12 @@ # Validated.pure { |x| x + 1 }.apply { Valid(4) } # => Valid(5) # # @yieldreturn [Validated::Valid,Validated::Invalid] # @return [Validated::Valid,Validated::Invalid] # - # @return [Validated::Valid] - def apply(val = Undefined) - Undefined.default(val) { yield }.fmap(Curry.(value!)) + def apply(val = Undefined, &block) + Undefined.default(val, &block).fmap(Curry.(value!)) end # Lifts a block/proc over Valid # # @overload fmap(proc) @@ -121,21 +123,21 @@ end # @return [String] def inspect if Unit.equal?(@value) - 'Valid()' + "Valid()" else "Valid(#{@value.inspect})" end end alias_method :to_s, :inspect # @param other [Object] # @return [Boolean] def ===(other) - self.class == other.class && value! === other.value! + other.instance_of?(self.class) && value! === other.value! end end # Invalid result # @@ -152,10 +154,12 @@ attr_reader :trace include Dry::Equalizer(:error) def initialize(error, trace = RightBiased::Left.trace_caller) + super() + @error = error @trace = trace end # Collects errors (ignores valid results) @@ -166,13 +170,13 @@ # # @overload apply # @yieldreturn [Validated::Valid,Validated::Invalid] # @return [Validated::Invalid] # - def apply(val = Undefined) + def apply(val = Undefined, &block) Undefined - .default(val) { yield } + .default(val, &block) .alt_map { |v| @error + v } .fmap { return self } end # Lifts a block/proc over Invalid @@ -218,11 +222,11 @@ alias_method :to_s, :inspect # @param other [Object] # @return [Boolean] def ===(other) - self.class == other.class && error === other.error + other.instance_of?(self.class) && error === other.error end end # Mixin with Validated constructors # @@ -248,11 +252,11 @@ # @param block [Proc] # @return [Valdated::Valid] # def Valid(value = Undefined, &block) v = Undefined.default(value, block) - raise ArgumentError, 'No value given' if !value.nil? && v.nil? + raise ArgumentError, "No value given" if !value.nil? && v.nil? Valid.new(v) end # Invalid constructor @@ -265,11 +269,11 @@ # @param block [Proc] # @return [Valdated::Invalid] # def Invalid(value = Undefined, &block) v = Undefined.default(value, block) - raise ArgumentError, 'No value given' if !value.nil? && v.nil? + raise ArgumentError, "No value given" if !value.nil? && v.nil? Invalid.new(v, RightBiased::Left.trace_caller) end end @@ -295,16 +299,16 @@ end class Failure < Result # Transforms to Validated # - # @return [Validated::Valid] + # @return [Validated::Invalid] def to_validated Validated::Invalid.new(failure, trace) end end end - require 'dry/monads/registry' + require "dry/monads/registry" register_mixin(:validated, Validated::Mixin) end end