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 included from Enumerable

#uniq

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

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(types, size_type = nil) ⇒ PTupleType

Returns a new instance of PTupleType.



2113
2114
2115
2116
# File 'lib/puppet/pops/types/types.rb', line 2113

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



2070
2071
2072
# File 'lib/puppet/pops/types/types.rb', line 2070

def size_type
  @size_type
end

#typesObject (readonly)



2072
2073
2074
# File 'lib/puppet/pops/types/types.rb', line 2072

def types
  @types
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object



2057
2058
2059
2060
2061
2062
2063
2064
2065
# File 'lib/puppet/pops/types/types.rb', line 2057

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

Instance Method Details

#accept(visitor, guard) ⇒ Object



2074
2075
2076
2077
2078
# File 'lib/puppet/pops/types/types.rb', line 2074

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)


2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
# File 'lib/puppet/pops/types/types.rb', line 2081

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



2120
2121
2122
2123
2124
2125
2126
# File 'lib/puppet/pops/types/types.rb', line 2120

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2205
2206
2207
# File 'lib/puppet/pops/types/types.rb', line 2205

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

#generalizeObject



2128
2129
2130
2131
2132
2133
2134
# File 'lib/puppet/pops/types/types.rb', line 2128

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

#hashObject



2201
2202
2203
# File 'lib/puppet/pops/types/types.rb', line 2201

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

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

Returns:

  • (Boolean)


2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
# File 'lib/puppet/pops/types/types.rb', line 2154

def instance?(o, guard = nil)
  # The inferred type of a class derived from Array is either Runtime or Object. It's not assignable to the Tuple type.
  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

Returns:

  • (Boolean)


2168
2169
2170
# File 'lib/puppet/pops/types/types.rb', line 2168

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



2172
2173
2174
# File 'lib/puppet/pops/types/types.rb', line 2172

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

#new_functionObject



2209
2210
2211
2212
2213
# File 'lib/puppet/pops/types/types.rb', line 2209

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

#normalize(guard = nil) ⇒ Object



2136
2137
2138
2139
2140
2141
2142
# File 'lib/puppet/pops/types/types.rb', line 2136

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]



2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
# File 'lib/puppet/pops/types/types.rb', line 2189

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



2144
2145
2146
2147
2148
2149
2150
2151
2152
# File 'lib/puppet/pops/types/types.rb', line 2144

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_rangeObject

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



2177
2178
2179
2180
2181
2182
2183
2184
# File 'lib/puppet/pops/types/types.rb', line 2177

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