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(EMPTY_ARRAY)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #iterable?, #iterable_type, #simple_name, #to_alias_expanded_s, #to_s

Constructor Details

#initialize(types) ⇒ PVariantType

Returns a new instance of PVariantType.

Parameters:



1795
1796
1797
# File 'lib/puppet/pops/types/types.rb', line 1795

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

Instance Attribute Details

#typesObject (readonly)



1792
1793
1794
# File 'lib/puppet/pops/types/types.rb', line 1792

def types
  @types
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1799
1800
1801
1802
# File 'lib/puppet/pops/types/types.rb', line 1799

def accept(visitor, guard)
  super
  @types.each { |t| t.accept(visitor, guard) }
end

#eachObject



1804
1805
1806
1807
1808
1809
1810
# File 'lib/puppet/pops/types/types.rb', line 1804

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1884
1885
1886
1887
# File 'lib/puppet/pops/types/types.rb', line 1884

def eql?(o)
  o = DATA if o.is_a?(PDataType)
  self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end

#generalizeObject



1812
1813
1814
1815
1816
1817
1818
# File 'lib/puppet/pops/types/types.rb', line 1812

def generalize
  if self == DEFAULT || self == DATA
    self
  else
    alter_type_array(@types, :generalize) { |altered| PVariantType.new(mod_types) }
  end
end

#hashObject



1867
1868
1869
# File 'lib/puppet/pops/types/types.rb', line 1867

def hash
  @types.hash
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


1871
1872
1873
1874
# File 'lib/puppet/pops/types/types.rb', line 1871

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, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1876
1877
1878
# File 'lib/puppet/pops/types/types.rb', line 1876

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

#normalize(guard = nil) ⇒ Object



1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
# File 'lib/puppet/pops/types/types.rb', line 1820

def normalize(guard = nil)
  if self == DEFAULT || self == DATA || @types.empty?
    self
  else
    # Normalize all contained types
    modified = false
    types = alter_type_array(@types, :normalize, guard)
    if types == self
      types = @types
    else
      modified = true
    end

    if types.size == 1
      types[0]
    elsif types.any? { |t| t.is_a?(PUndefType) }
      # Undef entry present. Use an OptionalType with a normalized Variant of all types that are not Undef
      POptionalType.new(PVariantType.new(types.reject { |ot| ot.is_a?(PUndefType) }).normalize(guard)).normalize(guard)
    else
      # Merge all variants into this one
      types = types.map do |t|
        if t.is_a?(PVariantType)
          modified = true
          t.types
        else
          t
        end
      end
      types.flatten! if modified
      size_before_merge = types.size

      types = swap_not_undefs(types)
      types = swap_optionals(types)
      types = merge_enums(types)
      types = merge_patterns(types)
      types = merge_int_ranges(types)
      types = merge_float_ranges(types)

      if types.size == 1
        types[0]
      else
        modified || types.size != size_before_merge ? PVariantType.new(types) : self
      end
    end
  end
end

#resolved?Boolean

Returns:

  • (Boolean)


1880
1881
1882
# File 'lib/puppet/pops/types/types.rb', line 1880

def resolved?
  @types.all? { |type| type.resolved? }
end