Class: Yoda::Model::TypeExpressions::UnionType

Inherits:
Base
  • Object
show all
Defined in:
lib/yoda/model/type_expressions/union_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==

Constructor Details

#initialize(types) ⇒ UnionType

Returns a new instance of UnionType.

Parameters:

  • types (Array<Base>)


19
20
21
# File 'lib/yoda/model/type_expressions/union_type.rb', line 19

def initialize(types)
  @types = types
end

Instance Attribute Details

#typesObject (readonly)

Returns the value of attribute types.



7
8
9
# File 'lib/yoda/model/type_expressions/union_type.rb', line 7

def types
  @types
end

Class Method Details

.new(types) ⇒ Base

Parameters:

  • types (Array<Base>)

Returns:



11
12
13
14
15
16
# File 'lib/yoda/model/type_expressions/union_type.rb', line 11

def self.new(types)
  reduced_types = types.reject { |type| type.is_a?(AnyType) || type.is_a?(UnknownType) }.uniq
  return (types.first || AnyType.new) if reduced_types.length == 0
  return reduced_types.first if reduced_types.length == 1
  super(reduced_types)
end

Instance Method Details

#change_root(paths) ⇒ self

Parameters:

  • paths (Array<Path>)

Returns:

  • (self)


33
34
35
# File 'lib/yoda/model/type_expressions/union_type.rb', line 33

def change_root(paths)
  self.class.new(types.map { |type| type.change_root(paths) })
end

#eql?(another) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/yoda/model/type_expressions/union_type.rb', line 23

def eql?(another)
  another.is_a?(UnionType) && Set.new(types) == Set.new(another.types)
end

#hashObject



27
28
29
# File 'lib/yoda/model/type_expressions/union_type.rb', line 27

def hash
  [self.class.name, Set.new(types)].hash
end

#map(&block) ⇒ self

Returns:

  • (self)


54
55
56
# File 'lib/yoda/model/type_expressions/union_type.rb', line 54

def map(&block)
  self.class.new(types.map(&block))
end

#resolve(registry) ⇒ Array<Store::Objects::Base>

Parameters:

  • registry (Registry)

Returns:



39
40
41
# File 'lib/yoda/model/type_expressions/union_type.rb', line 39

def resolve(registry)
  types.map { |type| type.resolve(registry) }.flatten.compact
end

#to_rbs_type(env) ⇒ Object

Parameters:



49
50
51
# File 'lib/yoda/model/type_expressions/union_type.rb', line 49

def to_rbs_type(env)
  RBS::Types::Union.new(types: types.map { |t| t.to_rbs_type(env) }, location: nil)
end

#to_sString

Returns:

  • (String)


44
45
46
# File 'lib/yoda/model/type_expressions/union_type.rb', line 44

def to_s
  types.map(&:to_s).join(' | ')
end