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 inherited from PAnyType

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

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



1808
1809
1810
# File 'lib/puppet/pops/types/types.rb', line 1808

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object



1804
1805
1806
# File 'lib/puppet/pops/types/types.rb', line 1804

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



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

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

#eachObject



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

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

#elementsObject



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

def elements
  @elements
end

#eql?(o) ⇒ Boolean



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

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

#generalizeObject



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

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

#hashObject



1845
1846
1847
# File 'lib/puppet/pops/types/types.rb', line 1845

def hash
  @elements.hash
end

#hashed_elementsObject



1841
1842
1843
# File 'lib/puppet/pops/types/types.rb', line 1841

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

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



1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
# File 'lib/puppet/pops/types/types.rb', line 1884

def instance?(o, guard = nil)
  return false unless o.is_a?(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



1849
1850
1851
# File 'lib/puppet/pops/types/types.rb', line 1849

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
# File 'lib/puppet/pops/types/types.rb', line 1853

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



1900
1901
1902
1903
1904
# File 'lib/puppet/pops/types/types.rb', line 1900

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



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

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



1866
1867
1868
1869
1870
1871
1872
1873
1874
# File 'lib/puppet/pops/types/types.rb', line 1866

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