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?, #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

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



1905
1906
1907
# File 'lib/puppet/pops/types/types.rb', line 1905

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

Class Method Details

.register_ptype(loader, ir) ⇒ Object



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

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



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

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

#eachObject



1914
1915
1916
1917
1918
1919
1920
# File 'lib/puppet/pops/types/types.rb', line 1914

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

#elementsObject



1977
1978
1979
# File 'lib/puppet/pops/types/types.rb', line 1977

def elements
  @elements
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1973
1974
1975
# File 'lib/puppet/pops/types/types.rb', line 1973

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

#generalizeObject



1922
1923
1924
1925
1926
1927
1928
# File 'lib/puppet/pops/types/types.rb', line 1922

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

#hashObject



1942
1943
1944
# File 'lib/puppet/pops/types/types.rb', line 1942

def hash
  @elements.hash
end

#hashed_elementsObject



1938
1939
1940
# File 'lib/puppet/pops/types/types.rb', line 1938

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

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

Returns:

  • (Boolean)


1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
# File 'lib/puppet/pops/types/types.rb', line 1981

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)


1946
1947
1948
# File 'lib/puppet/pops/types/types.rb', line 1946

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
# File 'lib/puppet/pops/types/types.rb', line 1950

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_functionObject



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

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



1930
1931
1932
1933
1934
1935
1936
# File 'lib/puppet/pops/types/types.rb', line 1930

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

#resolve(loader) ⇒ Object



1963
1964
1965
1966
1967
1968
1969
1970
1971
# File 'lib/puppet/pops/types/types.rb', line 1963

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