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

DATA =

Variant compatible with the Data type.

PVariantType.new([PHashType::DATA, PArrayType::DATA, PScalarType::DEFAULT, PUndefType::DEFAULT, PTupleType::DATA])
DEFAULT =
PVariantType.new(EMPTY_ARRAY)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PAnyType

#==, #assignable?, #callable?, #callable_args?, #callable_with?, #check_self_recursion, #create, #iterable?, #iterable_type, #name, new_function, #new_function, #resolve, simple_name, #simple_name, #to_alias_expanded_s, #to_s

Methods inherited from TypedModelObject

_ptype, create_ptype, register_ptypes

Methods included from PuppetObject

#_ptype

Constructor Details

#initialize(types) ⇒ PVariantType

Returns a new instance of PVariantType.

Parameters:



2676
2677
2678
# File 'lib/puppet/pops/types/types.rb', line 2676

def initialize(types)
  @types = types.freeze
end

Instance Attribute Details

#typesObject (readonly)



2661
2662
2663
# File 'lib/puppet/pops/types/types.rb', line 2661

def types
  @types
end

Class Method Details

.flatten_variants(types) ⇒ Object



2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
# File 'lib/puppet/pops/types/types.rb', line 2742

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.

Parameters:

Returns:



2670
2671
2672
2673
# File 'lib/puppet/pops/types/types.rb', line 2670

def self.maybe_create(types)
  types = flatten_variants(types).uniq
  types.size == 1 ? types[0] : new(types)
end

.register_ptype(loader, ir) ⇒ Object



2657
2658
2659
# File 'lib/puppet/pops/types/types.rb', line 2657

def self.register_ptype(loader, ir)
  create_ptype(loader, ir, 'AnyType', 'types' => PArrayType.new(PType::DEFAULT))
end

Instance Method Details

#accept(visitor, guard) ⇒ Object



2680
2681
2682
2683
# File 'lib/puppet/pops/types/types.rb', line 2680

def accept(visitor, guard)
  super
  @types.each { |t| t.accept(visitor, guard) }
end

#eachObject



2685
2686
2687
2688
2689
2690
2691
# File 'lib/puppet/pops/types/types.rb', line 2685

def each
  if block_given?
    types.each { |t| yield t }
  else
    types.to_enum
  end
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


2780
2781
2782
2783
# File 'lib/puppet/pops/types/types.rb', line 2780

def eql?(o)
  o = DATA if o.is_a?(PDataType)
  self.class == o.class && @types.size == o.types.size && (@types - o.types).empty?
end

#generalizeObject



2693
2694
2695
2696
2697
2698
2699
# File 'lib/puppet/pops/types/types.rb', line 2693

def generalize
  if self == DEFAULT || self == DATA
    self
  else
    alter_type_array(@types, :generalize) { |altered| PVariantType.maybe_create(altered) }
  end
end

#hashObject



2756
2757
2758
# File 'lib/puppet/pops/types/types.rb', line 2756

def hash
  @types.hash
end

#instance?(o, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


2760
2761
2762
2763
# File 'lib/puppet/pops/types/types.rb', line 2760

def instance?(o, guard = nil)
  # instance of variant if o is instance? of any of variant's types
  @types.any? { |type| type.instance?(o, guard) }
end

#kind_of_callable?(optional = true, guard = nil) ⇒ Boolean

Returns:

  • (Boolean)


2772
2773
2774
# File 'lib/puppet/pops/types/types.rb', line 2772

def kind_of_callable?(optional = true, guard = nil)
  @types.all? { |type| type.kind_of_callable?(optional, guard) }
end

#normalize(guard = nil) ⇒ Object



2701
2702
2703
2704
2705
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
# File 'lib/puppet/pops/types/types.rb', line 2701

def normalize(guard = nil)
  if self == DEFAULT || self == DATA || @types.empty?
    self
  else
    # Normalize all contained types
    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) }
      # Undef entry present. Use an OptionalType with a normalized Variant without Undefs and Optional wrappers
      POptionalType.new(PVariantType.maybe_create(types.reject { |t| t.is_a?(PUndefType) }.map { |t| t.is_a?(POptionalType) ? t.type : t })).normalize
    else
      # Merge all variants into this one
      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

Returns:

  • (Boolean)


2765
2766
2767
2768
2769
2770
# File 'lib/puppet/pops/types/types.rb', line 2765

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

Returns:

  • (Boolean)


2776
2777
2778
# File 'lib/puppet/pops/types/types.rb', line 2776

def resolved?
  @types.all? { |type| type.resolved? }
end