Class: Puppet::Pops::Types::PVariantType

Inherits:
PAnyType show all
Includes:
Enumerable
Defined in:
lib/puppet/pops/types/types.rb

Overview

A flexible type describing an any? of other types

Constant Summary collapse

DATA =

Variant compatible with the Data type.

PVariantType.new([PHashType::DATA, PArrayType::DATA, PScalarType::DEFAULT, PUndefType::DEFAULT, PTupleType::DATA])
DEFAULT =
PVariantType.new([])

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#assignable?, #callable?, #callable_args?, #enumerable?, #simple_name, #to_s

Methods included from Visitable

#accept

Constructor Details

#initialize(types) ⇒ PVariantType

Returns a new instance of PVariantType.



1316
1317
1318
# File 'lib/puppet/pops/types/types.rb', line 1316

def initialize(types)
  @types = types.freeze
end

Instance Attribute Details

#typesObject (readonly)



1314
1315
1316
# File 'lib/puppet/pops/types/types.rb', line 1314

def types
  @types
end

Instance Method Details

#==(o) ⇒ Object



1345
1346
1347
1348
1349
# File 'lib/puppet/pops/types/types.rb', line 1345

def ==(o)
  # TODO: This special case doesn't look like it belongs here
  self.class == o.class && (@types | o.types).size == @types.size ||
      o.class == PDataType && self == DATA
end

#eachObject



1320
1321
1322
1323
1324
1325
1326
# File 'lib/puppet/pops/types/types.rb', line 1320

def each
  if block_given?
    types.each { |t| yield t }
  else
    types.to_enum
  end
end

#generalizeObject



1328
1329
1330
# File 'lib/puppet/pops/types/types.rb', line 1328

def generalize
  (self == DEFAULT || self == DATA) ? self : PVariantType.new(@types.map {|t| t.generalize})
end

#hashObject



1332
1333
1334
# File 'lib/puppet/pops/types/types.rb', line 1332

def hash
  @types.hash
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


1336
1337
1338
1339
# File 'lib/puppet/pops/types/types.rb', line 1336

def instance?(o)
  # instance of variant if o is instance? of any of variant's types
  @types.any? { |type| type.instance?(o) }
end

#kind_of_callable?(optional = true) ⇒ Boolean

Returns:

  • (Boolean)


1341
1342
1343
# File 'lib/puppet/pops/types/types.rb', line 1341

def kind_of_callable?(optional = true)
  @types.all? { |type| type.kind_of_callable?(optional) }
end