Class: T::Types::Intersection

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

Overview

Takes a list of types. Validates that an object matches all of the types.

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(types) ⇒ Intersection

Returns a new instance of Intersection.



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

def initialize(types)
  @inner_types = types
end

Instance Method Details

#build_typeObject



23
24
25
26
# File 'lib/types/types/intersection.rb', line 23

def build_type
  types
  nil
end

#nameObject

overrides Base



29
30
31
# File 'lib/types/types/intersection.rb', line 29

def name
  "T.all(#{types.map(&:name).compact.sort.join(', ')})"
end

#recursively_valid?(obj) ⇒ Boolean

overrides Base

Returns:



34
35
36
# File 'lib/types/types/intersection.rb', line 34

def recursively_valid?(obj)
  types.all? {|type| type.recursively_valid?(obj)}
end

#typesObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/types/types/intersection.rb', line 11

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

#valid?(obj) ⇒ Boolean

overrides Base

Returns:



39
40
41
# File 'lib/types/types/intersection.rb', line 39

def valid?(obj)
  types.all? {|type| type.valid?(obj)}
end