Class: Puppet::Pops::Types::PAnyType

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

Overview

Base type for all types

Constant Summary collapse

DEFAULT =

The default instance of this type. Each type in the type system has this constant declared.

PAnyType.new

Instance Method Summary collapse

Methods included from Visitable

#accept

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



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

def ==(o)
  self.class == o.class
end

#assignable?(o) ⇒ Boolean

Checks if o is a type that is assignable to this type. If o is a Class then it is first converted to a type. If o is a Variant, then it is considered assignable when all its types are assignable

Returns:

  • (Boolean) —

    true when o is assignable to this type



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/pops/types/types.rb', line 34

def assignable?(o)
  case o
    when Class
    # Safe to call _assignable directly since a Class never is a Unit or Variant
    _assignable?(Puppet::Pops::Types::TypeCalculator.singleton.type(o))
  when PUnitType
    true
  when PVariantType
    # Assignable if all contained types are assignable
    o.types.all? { |vt| assignable?(vt) }
  when PNotUndefType
    if !(o.type.nil? || o.type.assignable?(PUndefType::DEFAULT))
      assignable?(o.type)
    else
      _assignable?(o)
    end
  else
    _assignable?(o)
  end
end

#callable?(args) ⇒ Boolean

Returns true if this instance is a callable that accepts the given args

Returns:

  • (Boolean)


58
59
60
# File 'lib/puppet/pops/types/types.rb', line 58

def callable?(args)
  args.is_a?(PAnyType) && kind_of_callable? && args.callable_args?(self)
end

#callable_args?(callable) ⇒ 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 true if this instance is considered valid as arguments to callable

Returns:

  • (Boolean)


65
66
67
# File 'lib/puppet/pops/types/types.rb', line 65

def callable_args?(callable)
  false
end

#enumerable? ⇒ Boolean

Subclasses that are enumerable will override this method to return true

Returns:

  • (Boolean) —

    false#



71
72
73
# File 'lib/puppet/pops/types/types.rb', line 71

def enumerable?
  is_a?(Enumerable)
end

#generalize ⇒ PAnyType

Generalizes value specific types. Types that are not value specific will return self otherwize the generalized type is returned.

Returns:



80
81
82
83
# File 'lib/puppet/pops/types/types.rb', line 80

def generalize
  # Applicable to all types that have no variables
  self
end

#hash ⇒ Object



93
94
95
# File 'lib/puppet/pops/types/types.rb', line 93

def hash
  self.class.hash
end

#instance?(o) ⇒ Boolean

Returns true if the given argument o is an instance of this type

Returns:

  • (Boolean)


99
100
101
# File 'lib/puppet/pops/types/types.rb', line 99

def instance?(o)
  true
end

#kind_of_callable?(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.

Responds true for all callables, variants of callables and unless optional is false, all optional callables.

Returns:

  • (Boolean) —

    trueif this type is considered callable



89
90
91
# File 'lib/puppet/pops/types/types.rb', line 89

def kind_of_callable?(optional=true)
  false
end

#simple_name ⇒ String

Strips the class name from all module prefixes, the leading 'P' and the ending 'Type'. I.e. an instance of Puppet::Pops::Types::PVariantType will return 'Variant'

Returns:

  • (String) —

    the simple name of this type



112
113
114
115
# File 'lib/puppet/pops/types/types.rb', line 112

def simple_name
  n = self.class.name
  n[n.rindex('::')+3..n.size-5]
end

#to_s ⇒ Object



117
118
119
# File 'lib/puppet/pops/types/types.rb', line 117

def to_s
  Puppet::Pops::Types::TypeCalculator.string(self)
end