Module: Puppet::Pops::Types::TypeFactory

Defined in:
lib/puppet/pops/types/type_factory.rb

Overview

Helper module that makes creation of type objects simpler.

Constant Summary collapse

Types =
Puppet::Pops::Types

Class Method Summary collapse

Class Method Details

.array_of(o) ⇒ Object

Produces a type for Array where o is either a type, or an instance for which a type is inferred.



75
76
77
78
79
# File 'lib/puppet/pops/types/type_factory.rb', line 75

def self.array_of(o)
  type = Types::PArrayType.new()
  type.element_type = type_of(o)
  type
end

.array_of_dataObject

Produces a type for Array



94
95
96
97
98
# File 'lib/puppet/pops/types/type_factory.rb', line 94

def self.array_of_data()
  type = Types::PArrayType.new()
  type.element_type = data()
  type
end

.booleanObject

Produces the Boolean type



40
41
42
# File 'lib/puppet/pops/types/type_factory.rb', line 40

def self.boolean()
  Types::PBooleanType.new()
end

.collectionObject

Produces the abstract type Collection



61
62
63
# File 'lib/puppet/pops/types/type_factory.rb', line 61

def self.collection()
  Types::PCollectionType.new()
end

.dataObject

Produces the Data type



68
69
70
# File 'lib/puppet/pops/types/type_factory.rb', line 68

def self.data()
  Types::PDataType.new()
end

.floatObject

Produces the Float type



19
20
21
# File 'lib/puppet/pops/types/type_factory.rb', line 19

def self.float()
  Types::PFloatType.new()
end

.hash_of(value, key = literal()) ⇒ Object

Produces a type for Hash[Literal, o] where o is either a type, or an instance for which a type is inferred.



84
85
86
87
88
89
# File 'lib/puppet/pops/types/type_factory.rb', line 84

def self.hash_of(value, key = literal())
  type = Types::PHashType.new()
  type.key_type = type_of(key)
  type.element_type = type_of(value)
  type
end

.hash_of_dataObject

Produces a type for Hash[Literal, Data]



103
104
105
106
107
108
# File 'lib/puppet/pops/types/type_factory.rb', line 103

def self.hash_of_data()
  type = Types::PHashType.new()
  type.key_type = literal()
  type.element_type = data()
  type
end

.integerObject

Produces the Integer type



12
13
14
# File 'lib/puppet/pops/types/type_factory.rb', line 12

def self.integer()
  Types::PIntegerType.new()
end

.label(t) ⇒ Object

Produces a string representation of the type



26
27
28
# File 'lib/puppet/pops/types/type_factory.rb', line 26

def self.label(t)
  @type_calculator.string(t)
end

.literalObject

Produces the Literal type



54
55
56
# File 'lib/puppet/pops/types/type_factory.rb', line 54

def self.literal()
  Types::PLiteralType.new()
end

.patternObject

Produces the Pattern type



47
48
49
# File 'lib/puppet/pops/types/type_factory.rb', line 47

def self.pattern()
  Types::PPatternType.new()
end

.ruby(o) ⇒ Object .ruby(o) ⇒ Object

Note:

To get the type for the class’ class use ‘TypeCalculator.infer©`

Produces a type for a class or infers a type for something that is not a class

Overloads:

  • .ruby(o) ⇒ Object

    Parameters:

    • o (Class)

      produces the type corresponding to the class (e.g. Integer becomes PIntegerType)

  • .ruby(o) ⇒ Object

    Parameters:

    • o (Object)

      produces the type corresponding to the instance class (e.g. 3 becomes PIntegerType)



138
139
140
141
142
143
144
145
146
# File 'lib/puppet/pops/types/type_factory.rb', line 138

def self.ruby(o)
  if o.is_a?(Class)
    @type_calculator.type(o)
  else
    type = Types::PRubyType.new()
    type.ruby_class = o.class.name
    type
  end
end

.stringObject

Produces the String type



33
34
35
# File 'lib/puppet/pops/types/type_factory.rb', line 33

def self.string()
  Types::PStringType.new()
end

.type_of(o) ⇒ Object

Produce a type corresponding to the class of given unless given is a String, Class or a PObjectType. When a String is given this is taken as a classname.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppet/pops/types/type_factory.rb', line 113

def self.type_of(o)
  if o.is_a?(Class)
    @type_calculator.type(o)
  elsif o.is_a?(Types::PObjectType)
    o
  elsif o.is_a?(String)
    type = Types::PRubyType.new()
    type.ruby_class = o
    type
  else
    @type_calculator.infer(o)
  end
end