Class: Puppet::Pops::Types::PVariantType
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 included from Enumerable
#uniq
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.
2902
2903
2904
|
# File 'lib/puppet/pops/types/types.rb', line 2902
def initialize(types)
@types = types.freeze
end
|
Instance Attribute Details
2887
2888
2889
|
# File 'lib/puppet/pops/types/types.rb', line 2887
def types
@types
end
|
Class Method Details
.flatten_variants(types) ⇒ Object
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
|
# File 'lib/puppet/pops/types/types.rb', line 2968
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.
2896
2897
2898
2899
|
# File 'lib/puppet/pops/types/types.rb', line 2896
def self.maybe_create(types)
types = flatten_variants(types).uniq
types.size == 1 ? types[0] : new(types)
end
|
.register_ptype(loader, ir) ⇒ Object
2883
2884
2885
|
# File 'lib/puppet/pops/types/types.rb', line 2883
def self.register_ptype(loader, ir)
create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PTypeType::DEFAULT))
end
|
Instance Method Details
#accept(visitor, guard) ⇒ Object
2906
2907
2908
2909
|
# File 'lib/puppet/pops/types/types.rb', line 2906
def accept(visitor, guard)
super
@types.each { |t| t.accept(visitor, guard) }
end
|
#assignable?(o, guard = nil) ⇒ Boolean
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
|
# File 'lib/puppet/pops/types/types.rb', line 3009
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
|
2911
2912
2913
2914
2915
2916
2917
|
# File 'lib/puppet/pops/types/types.rb', line 2911
def each
if block_given?
types.each { |t| yield t }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
3003
3004
3005
|
# File 'lib/puppet/pops/types/types.rb', line 3003
def eql?(o)
self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end
|
#generalize ⇒ Object
2919
2920
2921
2922
2923
2924
2925
|
# File 'lib/puppet/pops/types/types.rb', line 2919
def generalize
if self == DEFAULT
self
else
alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
end
end
|
2982
2983
2984
|
# File 'lib/puppet/pops/types/types.rb', line 2982
def hash
@types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2986
2987
2988
2989
|
# File 'lib/puppet/pops/types/types.rb', line 2986
def instance?(o, guard = nil)
@types.any? { |type| type.instance?(o, guard) }
end
|
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
2999
3000
3001
|
# File 'lib/puppet/pops/types/types.rb', line 2999
def kind_of_callable?(optional = true, guard = nil)
@types.all? { |type| type.kind_of_callable?(optional, guard) }
end
|
#normalize(guard = nil) ⇒ Object
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
|
# File 'lib/puppet/pops/types/types.rb', line 2927
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
2991
2992
2993
2994
2995
2996
2997
|
# File 'lib/puppet/pops/types/types.rb', line 2991
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
|