Sha256: 5f36179ed11f46c75644dbc76e16f516cf7fb250564d78be4076507d74475923

Contents?: true

Size: 555 Bytes

Versions: 3

Compression:

Stored size: 555 Bytes

Contents

require 'dry/types/decorator'

module Dry
  module Types
    class Safe
      include Decorator
      include Builder

      def call(input)
        if type.primitive?(input) || type.is_a?(Sum) || type.is_a?(Constructor)
          type[input]
        else
          input
        end
      rescue TypeError
        input
      end
      alias_method :[], :call

      def try(input, &block)
        type.try(input, &block)
      end

      private

      def decorate?(response)
        super || response.kind_of?(Constructor)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-0.7.2 lib/dry/types/safe.rb
dry-types-0.7.1 lib/dry/types/safe.rb
dry-types-0.7.0 lib/dry/types/safe.rb