Class: Puppet::Pops::Types::PStructType

Inherits:
PAnyType show all
Includes:
Enumerable
Defined in:
lib/puppet/pops/types/types.rb

Constant Summary collapse

DEFAULT =
PStructType.new([])

Instance Method Summary collapse

Methods inherited from PAnyType

#assignable?, #callable?, #callable_args?, #enumerable?, #kind_of_callable?, #simple_name, #to_s

Methods included from Visitable

#accept

Constructor Details

#initialize(elements) ⇒ PStructType

Returns a new instance of PStructType.



830
831
832
# File 'lib/puppet/pops/types/types.rb', line 830

def initialize(elements)
  @elements = elements.sort.freeze
end

Instance Method Details

#==(o) ⇒ Object



854
855
856
# File 'lib/puppet/pops/types/types.rb', line 854

def ==(o)
  self.class == o.class && @elements == o.elements
end

#eachObject



834
835
836
837
838
839
840
# File 'lib/puppet/pops/types/types.rb', line 834

def each
  if block_given?
    elements.each { |elem| yield elem }
  else
    elements.to_enum
  end
end

#elementsObject



858
859
860
# File 'lib/puppet/pops/types/types.rb', line 858

def elements
  @elements
end

#generalizeObject



842
843
844
# File 'lib/puppet/pops/types/types.rb', line 842

def generalize
  @elements.empty? ? DEFAULT : PStructType.new(@elements.map { |se| se.generalize })
end

#hashObject



850
851
852
# File 'lib/puppet/pops/types/types.rb', line 850

def hash
  @elements.hash
end

#hashed_elementsObject



846
847
848
# File 'lib/puppet/pops/types/types.rb', line 846

def hashed_elements
  @hashed ||= @elements.reduce({}) {|memo, e| memo[e.name] = e; memo }
end

#instance?(o) ⇒ Boolean

Returns:

  • (Boolean)


862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
# File 'lib/puppet/pops/types/types.rb', line 862

def instance?(o)
  return false unless o.is_a?(Hash)
  matched = 0
  @elements.all? do |e|
    key = e.name
    v = o[key]
    if v.nil? && !o.include?(key)
      # Entry is missing. Only OK when key is optional
      e.key_type.assignable?(PUndefType::DEFAULT)
    else
      matched += 1
      e.value_type.instance?(v)
    end
  end && matched == o.size
end