Class: Yoda::Model::TypeExpressions::UnionType
- Defined in:
- lib/yoda/model/type_expressions/union_type.rb
Instance Attribute Summary collapse
-
#types ⇒ Object
readonly
Returns the value of attribute types.
Class Method Summary collapse
Instance Method Summary collapse
- #change_root(paths) ⇒ self
- #eql?(another) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(types) ⇒ UnionType
constructor
A new instance of UnionType.
- #map(&block) ⇒ self
- #resolve(registry) ⇒ Array<Store::Objects::Base>
- #to_rbs_type(env) ⇒ Object
- #to_s ⇒ String
Methods inherited from Base
Constructor Details
#initialize(types) ⇒ UnionType
Returns a new instance of UnionType.
19 20 21 |
# File 'lib/yoda/model/type_expressions/union_type.rb', line 19 def initialize(types) @types = types end |
Instance Attribute Details
#types ⇒ Object (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
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
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
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 |
#hash ⇒ Object
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
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>
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
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_s ⇒ String
44 45 46 |
# File 'lib/yoda/model/type_expressions/union_type.rb', line 44 def to_s types.map(&:to_s).join(' | ') end |