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.
2905
2906
2907
|
# File 'lib/puppet/pops/types/types.rb', line 2905
def initialize(types)
@types = types.freeze
end
|
Instance Attribute Details
2890
2891
2892
|
# File 'lib/puppet/pops/types/types.rb', line 2890
def types
@types
end
|
Class Method Details
.flatten_variants(types) ⇒ Object
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
|
# File 'lib/puppet/pops/types/types.rb', line 2971
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.
2899
2900
2901
2902
|
# File 'lib/puppet/pops/types/types.rb', line 2899
def self.maybe_create(types)
types = flatten_variants(types).uniq
types.size == 1 ? types[0] : new(types)
end
|
.register_ptype(loader, ir) ⇒ Object
2886
2887
2888
|
# File 'lib/puppet/pops/types/types.rb', line 2886
def self.register_ptype(loader, ir)
create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PTypeType::DEFAULT))
end
|
Instance Method Details
#accept(visitor, guard) ⇒ Object
2909
2910
2911
2912
|
# File 'lib/puppet/pops/types/types.rb', line 2909
def accept(visitor, guard)
super
@types.each { |t| t.accept(visitor, guard) }
end
|
#assignable?(o, guard = nil) ⇒ Boolean
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
|
# File 'lib/puppet/pops/types/types.rb', line 3012
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
|
2914
2915
2916
2917
2918
2919
2920
|
# File 'lib/puppet/pops/types/types.rb', line 2914
def each
if block_given?
types.each { |t| yield t }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
3006
3007
3008
|
# File 'lib/puppet/pops/types/types.rb', line 3006
def eql?(o)
self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end
|
#generalize ⇒ Object
2922
2923
2924
2925
2926
2927
2928
|
# File 'lib/puppet/pops/types/types.rb', line 2922
def generalize
if self == DEFAULT
self
else
alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
end
end
|
2985
2986
2987
|
# File 'lib/puppet/pops/types/types.rb', line 2985
def hash
@types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2989
2990
2991
2992
|
# File 'lib/puppet/pops/types/types.rb', line 2989
def instance?(o, guard = nil)
@types.any? { |type| type.instance?(o, guard) }
end
|
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
3002
3003
3004
|
# File 'lib/puppet/pops/types/types.rb', line 3002
def kind_of_callable?(optional = true, guard = nil)
@types.all? { |type| type.kind_of_callable?(optional, guard) }
end
|
#normalize(guard = nil) ⇒ Object
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
2967
2968
2969
|
# File 'lib/puppet/pops/types/types.rb', line 2930
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
2994
2995
2996
2997
2998
2999
3000
|
# File 'lib/puppet/pops/types/types.rb', line 2994
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
|