Class: Puppet::Pops::Types::PVariantType
- Inherits:
-
PAnyType
show all
- Includes:
- Enumerable
- Defined in:
- lib/puppet/pops/types/types.rb
Overview
A flexible type describing an any? of other types
Constant Summary
collapse
- DEFAULT =
PVariantType.new(EMPTY_ARRAY)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from PAnyType
#==, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #iterable?, #iterable_type, #loader, #name, #new_function, new_function, #resolve, #roundtrip_with_string?, #simple_name, simple_name, #to_alias_expanded_s, #to_s
_pcore_type, create_ptype, register_ptypes
#_pcore_all_contents, #_pcore_contents, #_pcore_init_hash, #_pcore_type, #to_s
Constructor Details
Returns a new instance of PVariantType.
2923
2924
2925
|
# File 'lib/puppet/pops/types/types.rb', line 2923
def initialize(types)
@types = types.freeze
end
|
Instance Attribute Details
2908
2909
2910
|
# File 'lib/puppet/pops/types/types.rb', line 2908
def types
@types
end
|
Class Method Details
.flatten_variants(types) ⇒ Object
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
|
# File 'lib/puppet/pops/types/types.rb', line 2989
def self.flatten_variants(types)
modified = false
types = types.map do |t|
if t.is_a?(PVariantType)
modified = true
t.types
else
t
end
end
types.flatten! if modified
types
end
|
.maybe_create(types) ⇒ PAnyType
Checks if the number of unique types in the given array is greater than one, and if so creates a Variant with those types and returns it. If only one unique type is found, that type is instead returned.
2917
2918
2919
2920
|
# File 'lib/puppet/pops/types/types.rb', line 2917
def self.maybe_create(types)
types = flatten_variants(types).uniq
types.size == 1 ? types[0] : new(types)
end
|
.register_ptype(loader, ir) ⇒ Object
2904
2905
2906
|
# File 'lib/puppet/pops/types/types.rb', line 2904
def self.register_ptype(loader, ir)
create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PTypeType::DEFAULT))
end
|
Instance Method Details
#accept(visitor, guard) ⇒ Object
2927
2928
2929
2930
|
# File 'lib/puppet/pops/types/types.rb', line 2927
def accept(visitor, guard)
super
@types.each { |t| t.accept(visitor, guard) }
end
|
#assignable?(o, guard = nil) ⇒ Boolean
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
|
# File 'lib/puppet/pops/types/types.rb', line 3031
def assignable?(o, guard = nil)
if o.is_a?(PUndefType) && types.empty?
return false
end
return super unless o.is_a?(PVariantType)
return true if types.empty?
!o.types.empty? && o.types.all? { |vt| super(vt, guard) }
end
|
2932
2933
2934
2935
2936
2937
2938
|
# File 'lib/puppet/pops/types/types.rb', line 2932
def each
if block_given?
types.each { |t| yield t }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
3025
3026
3027
|
# File 'lib/puppet/pops/types/types.rb', line 3025
def eql?(o)
self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end
|
#generalize ⇒ Object
2940
2941
2942
2943
2944
2945
2946
|
# File 'lib/puppet/pops/types/types.rb', line 2940
def generalize
if self == DEFAULT
self
else
alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
end
end
|
3003
3004
3005
|
# File 'lib/puppet/pops/types/types.rb', line 3003
def hash
@types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
3007
3008
3009
3010
|
# File 'lib/puppet/pops/types/types.rb', line 3007
def instance?(o, guard = nil)
@types.any? { |type| type.instance?(o, guard) }
end
|
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
3021
3022
3023
|
# File 'lib/puppet/pops/types/types.rb', line 3021
def kind_of_callable?(optional = true, guard = nil)
@types.all? { |type| type.kind_of_callable?(optional, guard) }
end
|
#normalize(guard = nil) ⇒ Object
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
|
# File 'lib/puppet/pops/types/types.rb', line 2948
def normalize(guard = nil)
if self == DEFAULT || @types.empty?
self
else
modified = false
types = alter_type_array(@types, :normalize, guard)
if types == self
types = @types
else
modified = true
end
if types.size == 1
types[0]
elsif types.any? { |t| t.is_a?(PUndefType) || t.is_a?(POptionalType) }
POptionalType.new(PVariantType.maybe_create(types.reject { |t| t.is_a?(PUndefType) }.map { |t| t.is_a?(POptionalType) ? t.type : t })).normalize
else
types = PVariantType.flatten_variants(types)
size_before_merge = types.size
types = swap_not_undefs(types)
types = merge_enums(types)
types = merge_patterns(types)
types = merge_version_ranges(types)
types = merge_numbers(PIntegerType, types)
types = merge_numbers(PFloatType, types)
types = merge_numbers(PTimespanType, types)
types = merge_numbers(PTimestampType, types)
if types.size == 1
types[0]
else
modified || types.size != size_before_merge ? PVariantType.maybe_create(types) : self
end
end
end
end
|
#really_instance?(o, guard = nil) ⇒ Boolean
3012
3013
3014
3015
3016
3017
3018
3019
|
# File 'lib/puppet/pops/types/types.rb', line 3012
def really_instance?(o, guard = nil)
@types.reduce(-1) do |memo, type|
ri = type.really_instance?(o, guard)
break ri if ri > 0
ri > memo ? ri : memo
end
end
|