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

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

Overview

API:

  • public

Constant Summary collapse

DEFAULT =

API:

  • public

PStructType.new(EMPTY_ARRAY)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #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.

API:

  • public



1760
1761
1762
# File 'lib/puppet/pops/types/types.rb', line 1760

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object

API:

  • public



1756
1757
1758
# File 'lib/puppet/pops/types/types.rb', line 1756

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

API:

  • public



1764
1765
1766
1767
# File 'lib/puppet/pops/types/types.rb', line 1764

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

#eachObject

API:

  • public



1769
1770
1771
1772
1773
1774
1775
# File 'lib/puppet/pops/types/types.rb', line 1769

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

#elementsObject

API:

  • public



1832
1833
1834
# File 'lib/puppet/pops/types/types.rb', line 1832

def elements
  @elements
end

#eql?(o) ⇒ Boolean

Returns:

API:

  • public



1828
1829
1830
# File 'lib/puppet/pops/types/types.rb', line 1828

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

#generalizeObject

API:

  • public



1777
1778
1779
1780
1781
1782
1783
# File 'lib/puppet/pops/types/types.rb', line 1777

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

#hashObject

API:

  • public



1797
1798
1799
# File 'lib/puppet/pops/types/types.rb', line 1797

def hash
  @elements.hash
end

#hashed_elementsObject

API:

  • public



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

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

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

Returns:

API:

  • public



1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
# File 'lib/puppet/pops/types/types.rb', line 1836

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

Returns:

API:

  • public



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

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object

API:

  • public



1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
# File 'lib/puppet/pops/types/types.rb', line 1805

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

API:

  • public



1852
1853
1854
1855
1856
# File 'lib/puppet/pops/types/types.rb', line 1852

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

API:

  • public



1785
1786
1787
1788
1789
1790
1791
# File 'lib/puppet/pops/types/types.rb', line 1785

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

API:

  • public



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

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