Class: Puppet::Pops::Types::PCollectionType

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

Direct Known Subclasses

PArrayType, PHashType

Constant Summary collapse

DEFAULT_SIZE =
PIntegerType.new(0)
ZERO_SIZE =
PIntegerType.new(0, 0)
NOT_EMPTY_SIZE =
PIntegerType.new(1)
DEFAULT =
PCollectionType.new(nil)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #iterable_type, #kind_of_callable?, #loader, #name, #new_function, new_function, #really_instance?, #resolve, #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, #to_s

Constructor Details

#initialize(size_type) ⇒ PCollectionType

Returns a new instance of PCollectionType


1321
1322
1323
# File 'lib/puppet/pops/types/types.rb', line 1321

def initialize(size_type)
  @size_type = size_type.nil? ? nil : size_type.to_size
end

Instance Attribute Details

#size_typeObject (readonly)


1319
1320
1321
# File 'lib/puppet/pops/types/types.rb', line 1319

def size_type
  @size_type
end

Class Method Details

.register_ptype(loader, ir) ⇒ Object


1310
1311
1312
1313
1314
1315
1316
1317
# File 'lib/puppet/pops/types/types.rb', line 1310

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

Instance Method Details

#accept(visitor, guard) ⇒ Object


1325
1326
1327
1328
# File 'lib/puppet/pops/types/types.rb', line 1325

def accept(visitor, guard)
  super
  @size_type.accept(visitor, guard) unless @size_type.nil?
end

#eql?(o) ⇒ Boolean


1365
1366
1367
# File 'lib/puppet/pops/types/types.rb', line 1365

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

#generalizeObject


1330
1331
1332
# File 'lib/puppet/pops/types/types.rb', line 1330

def generalize
  DEFAULT
end

#has_empty_range?Boolean


1352
1353
1354
1355
# File 'lib/puppet/pops/types/types.rb', line 1352

def has_empty_range?
  from, to = size_range
  from == 0 && to == 0
end

#hashObject


1357
1358
1359
# File 'lib/puppet/pops/types/types.rb', line 1357

def hash
  @size_type.hash
end

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


1338
1339
1340
1341
1342
1343
1344
1345
# File 'lib/puppet/pops/types/types.rb', line 1338

def instance?(o, guard = nil)
  # The inferred type of a class derived from Array or Hash is either Runtime or Object. It's not assignable to the Collection type.
  if o.instance_of?(Array) || o.instance_of?(Hash)
    @size_type.nil? || @size_type.instance?(o.size)
  else
    false
  end
end

#iterable?(guard = nil) ⇒ Boolean


1361
1362
1363
# File 'lib/puppet/pops/types/types.rb', line 1361

def iterable?(guard = nil)
  true
end

#normalize(guard = nil) ⇒ Object


1334
1335
1336
# File 'lib/puppet/pops/types/types.rb', line 1334

def normalize(guard = nil)
  DEFAULT
end

#size_rangeObject

Returns an array with from (min) size to (max) size


1348
1349
1350
# File 'lib/puppet/pops/types/types.rb', line 1348

def size_range
  (@size_type || DEFAULT_SIZE).range
end