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(caller, obj) ⇒ Iterable?
Produces an ‘Iterable` for one of the following types with the following characterstics:.
-
.on(o) ⇒ Iterable?
Produces an ‘Iterable` for one of the following types with the following characterstics:.
-
.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
- #reverse_each(&block) ⇒ Object private
- #step(step, &block) ⇒ Object private
- #to_a ⇒ Object private
- #unbounded? ⇒ Boolean private
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.
22 23 24 25 26 |
# File 'lib/puppet/pops/types/iterable.rb', line 22 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) ⇒ 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
The value ‘nil` is returned for all other objects.
46 47 48 49 50 51 52 53 54 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 |
# File 'lib/puppet/pops/types/iterable.rb', line 46 def self.on(o) case o 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 tc = TypeCalculator.singleton Iterator.new(PVariantType.maybe_create(o.map {|e| tc.infer_set(e) }), o.each) end when Hash # Each element is a two element [key, value] tuple. if o.empty? Iterator.new(PHashType::DEFAULT_KEY_PAIR_TUPLE, o.each) else tc = TypeCalculator.singleton Iterator.new(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), 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 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.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/puppet/pops/types/iterable.rb', line 112 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.
124 125 126 |
# File 'lib/puppet/pops/types/iterable.rb', line 124 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.
128 129 130 |
# File 'lib/puppet/pops/types/iterable.rb', line 128 def element_type PAnyType::DEFAULT 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.
132 133 134 135 136 |
# File 'lib/puppet/pops/types/iterable.rb', line 132 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.
138 139 140 141 142 |
# File 'lib/puppet/pops/types/iterable.rb', line 138 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.
144 145 146 147 |
# File 'lib/puppet/pops/types/iterable.rb', line 144 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.
149 150 151 |
# File 'lib/puppet/pops/types/iterable.rb', line 149 def unbounded? true end |