Class: Puppet::Pops::Types::PHashType

Inherits:
PCollectionType show all
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PHashType.new(nil, nil)
DATA =
PHashType.new(PScalarType::DEFAULT, PDataType::DEFAULT, DEFAULT_SIZE)
EMPTY =
PHashType.new(PUndefType::DEFAULT, PUndefType::DEFAULT, PIntegerType.new(0, 0))

Constants inherited from PCollectionType

Puppet::Pops::Types::PCollectionType::DEFAULT_SIZE, Puppet::Pops::Types::PCollectionType::ZERO_SIZE

Instance Attribute Summary collapse

Attributes inherited from PCollectionType

#element_type, #size_type

Instance Method Summary collapse

Methods inherited from PCollectionType

#size_range

Methods inherited from PAnyType

#assignable?, #callable?, #callable_args?, #enumerable?, #kind_of_callable?, #simple_name, #to_s

Methods included from Visitable

#accept

Constructor Details

#initialize(key_type, value_type, size_type = nil) ⇒ PHashType

Returns a new instance of PHashType.



1238
1239
1240
1241
# File 'lib/puppet/pops/types/types.rb', line 1238

def initialize(key_type, value_type, size_type = nil)
  super(value_type, size_type)
  @key_type = key_type
end

Instance Attribute Details

#key_typeObject



1236
1237
1238
# File 'lib/puppet/pops/types/types.rb', line 1236

def key_type
  @key_type
end

Instance Method Details

#==(o) ⇒ Object



1272
1273
1274
# File 'lib/puppet/pops/types/types.rb', line 1272

def ==(o)
  super && @key_type == o.key_type
end

#generalizeObject



1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
# File 'lib/puppet/pops/types/types.rb', line 1243

def generalize
  if self == DEFAULT || self == EMPTY
    self
  else
    key_t = @key_type
    key_t = key_t.generalize unless key_t.nil?
    value_t = element_type
    value_t = value_t.generalize unless value_t.nil?
    PHashType.new(key_t, value_t)
  end
end

#hashObject



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

def hash
  @key_type.hash * 31 + super
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

def instance?(o)
  return false unless o.is_a?(Hash)
  key_t = key_type
  element_t = element_type
  if (key_t.nil? || o.keys.all? {|key| key_t.instance?(key) }) &&
      (element_t.nil? || o.values.all? {|value| element_t.instance?(value) })
    size_t = size_type
    size_t.nil? || size_t.instance?(o.size)
  else
    false
  end
end

#is_the_empty_hash?Boolean

Returns:

  • (Boolean)


1276
1277
1278
# File 'lib/puppet/pops/types/types.rb', line 1276

def is_the_empty_hash?
  self == EMPTY
end