Sha256: f238d6e60ab3c757030a5e26a854057a373cf687c46a978a3f328a56bcda6021

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'dry/data/type/optional'
require 'dry/data/type/hash'
require 'dry/data/type/array'

require 'dry/data/sum_type'

module Dry
  module Data
    class Type
      attr_reader :constructor

      attr_reader :primitive

      def self.[](primitive)
        if primitive == ::Array
          Type::Array
        elsif primitive == ::Hash
          Type::Hash
        else
          Type
        end
      end

      def self.strict_constructor(primitive, input)
        if input.is_a?(primitive)
          input
        else
          raise TypeError, "#{input.inspect} has invalid type"
        end
      end

      def self.passthrough_constructor(input)
        input
      end

      def initialize(constructor, primitive)
        @constructor = constructor
        @primitive = primitive
      end

      def name
        primitive.name
      end

      def call(input)
        constructor[input]
      end
      alias_method :[], :call

      def valid?(input)
        input.is_a?(primitive)
      end

      def |(other)
        Data.SumType(self, other)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-data-0.2.1 lib/dry/data/type.rb
dry-data-0.2.0 lib/dry/data/type.rb