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
-
#accept(visitor, guard) ⇒ Object
-
#callable_args?(callable_t, guard) ⇒ Boolean
private
-
#each ⇒ Object
Returns Enumerator for the types if no block is given, otherwise, calls the given block with each of the types in this tuple.
-
#eql?(o) ⇒ Boolean
-
#generalize ⇒ Object
-
#hash ⇒ Object
-
#initialize(types, size_type = nil) ⇒ PTupleType
constructor
A new instance of PTupleType.
-
#instance?(o, guard = nil) ⇒ Boolean
-
#iterable?(guard = nil) ⇒ Boolean
-
#iterable_type(guard = nil) ⇒ Object
-
#new_function(loader) ⇒ Object
-
#normalize(guard = nil) ⇒ Object
-
#repeat_last_range ⇒ Object
Returns the number of accepted occurrences [min, max] of the last type in the tuple The defaults is [1,1].
-
#resolve(type_parser, loader) ⇒ Object
-
#size_range ⇒ Object
Returns the number of elements accepted [min, max] in the tuple.
Methods inherited from PAnyType
#==, #assignable?, #callable?, #callable_with?, #check_self_recursion, #create, #kind_of_callable?, #name, new_function, #really_instance?, #simple_name, simple_name, #to_alias_expanded_s, #to_s
_ptype, create_ptype, register_ptypes
#_ptype
Constructor Details
#initialize(types, size_type = nil) ⇒ PTupleType
Returns a new instance of PTupleType.
2015
2016
2017
2018
|
# File 'lib/puppet/pops/types/types.rb', line 2015
def initialize(types, size_type = nil)
@types = types
@size_type = size_type.nil? ? nil : size_type.to_size
end
|
Instance Attribute Details
#size_type ⇒ Object
If set, describes min and max required of the given types - if max > size of types, the last type entry repeats
1972
1973
1974
|
# File 'lib/puppet/pops/types/types.rb', line 1972
def size_type
@size_type
end
|
1974
1975
1976
|
# File 'lib/puppet/pops/types/types.rb', line 1974
def types
@types
end
|
Class Method Details
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
1976
1977
1978
1979
1980
|
# File 'lib/puppet/pops/types/types.rb', line 1976
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.
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
|
# File 'lib/puppet/pops/types/types.rb', line 1983
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)
return false if param_block_t.nil?
return false unless param_block_t.assignable?(arg_block_t, guard)
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
params_tuple.assignable?(self, guard) && (param_block_t.nil? || param_block_t.assignable?(PUndefType::DEFAULT, guard))
end
|
Returns Enumerator for the types if no block is given, otherwise, calls the given block with each of the types in this tuple
2022
2023
2024
2025
2026
2027
2028
|
# File 'lib/puppet/pops/types/types.rb', line 2022
def each
if block_given?
types.each { |x| yield x }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
2106
2107
2108
|
# File 'lib/puppet/pops/types/types.rb', line 2106
def eql?(o)
self.class == o.class && @types == o.types && @size_type == o.size_type
end
|
#generalize ⇒ Object
2030
2031
2032
2033
2034
2035
2036
|
# File 'lib/puppet/pops/types/types.rb', line 2030
def generalize
if self == DEFAULT
DEFAULT
else
alter_type_array(@types, :generalize) { |altered_types| PTupleType.new(altered_types, @size_type) }
end
end
|
2102
2103
2104
|
# File 'lib/puppet/pops/types/types.rb', line 2102
def hash
@size_type.hash ^ @types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
|
# File 'lib/puppet/pops/types/types.rb', line 2056
def instance?(o, guard = nil)
return false unless o.is_a?(Array)
if @size_type
return false unless @size_type.instance?(o.size, guard)
else
return false unless @types.empty? || @types.size == o.size
end
index = -1
@types.empty? || o.all? do |element|
@types.fetch(index += 1) { @types.last }.instance?(element, guard)
end
end
|
#iterable?(guard = nil) ⇒ Boolean
2069
2070
2071
|
# File 'lib/puppet/pops/types/types.rb', line 2069
def iterable?(guard = nil)
true
end
|
#iterable_type(guard = nil) ⇒ Object
#new_function(loader) ⇒ Object
2110
2111
2112
2113
2114
|
# File 'lib/puppet/pops/types/types.rb', line 2110
def new_function(loader)
PArrayType.new_function(self, loader)
end
|
#normalize(guard = nil) ⇒ Object
2038
2039
2040
2041
2042
2043
2044
|
# File 'lib/puppet/pops/types/types.rb', line 2038
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_range ⇒ Object
Returns the number of accepted occurrences [min, max] of the last type in the tuple The defaults is [1,1]
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
|
# File 'lib/puppet/pops/types/types.rb', line 2090
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
2046
2047
2048
2049
2050
2051
2052
2053
2054
|
# File 'lib/puppet/pops/types/types.rb', line 2046
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_range ⇒ Object
Returns the number of elements accepted [min, max] in the tuple
2078
2079
2080
2081
2082
2083
2084
2085
|
# File 'lib/puppet/pops/types/types.rb', line 2078
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
|