Sha256: 1c65906b9cf058807483e8b2048ff95aa53d5d813b266b2f121ae133bcb4100d

Contents?: true

Size: 827 Bytes

Versions: 6

Compression:

Stored size: 827 Bytes

Contents

# frozen_string_literal: true

module Kind
  class UnionType
    include Kind::BasicObject

    Interface = Kind::RespondTo[:name, :===]

    singleton_class.send(:alias_method, :[], :new)

    attr_reader :inspect

    def initialize(kind)
      @kinds = Array(kind)
      @inspect = "(#{@kinds.map(&:name).join(' | ')})"
    end

    def |(kind)
      self.class.new(@kinds + [Interface[kind.nil? ? Kind::Nil : kind]])
    end

    def ===(value)
      @kinds.any? { |kind| kind === value }
    end

    alias_method :name, :inspect

    module Buildable
      def |(another_kind)
        __union_type | another_kind
      end

      private

        def __union_type
          @__union_type ||= UnionType[self]
        end
    end

    private_constant :Interface
  end

  RespondTo.send(:include, UnionType::Buildable)
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kind-5.10.0 lib/kind/objects/union_type.rb
kind-5.9.0 lib/kind/objects/union_type.rb
kind-5.8.1 lib/kind/objects/union_type.rb
kind-5.8.0 lib/kind/objects/union_type.rb
kind-5.7.0 lib/kind/objects/union_type.rb
kind-5.6.0 lib/kind/objects/union_type.rb