Class: T::Types::Union

Inherits:
Base
  • Object
show all
Defined in:
lib/types/types/union.rb

Overview

Takes a list of types. Validates that an object matches at least one of the types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #describe_obj, #error_message_for_obj, #hash, method_added, #subtype_of?, #to_s, #validate!

Constructor Details

#initialize(types) ⇒ Union

Returns a new instance of Union.



9
10
11
12
13
14
15
16
17
18
# File 'lib/types/types/union.rb', line 9

def initialize(types)
  @types = types.flat_map do |type|
    if type.is_a?(Union)
      # Simplify nested unions (mostly so `name` returns a nicer value)
      type.types
    else
      T::Utils.coerce(type)
    end
  end.uniq
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



7
8
9
# File 'lib/types/types/union.rb', line 7

def types
  @types
end

Instance Method Details

#nameObject



21
22
23
# File 'lib/types/types/union.rb', line 21

def name
  type_shortcuts(@types)
end

#valid?(obj) ⇒ Boolean

Returns:



45
46
47
48
49
50
51
# File 'lib/types/types/union.rb', line 45

def valid?(obj)
  @types.each do |type|
    return true if type.valid?(obj)
  end

  false
end