Class: Puppet::Pops::Types::PStructType

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

Constant Summary collapse

DEFAULT =
PStructType.new(EMPTY_ARRAY)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#uniq

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #kind_of_callable?, #name, new_function, #really_instance?, #roundtrip_with_string?, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_pcore_type, create_ptype, register_ptypes

Methods included from PuppetObject

#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



1814
1815
1816
# File 'lib/puppet/pops/types/types.rb', line 1814

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object



1810
1811
1812
# File 'lib/puppet/pops/types/types.rb', line 1810

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType', 'elements' => PArrayType.new(PTypeReferenceType.new('Pcore::StructElement')))
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1818
1819
1820
1821
# File 'lib/puppet/pops/types/types.rb', line 1818

def accept(visitor, guard)
  super
  @elements.each { |elem| elem.accept(visitor, guard) }
end

#eachObject



1823
1824
1825
1826
1827
1828
1829
# File 'lib/puppet/pops/types/types.rb', line 1823

def each
  if block_given?
    elements.each { |elem| yield elem }
  else
    elements.to_enum
  end
end

#elementsObject



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

def elements
  @elements
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1882
1883
1884
# File 'lib/puppet/pops/types/types.rb', line 1882

def eql?(o)
  self.class == o.class && @elements == o.elements
end

#generalizeObject



1831
1832
1833
1834
1835
1836
1837
# File 'lib/puppet/pops/types/types.rb', line 1831

def generalize
  if @elements.empty?
    DEFAULT
  else
    alter_type_array(@elements, :generalize) { |altered| PStructType.new(altered) }
  end
end

#hashObject



1851
1852
1853
# File 'lib/puppet/pops/types/types.rb', line 1851

def hash
  @elements.hash
end

#hashed_elementsObject



1847
1848
1849
# File 'lib/puppet/pops/types/types.rb', line 1847

def hashed_elements
  @hashed ||= @elements.reduce({}) {|memo, e| memo[e.name] = e; memo }
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
# File 'lib/puppet/pops/types/types.rb', line 1890

def instance?(o, guard = nil)
  # The inferred type of a class derived from Hash is either Runtime or Object. It's not assignable to the Struct type.
  return false unless o.instance_of?(Hash)
  matched = 0
  @elements.all? do |e|
    key = e.name
    v = o[key]
    if v.nil? && !o.include?(key)
      # Entry is missing. Only OK when key is optional
      e.key_type.assignable?(PUndefType::DEFAULT, guard)
    else
      matched += 1
      e.value_type.instance?(v, guard)
    end
  end && matched == o.size
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1855
1856
1857
# File 'lib/puppet/pops/types/types.rb', line 1855

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
# File 'lib/puppet/pops/types/types.rb', line 1859

def iterable_type(guard = nil)
  if self == DEFAULT
    PIterableType.new(PHashType::DEFAULT_KEY_PAIR_TUPLE)
  else
    tc = TypeCalculator.singleton
    PIterableType.new(
      PTupleType.new([
        PVariantType.maybe_create(@elements.map {|se| se.key_type }),
        PVariantType.maybe_create(@elements.map {|se| se.value_type })],
        PHashType::KEY_PAIR_TUPLE_SIZE))
  end
end

#new_function(loader) ⇒ Object



1907
1908
1909
1910
1911
# File 'lib/puppet/pops/types/types.rb', line 1907

def new_function(loader)
  # Simply delegate to Hash type and let the higher level assertion deal with
  # compliance with the Struct type regarding the produced result.
  PHashType.new_function(self, loader)
end

#normalize(guard = nil) ⇒ Object



1839
1840
1841
1842
1843
1844
1845
# File 'lib/puppet/pops/types/types.rb', line 1839

def normalize(guard = nil)
  if @elements.empty?
    DEFAULT
  else
    alter_type_array(@elements, :normalize, guard) { |altered| PStructType.new(altered) }
  end
end

#resolve(type_parser, loader) ⇒ Object



1872
1873
1874
1875
1876
1877
1878
1879
1880
# File 'lib/puppet/pops/types/types.rb', line 1872

def resolve(type_parser, loader)
  changed = false
  relements = @elements.map do |elem|
    relem = elem.resolve(type_parser, loader)
    changed ||= !relem.equal?(elem)
    relem
  end
  changed ? self.class.new(relements) : self
end