Class: Puppet::Pops::Types::PTypeAliasType
Overview
Describes a named alias for another Type. The alias is created with a name and an unresolved type expression. The type expression may in turn contain other aliases (including the alias that contains it) which means that an alias might contain self recursion. Whether or not that is the case is computed and remembered when the alias is resolved since guarding against self recursive constructs is relatively expensive.
Defined Under Namespace
Classes: AssertOtherTypeAcceptor, AssertSelfRecursionStatusAcceptor
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#accept(visitor, guard) ⇒ Object
-
#assignable?(o, guard = nil) ⇒ Boolean
-
#callable_args?(callable, guard) ⇒ Boolean
-
#check_self_recursion(originator) ⇒ Object
-
#eql?(o) ⇒ Boolean
-
#hash ⇒ Object
-
#initialize(name, type_expr, resolved_type = nil) ⇒ PTypeAliasType
constructor
A new instance of PTypeAliasType.
-
#instance?(o, guard = nil) ⇒ Boolean
-
#iterable?(guard = nil) ⇒ Boolean
-
#iterable_type(guard = nil) ⇒ Object
-
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
-
#method_missing(name, *arguments, &block) ⇒ Object
Delegates to resolved type.
-
#really_instance?(o, guard = nil) ⇒ Boolean
private
-
#resolve(type_parser, loader) ⇒ PTypeAliasType
private
Called from the TypeParser once it has found a type using the Loader.
-
#resolved_type ⇒ PAnyType
Returns the resolved type.
-
#respond_to_missing?(name, include_private) ⇒ Boolean
Delegates to resolved type.
-
#self_recursion? ⇒ Boolean
-
#set_self_recursion_status ⇒ Object
-
#to_s ⇒ String
Returns the expanded string the form of the alias, e.g.
-
#type_expr ⇒ Object
private
nil to prevent serialization of the type_expr used when first initializing this instance.
Methods inherited from PAnyType
#==, #callable?, #callable_with?, #create, #generalize, new_function, #normalize, simple_name, #simple_name, #to_alias_expanded_s
_ptype, create_ptype, register_ptypes
#_ptype
Constructor Details
#initialize(name, type_expr, resolved_type = nil) ⇒ PTypeAliasType
Returns a new instance of PTypeAliasType.
3154
3155
3156
3157
3158
3159
|
# File 'lib/puppet/pops/types/types.rb', line 3154
def initialize(name, type_expr, resolved_type = nil)
@name = name
@type_expr = type_expr
@resolved_type = resolved_type
@self_recursion = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, &block) ⇒ Object
Delegates to resolved type
3322
3323
3324
3325
|
# File 'lib/puppet/pops/types/types.rb', line 3322
def method_missing(name, *arguments, &block)
super if @resolved_type.equal?(PTypeReferenceType::DEFAULT)
resolved_type.send(name, *arguments, &block)
end
|
Instance Attribute Details
3149
3150
3151
|
# File 'lib/puppet/pops/types/types.rb', line 3149
def name
@name
end
|
Class Method Details
.register_ptype(loader, ir) ⇒ Object
Instance Method Details
#accept(visitor, guard) ⇒ Object
3297
3298
3299
3300
3301
3302
|
# File 'lib/puppet/pops/types/types.rb', line 3297
def accept(visitor, guard)
guarded_recursion(guard, nil) do |g|
super(visitor, g)
@resolved_type.accept(visitor, g) unless @resolved_type.nil?
end
end
|
#assignable?(o, guard = nil) ⇒ Boolean
3161
3162
3163
3164
3165
3166
3167
3168
|
# File 'lib/puppet/pops/types/types.rb', line 3161
def assignable?(o, guard = nil)
if @self_recursion
guard ||= RecursionGuard.new
guard.with_this(self) { |state| state == RecursionGuard::SELF_RECURSION_IN_BOTH ? true : super(o, guard) }
else
super(o, guard)
end
end
|
#callable_args?(callable, guard) ⇒ Boolean
3180
3181
3182
|
# File 'lib/puppet/pops/types/types.rb', line 3180
def callable_args?(callable, guard)
guarded_recursion(guard, false) { |g| resolved_type.callable_args?(callable, g) }
end
|
#check_self_recursion(originator) ⇒ Object
3184
3185
3186
|
# File 'lib/puppet/pops/types/types.rb', line 3184
def check_self_recursion(originator)
resolved_type.check_self_recursion(originator) unless originator.equal?(self)
end
|
#eql?(o) ⇒ Boolean
3293
3294
3295
|
# File 'lib/puppet/pops/types/types.rb', line 3293
def eql?(o)
super && o.name == @name
end
|
3204
3205
3206
|
# File 'lib/puppet/pops/types/types.rb', line 3204
def hash
@name.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
3192
3193
3194
|
# File 'lib/puppet/pops/types/types.rb', line 3192
def instance?(o, guard = nil)
really_instance?(o, guard) == 1
end
|
#iterable?(guard = nil) ⇒ Boolean
3196
3197
3198
|
# File 'lib/puppet/pops/types/types.rb', line 3196
def iterable?(guard = nil)
guarded_recursion(guard, false) { |g| resolved_type.iterable?(g) }
end
|
#iterable_type(guard = nil) ⇒ Object
3200
3201
3202
|
# File 'lib/puppet/pops/types/types.rb', line 3200
def iterable_type(guard = nil)
guarded_recursion(guard, nil) { |g| resolved_type.iterable_type(g) }
end
|
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
3188
3189
3190
|
# File 'lib/puppet/pops/types/types.rb', line 3188
def kind_of_callable?(optional=true, guard = nil)
guarded_recursion(guard, false) { |g| resolved_type.kind_of_callable?(optional, g) }
end
|
#really_instance?(o, guard = nil) ⇒ 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.
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
|
# File 'lib/puppet/pops/types/types.rb', line 3328
def really_instance?(o, guard = nil)
if @self_recursion
guard ||= RecursionGuard.new
guard.with_that(o) do
guard.with_this(self) { |state| state == RecursionGuard::SELF_RECURSION_IN_BOTH ? 0 : resolved_type.really_instance?(o, guard) }
end
else
resolved_type.really_instance?(o, guard)
end
end
|
#resolve(type_parser, loader) ⇒ PTypeAliasType
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.
Called from the TypeParser once it has found a type using the Loader. The TypeParser will interpret the contained expression and the resolved type is remembered. This method also checks and remembers if the resolve type contains self recursion.
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
|
# File 'lib/puppet/pops/types/types.rb', line 3254
def resolve(type_parser, loader)
if @resolved_type.nil?
@resolved_type = PTypeReferenceType::DEFAULT
@self_recursion = true begin
if @type_expr.is_a?(PTypeReferenceType)
@resolved_type = @type_expr.resolve(type_parser, loader)
else
@resolved_type = type_parser.interpret(@type_expr, loader).normalize
end
guard = RecursionGuard.new
real_type_asserter = AssertOtherTypeAcceptor.new
accept(real_type_asserter, guard)
unless real_type_asserter.other_type_detected?
raise ArgumentError, "Type alias '#{name}' cannot be resolved to a real type"
end
@self_recursion = guard.recursive_this?(self)
if @self_recursion
accept(AssertSelfRecursionStatusAcceptor.new, RecursionGuard.new)
when_self_recursion_detected
end
rescue
@resolved_type = nil
raise
end
else
@resolved_type.resolve(type_parser, loader) unless @resolved_type.equal?(PTypeReferenceType::DEFAULT)
end
self
end
|
#resolved_type ⇒ PAnyType
Returns the resolved type. The type must have been resolved by a call prior to calls to this method or an error will be raised.
3175
3176
3177
3178
|
# File 'lib/puppet/pops/types/types.rb', line 3175
def resolved_type
raise Puppet::Error, "Reference to unresolved type #{@name}" unless @resolved_type
@resolved_type
end
|
#respond_to_missing?(name, include_private) ⇒ Boolean
Delegates to resolved type
3317
3318
3319
|
# File 'lib/puppet/pops/types/types.rb', line 3317
def respond_to_missing?(name, include_private)
resolved_type.respond_to?(name, include_private)
end
|
#self_recursion? ⇒ Boolean
3304
3305
3306
|
# File 'lib/puppet/pops/types/types.rb', line 3304
def self_recursion?
@self_recursion
end
|
#set_self_recursion_status ⇒ Object
3237
3238
3239
3240
3241
3242
3243
3244
|
# File 'lib/puppet/pops/types/types.rb', line 3237
def set_self_recursion_status
return if @self_recursion || @resolved_type.is_a?(PTypeReferenceType)
@self_recursion = true
guard = RecursionGuard.new
accept(NoopTypeAcceptor::INSTANCE, guard)
@self_recursion = guard.recursive_this?(self)
when_self_recursion_detected if @self_recursion end
|
#to_s ⇒ String
Returns the expanded string the form of the alias, e.g. <alias name> = <resolved type>
3312
3313
3314
|
# File 'lib/puppet/pops/types/types.rb', line 3312
def to_s
TypeFormatter.singleton.alias_expanded_string(self)
end
|
#type_expr ⇒ 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.
Returns nil to prevent serialization of the type_expr used when first initializing this instance.
3341
3342
3343
|
# File 'lib/puppet/pops/types/types.rb', line 3341
def type_expr
nil
end
|