Class: Puppet::Pops::Types::PTupleType
- Inherits:
-
PAnyType
show all
- Includes:
- Enumerable
- Defined in:
- lib/puppet/pops/types/types.rb
Constant Summary
collapse
- DEFAULT =
PTupleType.new(EMPTY_ARRAY)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from PAnyType
#==, #assignable?, #callable?, #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
_pcore_type, create_ptype, register_ptypes
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s
Constructor Details
#initialize(types, size_type = nil) ⇒ PTupleType
Returns a new instance of PTupleType.
2202
2203
2204
2205
|
# File 'lib/puppet/pops/types/types.rb', line 2202
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
2159
2160
2161
|
# File 'lib/puppet/pops/types/types.rb', line 2159
def size_type
@size_type
end
|
2161
2162
2163
|
# File 'lib/puppet/pops/types/types.rb', line 2161
def types
@types
end
|
Class Method Details
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
2163
2164
2165
2166
2167
|
# File 'lib/puppet/pops/types/types.rb', line 2163
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.
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
|
# File 'lib/puppet/pops/types/types.rb', line 2170
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
2209
2210
2211
2212
2213
2214
2215
|
# File 'lib/puppet/pops/types/types.rb', line 2209
def each
if block_given?
types.each { |x| yield x }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
2294
2295
2296
|
# File 'lib/puppet/pops/types/types.rb', line 2294
def eql?(o)
self.class == o.class && @types == o.types && @size_type == o.size_type
end
|
#generalize ⇒ Object
2217
2218
2219
2220
2221
2222
2223
|
# File 'lib/puppet/pops/types/types.rb', line 2217
def generalize
if self == DEFAULT
DEFAULT
else
alter_type_array(@types, :generalize) { |altered_types| PTupleType.new(altered_types, @size_type) }
end
end
|
2290
2291
2292
|
# File 'lib/puppet/pops/types/types.rb', line 2290
def hash
@size_type.hash ^ @types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
|
# File 'lib/puppet/pops/types/types.rb', line 2243
def instance?(o, guard = nil)
return false unless o.instance_of?(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
2257
2258
2259
|
# File 'lib/puppet/pops/types/types.rb', line 2257
def iterable?(guard = nil)
true
end
|
#iterable_type(guard = nil) ⇒ Object
#new_function ⇒ Object
2298
2299
2300
2301
2302
|
# File 'lib/puppet/pops/types/types.rb', line 2298
def new_function
PArrayType.new_function(self)
end
|
#normalize(guard = nil) ⇒ Object
2225
2226
2227
2228
2229
2230
2231
|
# File 'lib/puppet/pops/types/types.rb', line 2225
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]
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
|
# File 'lib/puppet/pops/types/types.rb', line 2278
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(loader) ⇒ Object
2233
2234
2235
2236
2237
2238
2239
2240
2241
|
# File 'lib/puppet/pops/types/types.rb', line 2233
def resolve(loader)
changed = false
rtypes = @types.map do |type|
rtype = type.resolve(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
2266
2267
2268
2269
2270
2271
2272
2273
|
# File 'lib/puppet/pops/types/types.rb', line 2266
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
|