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
Class Method Summary collapse
-
.all_callables ⇒ Object
Produces a CallableType matching all callables.
-
.array_of(o) ⇒ Object
Produces a type for Array where o is either a type, or an instance for which a type is inferred.
-
.array_of_data ⇒ Object
Produces a type for Array.
-
.boolean ⇒ Object
Produces the Boolean type.
-
.callable(*params) ⇒ Object
Produces a Callable type with one signature without support for a block Use #with_block, or #with_optional_block to add a block to the callable If no parameters are given, the Callable will describe a signature that does not accept parameters.
-
.catalog_entry ⇒ Object
Produces an instance of the abstract type PCatalogEntryType.
-
.collection ⇒ Object
Produces the abstract type Collection.
-
.constrain_size(collection_t, from, to) ⇒ Object
Sets the accepted size range of a collection if something other than the default 0 to Infinity is wanted.
-
.data ⇒ Object
Produces the Data type.
-
.enum(*values) ⇒ Object
Produces the Enum type, optionally with specific string values.
-
.float ⇒ Object
Produces the Float type.
-
.float_range(from, to) ⇒ Object
Produces a Float range type.
-
.hash_of(value, key = scalar()) ⇒ Object
Produces a type for Hash[Scalar, o] where o is either a type, or an instance for which a type is inferred.
-
.hash_of_data ⇒ Object
Produces a type for Hash[Scalar, Data].
-
.host_class(class_name = nil) ⇒ Object
Produces PHostClassType with a string class_name.
-
.integer ⇒ Object
Produces the Integer type.
-
.is_range_parameter?(t) ⇒ Boolean
Returns true if the given type t is of valid range parameter type (integer or literal default).
-
.label(t) ⇒ Object
Produces a string representation of the type.
-
.numeric ⇒ Object
Produces the Numeric type.
-
.object ⇒ Object
Produces the Object type.
-
.optional(optional_type = nil) ⇒ Object
Produces the Optional type, i.e.
-
.optional_object ⇒ Object
Convenience method to produce an Optional type.
- .pattern(*regular_expressions) ⇒ Object
-
.range(from, to) ⇒ Object
Produces an Integer range type.
-
.regexp(pattern = nil) ⇒ Object
Produces the Regexp type.
-
.resource(type_name = nil, title = nil) ⇒ Object
Produces a PResourceType with a String type_name A PResourceType with a nil or empty name is compatible with any other PResourceType.
-
.ruby(o) ⇒ Object
Produces a type for a class or infers a type for something that is not a class.
-
.ruby_type(class_name = nil) ⇒ Object
Generic creator of a RubyType - allows creating the Ruby type with nil name, or String name.
-
.scalar ⇒ Object
Produces the Literal type.
-
.string(*values) ⇒ Object
Produces the String type, optionally with specific string values.
-
.struct(name_type_hash = {}) ⇒ Object
Produces the Struct type, either a non parameterized instance representing all structs (i.e. all hashes) or a hash with a given set of keys of String type (names), bound to a value of a given type.
- .tuple(*types) ⇒ Object
-
.type_of(o) ⇒ Object
Produce a type corresponding to the class of given unless given is a String, Class or a PAbstractType.
-
.type_type(inst_type = nil) ⇒ Object
Produces a type for Type.
-
.undef ⇒ Object
Creates an instance of the Undef type.
-
.variant(*types) ⇒ Object
Produces the Variant type, optionally with the “one of” types.
- .with_block(callable, *block_params) ⇒ Object
- .with_optional_block(callable, *block_params) ⇒ Object
Class Method Details
.all_callables ⇒ Object
Produces a CallableType matching all callables
190 191 192 |
# File 'lib/puppet/pops/types/type_factory.rb', line 190 def self.all_callables() return Puppet::Pops::Types::PCallableType.new end |
.array_of(o) ⇒ Object
Produces a type for Array where o is either a type, or an instance for which a type is inferred.
302 303 304 305 306 |
# File 'lib/puppet/pops/types/type_factory.rb', line 302 def self.array_of(o) type = Types::PArrayType.new() type.element_type = type_of(o) type end |
.array_of_data ⇒ Object
Produces a type for Array
321 322 323 324 325 |
# File 'lib/puppet/pops/types/type_factory.rb', line 321 def self.array_of_data() type = Types::PArrayType.new() type.element_type = data() type end |
.boolean ⇒ Object
Produces the Boolean type
123 124 125 |
# File 'lib/puppet/pops/types/type_factory.rb', line 123 def self.boolean() Types::PBooleanType.new() end |
.callable(*params) ⇒ Object
Produces a Callable type with one signature without support for a block Use #with_block, or #with_optional_block to add a block to the callable If no parameters are given, the Callable will describe a signature that does not accept parameters. To create a Callable that matches all callables use #all_callables.
The params is a list of types, where the three last entries may be optionally followed by min, max count, and a Callable which is taken as the block_type. If neither min or max are specified the parameters must match exactly. A min < params.size means that the difference are optional. If max > params.size means that the last type repeats. if max is :default, the max value is unbound (infinity).
Params are given as a sequence of arguments to #type_of.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/puppet/pops/types/type_factory.rb', line 209 def self.callable(*params) case params.last when Types::PCallableType last_callable = true when Types::POptionalType last_callable = true if params.last.optional_type.is_a?(Types::PCallableType) end block_t = last_callable ? params.pop : nil # compute a size_type for the signature based on the two last parameters if is_range_parameter?(params[-2]) && is_range_parameter?(params[-1]) size_type = range(params[-2], params[-1]) params = params[0, params.size - 2] elsif is_range_parameter?(params[-1]) size_type = range(params[-1], :default) params = params[0, params.size - 1] end types = params.map {|p| type_of(p) } # create a signature callable_t = Types::PCallableType.new() tuple_t = tuple(*types) tuple_t.size_type = size_type unless size_type.nil? callable_t.param_types = tuple_t callable_t.block_type = block_t callable_t end |
.catalog_entry ⇒ Object
Produces an instance of the abstract type PCatalogEntryType
270 271 272 |
# File 'lib/puppet/pops/types/type_factory.rb', line 270 def self.catalog_entry() Types::PCatalogEntryType.new() end |
.collection ⇒ Object
Produces the abstract type Collection
252 253 254 |
# File 'lib/puppet/pops/types/type_factory.rb', line 252 def self.collection() Types::PCollectionType.new() end |
.constrain_size(collection_t, from, to) ⇒ Object
Sets the accepted size range of a collection if something other than the default 0 to Infinity is wanted. The semantics for from/to are the same as for #range
396 397 398 399 |
# File 'lib/puppet/pops/types/type_factory.rb', line 396 def self.constrain_size(collection_t, from, to) collection_t.size_type = range(from, to) collection_t end |
.data ⇒ Object
Produces the Data type
259 260 261 |
# File 'lib/puppet/pops/types/type_factory.rb', line 259 def self.data() Types::PDataType.new() end |
.enum(*values) ⇒ Object
Produces the Enum type, optionally with specific string values
81 82 83 84 85 |
# File 'lib/puppet/pops/types/type_factory.rb', line 81 def self.enum(*values) t = Types::PEnumType.new() values.each {|v| t.addValues(v) } t end |
.float ⇒ Object
Produces the Float type
39 40 41 |
# File 'lib/puppet/pops/types/type_factory.rb', line 39 def self.float() Types::PFloatType.new() end |
.float_range(from, to) ⇒ Object
Produces a Float range type
29 30 31 32 33 34 |
# File 'lib/puppet/pops/types/type_factory.rb', line 29 def self.float_range(from, to) t = Types::PFloatType.new() t.from = Float(from) unless from == :default || from.nil? t.to = Float(to) unless to == :default || to.nil? t end |
.hash_of(value, key = scalar()) ⇒ Object
Produces a type for Hash[Scalar, o] where o is either a type, or an instance for which a type is inferred.
311 312 313 314 315 316 |
# File 'lib/puppet/pops/types/type_factory.rb', line 311 def self.hash_of(value, key = scalar()) type = Types::PHashType.new() type.key_type = type_of(key) type.element_type = type_of(value) type end |
.hash_of_data ⇒ Object
Produces a type for Hash[Scalar, Data]
330 331 332 333 334 335 |
# File 'lib/puppet/pops/types/type_factory.rb', line 330 def self.hash_of_data() type = Types::PHashType.new() type.key_type = scalar() type.element_type = data() type end |
.host_class(class_name = nil) ⇒ Object
Produces PHostClassType with a string class_name. A PHostClassType with nil or empty name is compatible with any other PHostClassType. A PHostClassType with a given name is only compatible with a PHostClassType with the same name.
291 292 293 294 295 296 297 |
# File 'lib/puppet/pops/types/type_factory.rb', line 291 def self.host_class(class_name = nil) type = Types::PHostClassType.new() unless class_name.nil? type.class_name = class_name.sub(/^::/, '') end type end |
.integer ⇒ Object
Produces the Integer type
12 13 14 |
# File 'lib/puppet/pops/types/type_factory.rb', line 12 def self.integer() Types::PIntegerType.new() end |
.is_range_parameter?(t) ⇒ Boolean
Returns true if the given type t is of valid range parameter type (integer or literal default).
402 403 404 |
# File 'lib/puppet/pops/types/type_factory.rb', line 402 def self.is_range_parameter?(t) t.is_a?(Integer) || t == 'default' || t == :default end |
.label(t) ⇒ Object
Produces a string representation of the type
53 54 55 |
# File 'lib/puppet/pops/types/type_factory.rb', line 53 def self.label(t) @type_calculator.string(t) end |
.numeric ⇒ Object
Produces the Numeric type
46 47 48 |
# File 'lib/puppet/pops/types/type_factory.rb', line 46 def self.numeric() Types::PNumericType.new() end |
.object ⇒ Object
Produces the Object type
130 131 132 |
# File 'lib/puppet/pops/types/type_factory.rb', line 130 def self.object() Types::PObjectType.new() end |
.optional(optional_type = nil) ⇒ Object
Produces the Optional type, i.e. a short hand for Variant[T, Undef]
67 68 69 70 71 |
# File 'lib/puppet/pops/types/type_factory.rb', line 67 def self.optional(optional_type = nil) t = Types::POptionalType.new t.optional_type = type_of(optional_type) t end |
.optional_object ⇒ Object
Convenience method to produce an Optional type
74 75 76 |
# File 'lib/puppet/pops/types/type_factory.rb', line 74 def self.optional_object() optional(object()) end |
.pattern(*regular_expressions) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/puppet/pops/types/type_factory.rb', line 147 def self.pattern(*regular_expressions) t = Types::PPatternType.new() regular_expressions.each do |re| case re when String re_T = Types::PRegexpType.new() re_T.pattern = re re_T.regexp() # compile it to catch errors t.addPatterns(re_T) when Regexp re_T = Types::PRegexpType.new() # Regep.to_s includes options user did not enter and does not escape source # to work either as a string or as a // regexp. The inspect method does a better # job, but includes the // re_T.pattern = re.inspect[1..-2] t.addPatterns(re_T) when Types::PRegexpType t.addPatterns(re.copy) when Types::PPatternType re.patterns.each do |p| t.addPatterns(p.copy) end else raise ArgumentError, "Only String, Regexp, Pattern-Type, and Regexp-Type are allowed: got '#{re.class}" end end t end |
.range(from, to) ⇒ Object
Produces an Integer range type
19 20 21 22 23 24 |
# File 'lib/puppet/pops/types/type_factory.rb', line 19 def self.range(from, to) t = Types::PIntegerType.new() t.from = from unless (from == :default || from == 'default') t.to = to unless (to == :default || to == 'default') t end |
.regexp(pattern = nil) ⇒ Object
Produces the Regexp type
138 139 140 141 142 143 144 145 |
# File 'lib/puppet/pops/types/type_factory.rb', line 138 def self.regexp(pattern = nil) t = Types::PRegexpType.new() if pattern t.pattern = pattern.is_a?(Regexp) ? pattern.inspect[1..-2] : pattern end t.regexp() unless pattern.nil? # compile pattern to catch errors t end |
.resource(type_name = nil, title = nil) ⇒ Object
Produces a PResourceType with a String type_name A PResourceType with a nil or empty name is compatible with any other PResourceType. A PResourceType with a given name is only compatible with a PResourceType with the same name. (There is no resource-type subtyping in Puppet (yet)).
279 280 281 282 283 284 285 |
# File 'lib/puppet/pops/types/type_factory.rb', line 279 def self.resource(type_name = nil, title = nil) type = Types::PResourceType.new() type_name = type_name.type_name if type_name.is_a?(Types::PResourceType) type.type_name = type_name.downcase unless type_name.nil? type.title = title type end |
.ruby(o) ⇒ Object .ruby(o) ⇒ Object
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
374 375 376 377 378 379 380 381 382 |
# File 'lib/puppet/pops/types/type_factory.rb', line 374 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 |
.ruby_type(class_name = nil) ⇒ Object
Generic creator of a RubyType - allows creating the Ruby type with nil name, or String name. Also see ruby(o) which performs inference, or mapps a Ruby Class to its name.
387 388 389 390 391 |
# File 'lib/puppet/pops/types/type_factory.rb', line 387 def self.ruby_type(class_name = nil) type = Types::PRubyType.new() type.ruby_class = class_name type end |
.scalar ⇒ Object
Produces the Literal type
183 184 185 |
# File 'lib/puppet/pops/types/type_factory.rb', line 183 def self.scalar() Types::PScalarType.new() end |
.string(*values) ⇒ Object
Produces the String type, optionally with specific string values
60 61 62 63 64 |
# File 'lib/puppet/pops/types/type_factory.rb', line 60 def self.string(*values) t = Types::PStringType.new() values.each {|v| t.addValues(v) } t end |
.struct(name_type_hash = {}) ⇒ Object
Produces the Struct type, either a non parameterized instance representing all structs (i.e. all hashes) or a hash with a given set of keys of String type (names), bound to a value of a given type. Type may be a Ruby Class, a Puppet Type, or an instance from which the type is inferred.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/puppet/pops/types/type_factory.rb', line 100 def self.struct(name_type_hash = {}) t = Types::PStructType.new name_type_hash.map do |name, type| elem = Types::PStructElement.new if name.is_a?(String) && name.empty? raise ArgumentError, "An empty String can not be used where a String[1, default] is expected" end elem.name = name elem.type = type_of(type) elem end.each {|elem| t.addElements(elem) } t end |
.tuple(*types) ⇒ Object
114 115 116 117 118 |
# File 'lib/puppet/pops/types/type_factory.rb', line 114 def self.tuple(*types) t = Types::PTupleType.new types.each {|elem| t.addTypes(type_of(elem)) } t end |
.type_of(o) ⇒ Object
Produce a type corresponding to the class of given unless given is a String, Class or a PAbstractType. When a String is given this is taken as a classname.
349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/puppet/pops/types/type_factory.rb', line 349 def self.type_of(o) if o.is_a?(Class) @type_calculator.type(o) elsif o.is_a?(Types::PAbstractType) o elsif o.is_a?(String) type = Types::PRubyType.new() type.ruby_class = o type else @type_calculator.infer_generic(o) end end |
.type_type(inst_type = nil) ⇒ Object
Produces a type for Type
340 341 342 343 344 |
# File 'lib/puppet/pops/types/type_factory.rb', line 340 def self.type_type(inst_type = nil) type = Types::PType.new() type.type = inst_type type end |
.undef ⇒ Object
Creates an instance of the Undef type
265 266 267 |
# File 'lib/puppet/pops/types/type_factory.rb', line 265 def self.undef() Types::PNilType.new() end |
.variant(*types) ⇒ Object
Produces the Variant type, optionally with the “one of” types
90 91 92 93 94 |
# File 'lib/puppet/pops/types/type_factory.rb', line 90 def self.variant(*types) t = Types::PVariantType.new() types.each {|v| t.addTypes(type_of(v)) } t end |
.with_block(callable, *block_params) ⇒ Object
239 240 241 242 |
# File 'lib/puppet/pops/types/type_factory.rb', line 239 def self.with_block(callable, *block_params) callable.block_type = callable(*block_params) callable end |
.with_optional_block(callable, *block_params) ⇒ Object
244 245 246 247 |
# File 'lib/puppet/pops/types/type_factory.rb', line 244 def self.with_optional_block(callable, *block_params) callable.block_type = optional(callable(*block_params)) callable end |