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
-
.asserted_iterable(my_caller, obj, infer_elements = false) ⇒ Iterable?
Produces an ‘Iterable` for one of the following types with the following characterstics:.
-
.on(o, element_type = nil, infer_elements = true) ⇒ Iterable?
Produces an ‘Iterable` for one of the following types with the following characteristics:.
-
.unbounded?(object) ⇒ Boolean
private
Answers the question if there is an end to the iteration.
Instance Method Summary collapse
- #each(&block) ⇒ Object private
- #element_type ⇒ Object private
- #hash_style? ⇒ Boolean private
- #reverse_each(&block) ⇒ Object private
- #step(step, &block) ⇒ Object private
- #to_a ⇒ Object private
- #unbounded? ⇒ Boolean private
Class Method Details
.asserted_iterable(my_caller, obj, infer_elements = false) ⇒ 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.
34 35 36 37 38 39 |
# File 'lib/puppet/pops/types/iterable.rb', line 34 def self.asserted_iterable(my_caller, obj, infer_elements = false) iter = on(obj, nil, infer_elements) raise ArgumentError, "#{my_caller.class}(): wrong argument type (#{obj.class}; is not Iterable." if iter.nil? iter end |
.on(o, element_type = nil, infer_elements = true) ⇒ 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.
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 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/puppet/pops/types/iterable.rb', line 63 def self.on(o, element_type = nil, infer_elements = true) 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? && infer_elements 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? && infer_elements 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.
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/puppet/pops/types/iterable.rb', line 143 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.
155 156 157 |
# File 'lib/puppet/pops/types/iterable.rb', line 155 def each(&block) step(1, &block) end |
#element_type ⇒ 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.
159 160 161 |
# File 'lib/puppet/pops/types/iterable.rb', line 159 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.
181 182 183 |
# File 'lib/puppet/pops/types/iterable.rb', line 181 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.
163 164 165 166 167 |
# File 'lib/puppet/pops/types/iterable.rb', line 163 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.
169 170 171 172 173 |
# File 'lib/puppet/pops/types/iterable.rb', line 169 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_a ⇒ 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.
175 176 177 178 179 |
# File 'lib/puppet/pops/types/iterable.rb', line 175 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.
185 186 187 |
# File 'lib/puppet/pops/types/iterable.rb', line 185 def unbounded? true end |