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
#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, create, #create, #iterable?, #iterable_type, #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
Constructor Details
Returns a new instance of PVariantType.
2681
2682
2683
|
# File 'lib/puppet/pops/types/types.rb', line 2681
def initialize(types)
@types = types.freeze
end
|
Instance Attribute Details
2666
2667
2668
|
# File 'lib/puppet/pops/types/types.rb', line 2666
def types
@types
end
|
Class Method Details
.flatten_variants(types) ⇒ Object
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
|
# File 'lib/puppet/pops/types/types.rb', line 2747
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.
2675
2676
2677
2678
|
# File 'lib/puppet/pops/types/types.rb', line 2675
def self.maybe_create(types)
types = flatten_variants(types).uniq
types.size == 1 ? types[0] : new(types)
end
|
.register_ptype(loader, ir) ⇒ Object
2662
2663
2664
|
# File 'lib/puppet/pops/types/types.rb', line 2662
def self.register_ptype(loader, ir)
create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PType::DEFAULT))
end
|
Instance Method Details
#accept(visitor, guard) ⇒ Object
2685
2686
2687
2688
|
# File 'lib/puppet/pops/types/types.rb', line 2685
def accept(visitor, guard)
super
@types.each { |t| t.accept(visitor, guard) }
end
|
2690
2691
2692
2693
2694
2695
2696
|
# File 'lib/puppet/pops/types/types.rb', line 2690
def each
if block_given?
types.each { |t| yield t }
else
types.to_enum
end
end
|
#eql?(o) ⇒ Boolean
2785
2786
2787
|
# File 'lib/puppet/pops/types/types.rb', line 2785
def eql?(o)
self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end
|
#generalize ⇒ Object
2698
2699
2700
2701
2702
2703
2704
|
# File 'lib/puppet/pops/types/types.rb', line 2698
def generalize
if self == DEFAULT
self
else
alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
end
end
|
2761
2762
2763
|
# File 'lib/puppet/pops/types/types.rb', line 2761
def hash
@types.hash
end
|
#instance?(o, guard = nil) ⇒ Boolean
2765
2766
2767
2768
|
# File 'lib/puppet/pops/types/types.rb', line 2765
def instance?(o, guard = nil)
@types.any? { |type| type.instance?(o, guard) }
end
|
#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean
2777
2778
2779
|
# File 'lib/puppet/pops/types/types.rb', line 2777
def kind_of_callable?(optional = true, guard = nil)
@types.all? { |type| type.kind_of_callable?(optional, guard) }
end
|
#normalize(guard = nil) ⇒ Object
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
|
# File 'lib/puppet/pops/types/types.rb', line 2706
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
2770
2771
2772
2773
2774
2775
|
# File 'lib/puppet/pops/types/types.rb', line 2770
def really_instance?(o, guard = nil)
@types.reduce(-1) do |memo, type|
ri = type.really_instance?(o, guard)
ri > memo ? ri : memo
end
end
|
#resolved? ⇒ Boolean
2781
2782
2783
|
# File 'lib/puppet/pops/types/types.rb', line 2781
def resolved?
@types.all? { |type| type.resolved? }
end
|