Class: Puppet::Pops::Types::TypeCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/type_calculator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeCalculator

Returns a new instance of TypeCalculator.



170
171
172
173
# File 'lib/puppet/pops/types/type_calculator.rb', line 170

def initialize
  @@infer_visitor ||= Visitor.new(nil, 'infer',0,0)
  @@extract_visitor ||= Visitor.new(nil, 'extract',0,0)
end

Class Method Details

.assignable?(t1, t2) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/puppet/pops/types/type_calculator.rb', line 104

def self.assignable?(t1, t2)
  singleton.assignable?(t1,t2)
end

.callable?(callable, args) ⇒ Boolan

Answers, does the given callable accept the arguments given in args (an array or a tuple)

Parameters:

Returns:

  • (Boolan)

    true if the callable accepts the arguments



114
115
116
# File 'lib/puppet/pops/types/type_calculator.rb', line 114

def self.callable?(callable, args)
  singleton.callable?(callable, args)
end

.debug_string(t) ⇒ Object



145
146
147
148
# File 'lib/puppet/pops/types/type_calculator.rb', line 145

def self.debug_string(t)
  Puppet.deprecation_warning('TypeCalculator.debug_string is deprecated. Use TypeFormatter')
  TypeFormatter.debug_string(t)
end

.enumerable(t) ⇒ Object



151
152
153
154
# File 'lib/puppet/pops/types/type_calculator.rb', line 151

def self.enumerable(t)
  Puppet.deprecation_warning('TypeCalculator.enumerable is deprecated. Use iterable')
  singleton.iterable(t)
end

.generalize(o) ⇒ Object



135
136
137
# File 'lib/puppet/pops/types/type_calculator.rb', line 135

def self.generalize(o)
  singleton.generalize(o)
end

.infer(o) ⇒ Object



130
131
132
# File 'lib/puppet/pops/types/type_calculator.rb', line 130

def self.infer(o)
  singleton.infer(o)
end

.infer_set(o) ⇒ Object



140
141
142
# File 'lib/puppet/pops/types/type_calculator.rb', line 140

def self.infer_set(o)
  singleton.infer_set(o)
end

.instance?(t, o) ⇒ Boolean

Answers ‘is o an instance of type t’

Returns:

  • (Boolean)


323
324
325
# File 'lib/puppet/pops/types/type_calculator.rb', line 323

def self.instance?(t, o)
  singleton.instance?(t,o)
end

.is_kind_of_callable?(t, optional = true) ⇒ 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)


731
732
733
# File 'lib/puppet/pops/types/type_calculator.rb', line 731

def self.is_kind_of_callable?(t, optional = true)
  t.is_a?(PAnyType) && t.kind_of_callable?(optional)
end

.iterable(t) ⇒ Object



157
158
159
# File 'lib/puppet/pops/types/type_calculator.rb', line 157

def self.iterable(t)
  singleton.iterable(t)
end

.singletonTypeCalculator

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 the singleton instance.

Returns:



164
165
166
# File 'lib/puppet/pops/types/type_calculator.rb', line 164

def self.singleton
  @tc_instance ||= new
end

.string(t) ⇒ String

Produces a String representation of the given type.

Parameters:

  • t (PAnyType)

    the type to produce a string form

Returns:

  • (String)

    the type in string form



124
125
126
127
# File 'lib/puppet/pops/types/type_calculator.rb', line 124

def self.string(t)
  Puppet.deprecation_warning('TypeCalculator.string is deprecated. Use TypeFormatter')
  TypeFormatter.string(t)
end

Instance Method Details

#assignable?(t, t2) ⇒ Boolean

Answers ‘can an instance of type t2 be assigned to a variable of type t’. Does not accept nil/undef unless the type accepts it.

Returns:

  • (Boolean)


203
204
205
206
207
208
# File 'lib/puppet/pops/types/type_calculator.rb', line 203

def assignable?(t, t2)
   if t.is_a?(Module)
     t = type(t)
   end
   t.is_a?(PAnyType) ? t.assignable?(t2) : false
end

#callable?(callable, args) ⇒ Boolean

Answers, does the given callable accept the arguments given in args (an array or a tuple)

Returns:

  • (Boolean)


224
225
226
# File 'lib/puppet/pops/types/type_calculator.rb', line 224

def callable?(callable, args)
  callable.is_a?(PAnyType) && callable.callable?(args)
end

#common_type(t1, t2) ⇒ Object

Answers, ‘What is the common type of t1 and t2?’

TODO: The current implementation should be optimized for performance

Raises:

  • (ArgumentError)


357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/puppet/pops/types/type_calculator.rb', line 357

def common_type(t1, t2)
  raise ArgumentError, 'two types expected' unless (is_ptype?(t1) || is_pnil?(t1)) && (is_ptype?(t2) || is_pnil?(t2))

  # TODO: This is not right since Scalar U Undef is Any
  # if either is nil, the common type is the other
  if is_pnil?(t1)
    return t2
  elsif is_pnil?(t2)
    return t1
  end

  # If either side is Unit, it is the other type
  if t1.is_a?(PUnitType)
    return t2
  elsif t2.is_a?(PUnitType)
    return t1
  end

  # Simple case, one is assignable to the other
  if assignable?(t1, t2)
    return t1
  elsif assignable?(t2, t1)
    return t2
  end

  # when both are arrays, return an array with common element type
  if t1.is_a?(PArrayType) && t2.is_a?(PArrayType)
    return PArrayType.new(common_type(t1.element_type, t2.element_type))
  end

  # when both are hashes, return a hash with common key- and element type
  if t1.is_a?(PHashType) && t2.is_a?(PHashType)
    key_type = common_type(t1.key_type, t2.key_type)
    element_type = common_type(t1.element_type, t2.element_type)
    return PHashType.new(key_type, element_type)
  end

  # when both are host-classes, reduce to PHostClass[] (since one was not assignable to the other)
  if t1.is_a?(PHostClassType) && t2.is_a?(PHostClassType)
    return PHostClassType::DEFAULT
  end

  # when both are resources, reduce to Resource[T] or Resource[] (since one was not assignable to the other)
  if t1.is_a?(PResourceType) && t2.is_a?(PResourceType)
    # only Resource[] unless the type name is the same
    return t1.type_name == t2.type_name ?  PResourceType.new(t1.type_name, nil) : PResourceType::DEFAULT
  end

  # Integers have range, expand the range to the common range
  if t1.is_a?(PIntegerType) && t2.is_a?(PIntegerType)
    return PIntegerType.new([t1.numeric_from, t2.numeric_from].min, [t1.numeric_to, t2.numeric_to].max)
  end

  # Floats have range, expand the range to the common range
  if t1.is_a?(PFloatType) && t2.is_a?(PFloatType)
    return PFloatType.new([t1.numeric_from, t2.numeric_from].min, [t1.numeric_to, t2.numeric_to].max)
  end

  if t1.is_a?(PStringType) && t2.is_a?(PStringType)
    common_size_type = common_type(t1.size_type, t2.size_type) unless t1.size_type.nil? || t2.size_type.nil?
    common_strings = t1.values.empty? || t2.values.empty? ? [] : t1.values | t2.values
    return PStringType.new(common_size_type, common_strings)
  end

  if t1.is_a?(PPatternType) && t2.is_a?(PPatternType)
    return PPatternType.new(t1.patterns | t2.patterns)
  end

  if t1.is_a?(PEnumType) && t2.is_a?(PEnumType)
    # The common type is one that complies with either set
    return PEnumType.new(t1.values | t2.values)
  end

  if t1.is_a?(PVariantType) && t2.is_a?(PVariantType)
    # The common type is one that complies with either set
    return PVariantType.new(t1.types | t2.types)
  end

  if t1.is_a?(PRegexpType) && t2.is_a?(PRegexpType)
    # if they were identical, the general rule would return a parameterized regexp
    # since they were not, the result is a generic regexp type
    return PPatternType::DEFAULT
  end

  if t1.is_a?(PCallableType) && t2.is_a?(PCallableType)
    # They do not have the same signature, and one is not assignable to the other,
    # what remains is the most general form of Callable
    return PCallableType::DEFAULT
  end

  # Common abstract types, from most specific to most general
  if common_numeric?(t1, t2)
    return PNumericType::DEFAULT
  end

  if common_scalar?(t1, t2)
    return PScalarType::DEFAULT
  end

  if common_data?(t1,t2)
    return PDataType::DEFAULT
  end

  # Meta types Type[Integer] + Type[String] => Type[Data]
  if t1.is_a?(PType) && t2.is_a?(PType)
    return PType.new(common_type(t1.type, t2.type))
  end

  # If both are Runtime types
  if t1.is_a?(PRuntimeType) && t2.is_a?(PRuntimeType)
    if t1.runtime == t2.runtime && t1.runtime_type_name == t2.runtime_type_name
      return t1
    end
    # finding the common super class requires that names are resolved to class
    # NOTE: This only supports runtime type of :ruby
    c1 = ClassLoader.provide_from_type(t1)
    c2 = ClassLoader.provide_from_type(t2)
    if c1 && c2
      c2_superclasses = superclasses(c2)
      superclasses(c1).each do|c1_super|
        c2_superclasses.each do |c2_super|
          if c1_super == c2_super
            return PRuntimeType.new(:ruby, c1_super.name)
          end
        end
      end
    end
  end

  # They better both be Any type, or the wrong thing was asked and nil is returned
  t1.is_a?(PAnyType) && t2.is_a?(PAnyType) ? PAnyType::DEFAULT : nil
end

#debug_string(t) ⇒ Object

Produces a debug string representing the type (possibly with more information that the regular string format)



511
512
513
514
# File 'lib/puppet/pops/types/type_calculator.rb', line 511

def debug_string(t)
  Puppet.deprecation_warning('TypeCalculator.debug_string is deprecated. Use TypeFormatter')
  TypeFormatter.singleton.debug_string(t)
end

#enumerable(t) ⇒ Object

Returns an iterable if the t represents something that can be iterated



211
212
213
214
# File 'lib/puppet/pops/types/type_calculator.rb', line 211

def enumerable(t)
  Puppet.deprecation_warning('TypeCalculator.enumerable is deprecated. Use iterable')
  iterable(t)
end

#equals(left, right) ⇒ Object

Answers if the two given types describe the same type



229
230
231
232
233
234
235
# File 'lib/puppet/pops/types/type_calculator.rb', line 229

def equals(left, right)
  return false unless left.is_a?(PAnyType) && right.is_a?(PAnyType)
  # Types compare per class only - an extra test must be made if the are mutually assignable
  # to find all types that represent the same type of instance
  #
  left == right || (assignable?(right, left) && assignable?(left, right))
end

#generalize(o) ⇒ Object

Generalizes value specific types. The generalized type is returned.



276
277
278
# File 'lib/puppet/pops/types/type_calculator.rb', line 276

def generalize(o)
  o.is_a?(PAnyType) ? o.generalize : o
end

#infer(o) ⇒ Object

Answers ‘what is the single common Puppet Type describing o’, or if o is an Array or Hash, what is the single common type of the elements (or keys and elements for a Hash).



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/puppet/pops/types/type_calculator.rb', line 284

def infer(o)
  # Optimize the most common cases into direct calls.
  case o
  when String
    infer_String(o)
  when Integer
    infer_Integer(o)
  when Array
    infer_Array(o)
  when Hash
    infer_Hash(o)
  when Evaluator::PuppetProc
    infer_PuppetProc(o)
  else
    @@infer_visitor.visit_this_0(self, o)
  end
end

#infer_and_reduce_type(enumerable) ⇒ Object

Reduce an enumerable of objects to a single common type



527
528
529
# File 'lib/puppet/pops/types/type_calculator.rb', line 527

def infer_and_reduce_type(enumerable)
  reduce_type(enumerable.map {|o| infer(o) })
end

#infer_Array(o) ⇒ Object

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.



659
660
661
662
663
664
665
# File 'lib/puppet/pops/types/type_calculator.rb', line 659

def infer_Array(o)
  if o.empty?
    PArrayType::EMPTY
  else
    PArrayType.new(infer_and_reduce_type(o), size_as_type(o))
  end
end

#infer_Closure(o) ⇒ Object

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.



539
540
541
# File 'lib/puppet/pops/types/type_calculator.rb', line 539

def infer_Closure(o)
  o.type
end

#infer_FalseClass(o) ⇒ Object

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.



643
644
645
# File 'lib/puppet/pops/types/type_calculator.rb', line 643

def infer_FalseClass(o)
  PBooleanType::DEFAULT
end

#infer_Float(o) ⇒ Object

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.



579
580
581
# File 'lib/puppet/pops/types/type_calculator.rb', line 579

def infer_Float(o)
  PFloatType.new(o, o)
end

#infer_Function(o) ⇒ Object

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.



549
550
551
# File 'lib/puppet/pops/types/type_calculator.rb', line 549

def infer_Function(o)
  o.class.dispatcher.to_type
end

#infer_generic(o) ⇒ Object



302
303
304
# File 'lib/puppet/pops/types/type_calculator.rb', line 302

def infer_generic(o)
  generalize(infer(o))
end

#infer_Hash(o) ⇒ Object

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.



668
669
670
671
672
673
674
675
676
# File 'lib/puppet/pops/types/type_calculator.rb', line 668

def infer_Hash(o)
  if o.empty?
    PHashType::EMPTY
  else
    ktype = infer_and_reduce_type(o.keys)
    etype = infer_and_reduce_type(o.values)
    PHashType.new(ktype, etype, size_as_type(o))
  end
end

#infer_Integer(o) ⇒ Object

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.



584
585
586
# File 'lib/puppet/pops/types/type_calculator.rb', line 584

def infer_Integer(o)
  PIntegerType.new(o, o)
end

#infer_Iterator(o) ⇒ Object

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.



544
545
546
# File 'lib/puppet/pops/types/type_calculator.rb', line 544

def infer_Iterator(o)
  PIteratorType.new(o.element_type)
end

#infer_Module(o) ⇒ Object

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.

The type of all modules is PType



534
535
536
# File 'lib/puppet/pops/types/type_calculator.rb', line 534

def infer_Module(o)
  PType::new(PRuntimeType.new(:ruby, o.name))
end

#infer_NilClass(o) ⇒ Object

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.



594
595
596
# File 'lib/puppet/pops/types/type_calculator.rb', line 594

def infer_NilClass(o)
  PUndefType::DEFAULT
end

#infer_Object(o) ⇒ Object

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.



554
555
556
# File 'lib/puppet/pops/types/type_calculator.rb', line 554

def infer_Object(o)
  PRuntimeType.new(:ruby, o.class.name)
end

#infer_PAnyType(o) ⇒ Object

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.

The type of all types is PType



561
562
563
# File 'lib/puppet/pops/types/type_calculator.rb', line 561

def infer_PAnyType(o)
  PType.new(o)
end

#infer_Proc(o) ⇒ Object

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.

Parameters:

  • o (Proc)


600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/puppet/pops/types/type_calculator.rb', line 600

def infer_Proc(o)
  min = 0
  max = 0
  mapped_types = o.parameters.map do |p|
    case p[0]
    when :rest
      max = :default
      break PAnyType::DEFAULT
    when :req
      min += 1
    end
    max += 1
    PAnyType::DEFAULT
  end
  mapped_types << min
  mapped_types << max
  TypeFactory.callable(*mapped_types)
end

#infer_PType(o) ⇒ Object

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.

The type of all types is PType This is the metatype short circuit.



569
570
571
# File 'lib/puppet/pops/types/type_calculator.rb', line 569

def infer_PType(o)
  PType.new(o)
end

#infer_PuppetProc(o) ⇒ Object

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.



620
621
622
# File 'lib/puppet/pops/types/type_calculator.rb', line 620

def infer_PuppetProc(o)
  infer_Closure(o.closure)
end

#infer_Regexp(o) ⇒ Object

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.



589
590
591
# File 'lib/puppet/pops/types/type_calculator.rb', line 589

def infer_Regexp(o)
  PRegexpType.new(o.source)
end

#infer_Resource(o) ⇒ Object

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.

A Puppet::Parser::Resource, or Puppet::Resource



650
651
652
653
654
655
656
# File 'lib/puppet/pops/types/type_calculator.rb', line 650

def infer_Resource(o)
  # Only Puppet::Resource can have a title that is a symbol :undef, a PResource cannot.
  # A mapping must be made to empty string. A nil value will result in an error later
  title = o.title
  title = '' if :undef == title
  PType.new(PResourceType.new(o.type.to_s.downcase, title))
end

#infer_set(o) ⇒ Object

Answers ‘what is the set of Puppet Types of o’



309
310
311
312
313
314
315
316
317
318
# File 'lib/puppet/pops/types/type_calculator.rb', line 309

def infer_set(o)
  case o
    when Array
      infer_set_Array(o)
    when Hash
      infer_set_Hash(o)
    else
      infer_set_Object(o)
  end
end

#infer_set_Array(o) ⇒ Object



688
689
690
691
692
693
694
# File 'lib/puppet/pops/types/type_calculator.rb', line 688

def infer_set_Array(o)
  if o.empty?
    PArrayType::EMPTY
  else
    PTupleType.new(o.map {|x| infer_set(x) })
  end
end

#infer_set_Hash(o) ⇒ Object



696
697
698
699
700
701
702
703
704
705
706
# File 'lib/puppet/pops/types/type_calculator.rb', line 696

def infer_set_Hash(o)
  if o.empty?
    PHashType::EMPTY
  elsif o.keys.all? {|k| PStringType::NON_EMPTY.instance?(k) }
    PStructType.new(o.each_pair.map { |k,v| PStructElement.new(PStringType.new(nil, [k]), infer_set(v)) })
  else
    ktype = PVariantType.new(o.keys.map {|k| infer_set(k) })
    etype = PVariantType.new(o.values.map {|e| infer_set(e) })
    PHashType.new(unwrap_single_variant(ktype), unwrap_single_variant(etype), size_as_type(o))
  end
end

#infer_set_Object(o) ⇒ Object

Common case for everything that intrinsically only has a single type



684
685
686
# File 'lib/puppet/pops/types/type_calculator.rb', line 684

def infer_set_Object(o)
  infer(o)
end

#infer_String(o) ⇒ Object

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.



574
575
576
# File 'lib/puppet/pops/types/type_calculator.rb', line 574

def infer_String(o)
  PStringType.new(size_as_type(o), [o])
end

#infer_Symbol(o) ⇒ Object

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.

Inference of :default as PDefaultType, and all other are Ruby



626
627
628
629
630
631
632
633
634
635
# File 'lib/puppet/pops/types/type_calculator.rb', line 626

def infer_Symbol(o)
  case o
  when :default
    PDefaultType::DEFAULT
  when :undef
    PUndefType::DEFAULT
  else
    infer_Object(o)
  end
end

#infer_TrueClass(o) ⇒ Object

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.



638
639
640
# File 'lib/puppet/pops/types/type_calculator.rb', line 638

def infer_TrueClass(o)
  PBooleanType::DEFAULT
end

#injectable_class(klazz) ⇒ Class?

Answers the question ‘is it possible to inject an instance of the given class’ A class is injectable if it has a special *assisted inject* class method called ‘inject` taking an injector and a scope as argument, or if it has a zero args `initialize` method.

Parameters:

Returns:

  • (Class, nil)

    the injectable Class, or nil if not injectable



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/puppet/pops/types/type_calculator.rb', line 183

def injectable_class(klazz)
  # Handle case when we get a PType instead of a class
  if klazz.is_a?(PRuntimeType)
    klazz = ClassLoader.provide(klazz)
  end

  # data types can not be injected (check again, it is not safe to assume that given RubyRuntime klazz arg was ok)
  return false unless type(klazz).is_a?(PRuntimeType)
  if (klazz.respond_to?(:inject) && klazz.method(:inject).arity == -4) || klazz.instance_method(:initialize).arity == 0
    klazz
  else
    nil
  end
end

#instance?(t, o) ⇒ Boolean

Answers ‘is o an instance of type t’

Returns:

  • (Boolean)


330
331
332
333
334
335
# File 'lib/puppet/pops/types/type_calculator.rb', line 330

def instance?(t, o)
  if t.is_a?(Module)
    t = type(t)
  end
  t.is_a?(PAnyType) ? t.instance?(o) : false
end

#is_pnil?(t) ⇒ Boolean

Answers if t represents the puppet type PUndefType

Returns:

  • (Boolean)


347
348
349
# File 'lib/puppet/pops/types/type_calculator.rb', line 347

def is_pnil?(t)
  t.nil? || t.is_a?(PUndefType)
end

#is_ptype?(t) ⇒ Boolean

Answers if t is a puppet type

Returns:

  • (Boolean)


340
341
342
# File 'lib/puppet/pops/types/type_calculator.rb', line 340

def is_ptype?(t)
  t.is_a?(PAnyType)
end

#iterable(t) ⇒ Object

Returns an iterable if the t represents something that can be iterated



217
218
219
220
# File 'lib/puppet/pops/types/type_calculator.rb', line 217

def iterable(t)
  # Create an iterable on the type if possible
  Iterable.on(t)
end

#max(a, b) ⇒ Object



735
736
737
# File 'lib/puppet/pops/types/type_calculator.rb', line 735

def max(a,b)
  a >=b ? a : b
end

#min(a, b) ⇒ Object



739
740
741
# File 'lib/puppet/pops/types/type_calculator.rb', line 739

def min(a,b)
  a <= b ? a : b
end

#reduce_type(enumerable) ⇒ Object

Reduces an enumerable of types to a single common type.



520
521
522
# File 'lib/puppet/pops/types/type_calculator.rb', line 520

def reduce_type(enumerable)
  enumerable.reduce(nil) {|memo, t| common_type(memo, t) }
end

#size_as_type(collection) ⇒ Object



678
679
680
681
# File 'lib/puppet/pops/types/type_calculator.rb', line 678

def size_as_type(collection)
  size = collection.size
  PIntegerType.new(size, size)
end

#size_range(range) ⇒ Object

Transform int range to a size constraint if range == nil the constraint is 1,1 if range.from == nil min size = 1 if range.to == nil max size == Infinity



721
722
723
724
725
726
727
728
# File 'lib/puppet/pops/types/type_calculator.rb', line 721

def size_range(range)
  return [1,1] if range.nil?
  from = range.from
  to = range.to
  x = from.nil? ? 1 : from
  y = to.nil? ? TheInfinity : to
  [x, y]
end

#string(t) ⇒ Object

Produces a string representing the type



503
504
505
506
# File 'lib/puppet/pops/types/type_calculator.rb', line 503

def string(t)
  Puppet.deprecation_warning('TypeCalculator.string is deprecated. Use TypeFormatter')
  TypeFormatter.singleton.string(t)
end

#superclasses(c) ⇒ Object

Produces the superclasses of the given class, including the class



491
492
493
494
495
496
497
498
# File 'lib/puppet/pops/types/type_calculator.rb', line 491

def superclasses(c)
  result = [c]
  while s = c.superclass
    result << s
    c = s
  end
  result
end

#to_sObject

Debugging to_s to reduce the amount of output



763
764
765
# File 'lib/puppet/pops/types/type_calculator.rb', line 763

def to_s
  '[a TypeCalculator]'
end

#tuple_entry_at(tuple_t, from, to, index) ⇒ Object

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.

Produces the tuple entry at the given index given a tuple type, its from/to constraints on the last type, and an index. Produces nil if the index is out of bounds from must be less than to, and from may not be less than 0



750
751
752
753
754
755
756
757
758
759
760
# File 'lib/puppet/pops/types/type_calculator.rb', line 750

def tuple_entry_at(tuple_t, from, to, index)
  regular = (tuple_t.types.size - 1)
  if index < regular
    tuple_t.types[index]
  elsif index < regular + to
    # in the varargs part
    tuple_t.types[-1]
  else
    nil
  end
end

#type(c) ⇒ Object

Answers ‘what is the Puppet Type corresponding to the given Ruby class’

Parameters:

  • c (Module)

    the class for which a puppet type is wanted

Raises:

  • (ArgumentError)


241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/puppet/pops/types/type_calculator.rb', line 241

def type(c)
  raise ArgumentError, 'Argument must be a Module' unless c.is_a? Module

  # Can't use a visitor here since we don't have an instance of the class
  case
  when c <= Integer
    type = PIntegerType::DEFAULT
  when c == Float
    type = PFloatType::DEFAULT
  when c == Numeric
    type = PNumericType::DEFAULT
  when c == String
    type = PStringType::DEFAULT
  when c == Regexp
    type = PRegexpType::DEFAULT
  when c == NilClass
    type = PUndefType::DEFAULT
  when c == FalseClass, c == TrueClass
    type = PBooleanType::DEFAULT
  when c == Class
    type = PType::DEFAULT
  when c == Array
    # Assume array of data values
    type = PArrayType::DATA
  when c == Hash
    # Assume hash with scalar keys and data values
    type = PHashType::DATA
 else
    type = PRuntimeType.new(:ruby, c.name)
  end
  type
end

#unwrap_single_variant(possible_variant) ⇒ Object



708
709
710
711
712
713
714
# File 'lib/puppet/pops/types/type_calculator.rb', line 708

def unwrap_single_variant(possible_variant)
  if possible_variant.is_a?(PVariantType) && possible_variant.types.size == 1
    possible_variant.types[0]
  else
    possible_variant
  end
end