Class: Puppet::Pops::Types::PTupleType

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

Constant Summary collapse

DATA =
PTupleType.new([PDataType::DEFAULT], PCollectionType::DEFAULT_SIZE)
DEFAULT =
PTupleType.new(EMPTY_ARRAY)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #check_self_recursion, #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(types, size_type = nil) ⇒ PTupleType

Returns a new instance of PTupleType.



1926
1927
1928
1929
# File 'lib/puppet/pops/types/types.rb', line 1926

def initialize(types, size_type = nil)
  @types = types
  @size_type = size_type.nil? ? nil : size_type.to_size
end

Instance Attribute Details

#size_typeObject (readonly)

If set, describes min and max required of the given types - if max > size of types, the last type entry repeats



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

def size_type
  @size_type
end

#typesObject (readonly)



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

def types
  @types
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



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

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType',
    'types' => PArrayType.new(PType::DEFAULT),
    'size_type' => {
      KEY_TYPE => POptionalType.new(PType.new(PIntegerType::DEFAULT)),
      KEY_VALUE => nil
    }
  )
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



1887
1888
1889
1890
1891
# File 'lib/puppet/pops/types/types.rb', line 1887

def accept(visitor, guard)
  super
  @size_type.accept(visitor, guard) unless @size_type.nil?
  @types.each { |elem| elem.accept(visitor, guard) }
end

#callable_args?(callable_t, guard) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'lib/puppet/pops/types/types.rb', line 1894

def callable_args?(callable_t, guard)
  unless size_type.nil?
    raise ArgumentError, 'Callable tuple may not have a size constraint when used as args'
  end

  params_tuple = callable_t.param_types
  param_block_t = callable_t.block_type
  arg_types = @types
  arg_block_t = arg_types.last
  if arg_block_t.kind_of_callable?(true, guard)
    # Can't pass a block to a callable that doesn't accept one
    return false if param_block_t.nil?

    # Check that the block is of the right tyṕe
    return false unless param_block_t.assignable?(arg_block_t, guard)

    # Check other arguments
    arg_count = arg_types.size - 1
    params_size_t = params_tuple.size_type || PIntegerType.new(*params_tuple.size_range)
    return false unless params_size_t.assignable?(PIntegerType.new(arg_count, arg_count), guard)

    ctypes = params_tuple.types
    arg_count.times do |index|
      return false unless (ctypes[index] || ctypes[-1]).assignable?(arg_types[index], guard)
    end
    return true
  end

  # Check that tuple is assignable and that the block (if declared) is optional
  params_tuple.assignable?(self, guard) && (param_block_t.nil? || param_block_t.assignable?(PUndefType::DEFAULT, guard))
end

#eachObject

Returns Enumerator for the types if no block is given, otherwise, calls the given block with each of the types in this tuple



1933
1934
1935
1936
1937
1938
1939
# File 'lib/puppet/pops/types/types.rb', line 1933

def each
  if block_given?
    types.each { |x| yield x }
  else
    types.to_enum
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2016
2017
2018
# File 'lib/puppet/pops/types/types.rb', line 2016

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

#generalizeObject



1941
1942
1943
1944
1945
1946
1947
# File 'lib/puppet/pops/types/types.rb', line 1941

def generalize
  if self == DEFAULT
    DEFAULT
  else
    alter_type_array(@types, :generalize) { |altered_types| PTupleType.new(altered_types, @size_type) }
  end
end

#hashObject



2012
2013
2014
# File 'lib/puppet/pops/types/types.rb', line 2012

def hash
  @size_type.hash ^ @types.hash
end

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

Returns:

  • (Boolean)


1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
# File 'lib/puppet/pops/types/types.rb', line 1967

def instance?(o, guard = nil)
  return false unless o.is_a?(Array)
  # compute the tuple's min/max size, and check if that size matches
  size_t = size_type || PIntegerType.new(*size_range)

  return false unless size_t.instance?(o.size, guard)
  o.each_with_index do |element, index|
    return false unless (types[index] || types[-1]).instance?(element, guard)
  end
  true
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1979
1980
1981
# File 'lib/puppet/pops/types/types.rb', line 1979

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1983
1984
1985
# File 'lib/puppet/pops/types/types.rb', line 1983

def iterable_type(guard = nil)
  PIterableType.new(PVariantType.maybe_create(types))
end

#new_function(loader) ⇒ Object



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

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

#normalize(guard = nil) ⇒ Object



1949
1950
1951
1952
1953
1954
1955
# File 'lib/puppet/pops/types/types.rb', line 1949

def normalize(guard = nil)
  if self == DEFAULT
    DEFAULT
  else
    alter_type_array(@types, :normalize, guard) { |altered_types| PTupleType.new(altered_types, @size_type) }
  end
end

#repeat_last_rangeObject

Returns the number of accepted occurrences [min, max] of the last type in the tuple The defaults is [1,1]



2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
# File 'lib/puppet/pops/types/types.rb', line 2000

def repeat_last_range
  if @size_type.nil?
    return [1, 1]
  end
  types_size = @types.size
  from, to = @size_type.range
  min = from - (types_size-1)
  min = min <= 0 ? 0 : min
  max = to - (types_size-1)
  [min, max]
end

#resolve(type_parser, loader) ⇒ Object



1957
1958
1959
1960
1961
1962
1963
1964
1965
# File 'lib/puppet/pops/types/types.rb', line 1957

def resolve(type_parser, loader)
  changed = false
  rtypes = @types.map do |type|
    rtype = type.resolve(type_parser, loader)
    changed ||= !rtype.equal?(type)
    rtype
  end
  changed ? self.class.new(rtypes, @size_type) : self
end

#size_rangeObject

Returns the number of elements accepted [min, max] in the tuple



1988
1989
1990
1991
1992
1993
1994
1995
# File 'lib/puppet/pops/types/types.rb', line 1988

def size_range
  if @size_type.nil?
    types_size = @types.size
    types_size == 0 ? [0, Float::INFINITY] : [types_size, types_size]
  else
    @size_type.range
  end
end