Class: Yoda::Model::Types::UnionType

Inherits:
Base
  • Object
show all
Defined in:
lib/yoda/model/types/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/types/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/types/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/types/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)


34
35
36
# File 'lib/yoda/model/types/union_type.rb', line 34

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

#eql?(another) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



28
29
30
# File 'lib/yoda/model/types/union_type.rb', line 28

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

#map(&block) ⇒ self

Returns:

  • (self)


50
51
52
# File 'lib/yoda/model/types/union_type.rb', line 50

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

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

Parameters:

  • registry (Registry)

Returns:



40
41
42
# File 'lib/yoda/model/types/union_type.rb', line 40

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

#to_sString

Returns:

  • (String)


45
46
47
# File 'lib/yoda/model/types/union_type.rb', line 45

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