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)

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #kind_of_callable?, #simple_name, #to_alias_expanded_s, #to_s

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



1192
1193
1194
# File 'lib/puppet/pops/types/types.rb', line 1192

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

Instance Method Details

#accept(visitor, guard) ⇒ Object



1196
1197
1198
1199
# File 'lib/puppet/pops/types/types.rb', line 1196

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

#eachObject



1201
1202
1203
1204
1205
1206
1207
# File 'lib/puppet/pops/types/types.rb', line 1201

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

#elementsObject



1254
1255
1256
# File 'lib/puppet/pops/types/types.rb', line 1254

def elements
  @elements
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


1250
1251
1252
# File 'lib/puppet/pops/types/types.rb', line 1250

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

#generalizeObject



1209
1210
1211
1212
1213
1214
1215
# File 'lib/puppet/pops/types/types.rb', line 1209

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

#hashObject



1229
1230
1231
# File 'lib/puppet/pops/types/types.rb', line 1229

def hash
  @elements.hash
end

#hashed_elementsObject



1225
1226
1227
# File 'lib/puppet/pops/types/types.rb', line 1225

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

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/puppet/pops/types/types.rb', line 1258

def instance?(o)
  return false unless o.is_a?(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)
    else
      matched += 1
      e.value_type.instance?(v)
    end
  end && matched == o.size
end

#iterable?(guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


1233
1234
1235
# File 'lib/puppet/pops/types/types.rb', line 1233

def iterable?(guard = nil)
  true
end

#iterable_type(guard = nil) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/puppet/pops/types/types.rb', line 1237

def iterable_type(guard = nil)
  if self == DEFAULT
    PIterableType.new(PHashType::DEFAULT_KEY_PAIR_TUPLE)
  else
    tc = TypeCalculator.singleton
    PIterableType.new(
      PTupleType.new([
        tc.unwrap_single_variant(PVariantType.new(@elements.map {|se| se.key_type })),
        tc.unwrap_single_variant(PVariantType.new(@elements.map {|se| se.value_type }))],
        PHashType::KEY_PAIR_TUPLE_SIZE))
  end
end

#normalize(guard = nil) ⇒ Object



1217
1218
1219
1220
1221
1222
1223
# File 'lib/puppet/pops/types/types.rb', line 1217

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