Sha256: 0fd1f75c78366ad662cf24c52ef37c2b2295e3d43e0d12d6b2e01f42dad91955

Contents?: true

Size: 803 Bytes

Versions: 5

Compression:

Stored size: 803 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]])
    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

5 entries across 5 versions & 1 rubygems

Version Path
kind-5.5.0 lib/kind/objects/union_type.rb
kind-5.4.1 lib/kind/objects/union_type.rb
kind-5.4.0 lib/kind/objects/union_type.rb
kind-5.3.0 lib/kind/objects/union_type.rb
kind-5.2.0 lib/kind/objects/union_type.rb