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, #create, #kind_of_callable?, #loader, #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, #to_s

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



1996
1997
1998
# File 'lib/puppet/pops/types/types.rb', line 1996

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object



1992
1993
1994
# File 'lib/puppet/pops/types/types.rb', line 1992

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



2000
2001
2002
2003
# File 'lib/puppet/pops/types/types.rb', line 2000

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

#eachObject



2005
2006
2007
2008
2009
2010
2011
# File 'lib/puppet/pops/types/types.rb', line 2005

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

#elementsObject



2067
2068
2069
# File 'lib/puppet/pops/types/types.rb', line 2067

def elements
  @elements
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2063
2064
2065
# File 'lib/puppet/pops/types/types.rb', line 2063

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

#generalizeObject



2013
2014
2015
2016
2017
2018
2019
# File 'lib/puppet/pops/types/types.rb', line 2013

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

#hashObject



2033
2034
2035
# File 'lib/puppet/pops/types/types.rb', line 2033

def hash
  @elements.hash
end

#hashed_elementsObject



2029
2030
2031
# File 'lib/puppet/pops/types/types.rb', line 2029

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

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

Returns:

  • (Boolean)


2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
# File 'lib/puppet/pops/types/types.rb', line 2071

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)


2037
2038
2039
# File 'lib/puppet/pops/types/types.rb', line 2037

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
# File 'lib/puppet/pops/types/types.rb', line 2041

def iterable_type(guard = nil)
  if self == DEFAULT
    PIterableType.new(PHashType::DEFAULT_KEY_PAIR_TUPLE)
  else
    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_functionObject



2088
2089
2090
2091
2092
# File 'lib/puppet/pops/types/types.rb', line 2088

def new_function
  # 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)
end

#normalize(guard = nil) ⇒ Object



2021
2022
2023
2024
2025
2026
2027
# File 'lib/puppet/pops/types/types.rb', line 2021

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

#resolve(loader) ⇒ Object



2053
2054
2055
2056
2057
2058
2059
2060
2061
# File 'lib/puppet/pops/types/types.rb', line 2053

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