Sha256: edab00fd9abebd28f6607b60eb003f9f919b5da2dcd1a299913014c9d2f28c9c

Contents?: true

Size: 825 Bytes

Versions: 6

Compression:

Stored size: 825 Bytes

Contents

require "dry-types"

module Kanji
  module Types
    module TypeInterface
      def try(obj, &block)
        result = valid?(obj) ? success(obj) : failure(obj, constraint_error(obj))

        return result if result.success?

        if block
          yield(result)
        else
          result
        end
      end

      def success(obj)
        Dry::Types::Result::Success.new(obj)
      end

      def failure(obj, error)
        Dry::Types::Result::Failure.new(obj, error)
      end

      def |(other)
        Dry::Types::Sum::Constrained.new(self, other)
      end

      def optional
        Dry::Types::Sum.new(Kanji::Types::Nil, self)
      end

      def constrained?
        true
      end

      def valid?
        raise NotImplementedError, "You must implement the valid? method"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kanji-web-0.3.1 lib/kanji/types/type_interface.rb
kanji-web-0.3.0 lib/kanji/types/type_interface.rb
kanji-web-0.2.2 lib/kanji/types/type_interface.rb
kanji-web-0.2.1 lib/kanji/types/type_interface.rb
kanji-web-0.2.0 lib/kanji/types/type_interface.rb
kanji-web-0.1.0 lib/kanji/types/type_interface.rb