Module: Puppet::Pops::Types::Iterable Private

Included in:
TreeIterator, Iterator
Defined in:
lib/puppet/pops/types/iterable.rb,
lib/puppet/pops/types/tree_iterators.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

The runtime Iterable type for an Iterable

Defined Under Namespace

Classes: BreadthFirstTreeIterator, DepthFirstTreeIterator, TreeIterator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.asserted_iterable(caller, obj) ⇒ Iterable?

Produces an ‘Iterable` for one of the following types with the following characterstics:

‘String` - yields each character in the string `Array` - yields each element in the array `Hash` - yields each key/value pair as a two element array `Integer` - when positive, yields each value from zero to the given number `PIntegerType` - yields each element from min to max (inclusive) provided min < max and neither is unbounded. `PEnumtype` - yields each possible value of the enum. `Range` - yields an iterator for all elements in the range provided that the range start and end

are both integers or both strings and start is less than end using natural ordering.

‘Dir` - yields each name in the directory

An ‘ArgumentError` is raised for all other objects.

Parameters:

  • o (Object)

    The object to produce an ‘Iterable` for

Returns:

  • (Iterable, nil)

    The produced ‘Iterable`

Raises:

  • (ArgumentError)

    In case an ‘Iterable` cannot be produced



30
31
32
33
34
# File 'lib/puppet/pops/types/iterable.rb', line 30

def self.asserted_iterable(caller, obj)
  iter = self.on(obj)
  raise ArgumentError, "#{caller.class}(): wrong argument type (#{obj.class}; is not Iterable." if iter.nil?
  iter
end

.on(o, element_type = nil) ⇒ Iterable?

Produces an ‘Iterable` for one of the following types with the following characteristics:

‘String` - yields each character in the string `Array` - yields each element in the array `Hash` - yields each key/value pair as a two element array `Integer` - when positive, yields each value from zero to the given number `PIntegerType` - yields each element from min to max (inclusive) provided min < max and neither is unbounded. `PEnumtype` - yields each possible value of the enum. `Range` - yields an iterator for all elements in the range provided that the range start and end

are both integers or both strings and start is less than end using natural ordering.

‘Dir` - yields each name in the directory

The value ‘nil` is returned for all other objects.

Parameters:

  • o (Object)

    The object to produce an ‘Iterable` for

  • element_type (PAnyType) (defaults to: nil)

    the element type for the iterator. Optional (inferred if not provided)

Returns:

  • (Iterable, nil)

    The produced ‘Iterable` or `nil` if it couldn’t be produced



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/puppet/pops/types/iterable.rb', line 55

def self.on(o, element_type = nil)
  case o
  when IteratorProducer
    o.iterator
  when Iterable
    o
  when String
    Iterator.new(PStringType.new(PIntegerType.new(1, 1)), o.each_char)
  when Array
    if o.empty?
      Iterator.new(PUnitType::DEFAULT, o.each)
    else
      if element_type.nil?
        tc = TypeCalculator.singleton
        element_type = PVariantType.maybe_create(o.map {|e| tc.infer_set(e) })
      end
      Iterator.new(element_type, o.each)
    end
  when Hash
    # Each element is a two element [key, value] tuple.
    if o.empty?
      HashIterator.new(PHashType::DEFAULT_KEY_PAIR_TUPLE, o.each)
    else
      if element_type.nil?
        tc = TypeCalculator.singleton
        element_type = PTupleType.new([
          PVariantType.maybe_create(o.keys.map {|e| tc.infer_set(e) }),
          PVariantType.maybe_create(o.values.map {|e| tc.infer_set(e) })], PHashType::KEY_PAIR_TUPLE_SIZE)
      end
      HashIterator.new(element_type, o.each_pair)
    end
  when Integer
    if o == 0
      Iterator.new(PUnitType::DEFAULT, o.times)
    elsif o > 0
      IntegerRangeIterator.new(PIntegerType.new(0, o - 1))
    else
      nil
    end
  when PIntegerType
    # a finite range will always produce at least one element since it's inclusive
    o.finite_range? ? IntegerRangeIterator.new(o) : nil
  when PEnumType
    Iterator.new(o, o.values.each)
  when PTypeAliasType
    on(o.resolved_type)
  when Range
    min = o.min
    max = o.max
    if min.is_a?(Integer) && max.is_a?(Integer) && max >= min
      IntegerRangeIterator.new(PIntegerType.new(min, max))
    elsif min.is_a?(String) && max.is_a?(String) && max >= min
      # A generalized element type where only the size is inferred is used here since inferring the full
      # range might waste a lot of memory.
      if min.length < max.length
        shortest = min
        longest = max
      else
        shortest = max
        longest = min
      end
      Iterator.new(PStringType.new(PIntegerType.new(shortest.length, longest.length)), o.each)
    else
      # Unsupported range. It's either descending or nonsensical for other reasons (float, mixed types, etc.)
      nil
    end
  else
    # Not supported. We cannot determine the element type
    nil
  end
end

.unbounded?(object) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Answers the question if there is an end to the iteration. Puppet does not currently provide any unbounded iterables.

Returns:

  • (Boolean)

    ‘true` if the iteration is unbounded



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/puppet/pops/types/iterable.rb', line 131

def self.unbounded?(object)
  case object
  when Iterable
    object.unbounded?
  when String,Integer,Array,Hash,Enumerator,PIntegerType,PEnumType,Dir
    false
  else
    TypeAsserter.assert_instance_of('', PIterableType::DEFAULT, object, false)
    !object.respond_to?(:size)
  end
end

Instance Method Details

#each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



143
144
145
# File 'lib/puppet/pops/types/iterable.rb', line 143

def each(&block)
  step(1, &block)
end

#element_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



147
148
149
# File 'lib/puppet/pops/types/iterable.rb', line 147

def element_type
  PAnyType::DEFAULT
end

#hash_style?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


168
169
170
# File 'lib/puppet/pops/types/iterable.rb', line 168

def hash_style?
  false
end

#reverse_each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


151
152
153
154
155
# File 'lib/puppet/pops/types/iterable.rb', line 151

def reverse_each(&block)
  # Default implementation cannot propagate reverse_each to a new enumerator so chained
  # calls must put reverse_each last.
  raise ArgumentError, 'reverse_each() is not implemented'
end

#step(step, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (ArgumentError)


157
158
159
160
161
# File 'lib/puppet/pops/types/iterable.rb', line 157

def step(step, &block)
  # Default implementation cannot propagate step to a new enumerator so chained
  # calls must put stepping last.
  raise ArgumentError, 'step() is not implemented'
end

#to_aObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



163
164
165
166
# File 'lib/puppet/pops/types/iterable.rb', line 163

def to_a
  raise Puppet::Error, 'Attempt to create an Array from an unbounded Iterable' if unbounded?
  super
end

#unbounded?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


172
173
174
# File 'lib/puppet/pops/types/iterable.rb', line 172

def unbounded?
  true
end