class Puppet::Pops::Types::Iterable::DepthFirstTreeIterator

Public Class Methods

new(enum, options = EMPTY_HASH) click to toggle source

Creates a DepthFirstTreeIterator that by default treats all Array, Hash and Object instances as containers - the 'containers' option can be set to a type that denotes which types of values should be treated as containers - a `Variant[Array, Hash]` would for instance not treat Object values as containers, whereas just `Object` would only treat objects as containers.

@param [Hash] options the options @option options [PTypeType] :containers ('Variant[Hash, Array, Object]') The type(s) that should be treated as containers @option options [Boolean] :with_root ('true') If the root container itself should be included in the iteration

    # File lib/puppet/pops/types/tree_iterators.rb
136 def initialize(enum, options = EMPTY_HASH)
137   super
138 end

Public Instance Methods

next() click to toggle source
    # File lib/puppet/pops/types/tree_iterators.rb
140 def next
141   loop do
142     break if @value_stack.empty?
143 
144     # first call
145     if @indexer_stack.empty?
146       @indexer_stack << indexer_on(@root)
147       @recursed = true
148       return [[], @root] if @with_root
149     end
150 
151     begin
152       if @recursed
153         @current_path << nil
154         @recursed = false
155       end
156       idx = @indexer_stack[-1].next
157       @current_path[-1] = idx
158       v = @value_stack[-1]
159       value = v.is_a?(PuppetObject) ? v.send(idx) : v[idx]
160       indexer = indexer_on(value)
161       if indexer
162         # recurse
163         @recursed = true
164         @value_stack << value
165         @indexer_stack << indexer
166         redo unless @with_containers
167       else
168         redo unless @with_values
169       end
170       return [@current_path.dup, value]
171 
172     rescue StopIteration
173       # end of current value's range of content
174       # pop all until out of next values
175       at_the_very_end = false
176       loop do
177         pop_level
178         at_the_very_end = @indexer_stack.empty?
179         break if at_the_very_end || has_next?(@indexer_stack[-1])
180       end
181     end
182   end
183   raise StopIteration
184 end

Private Instance Methods

pop_level() click to toggle source
    # File lib/puppet/pops/types/tree_iterators.rb
186 def pop_level
187   @value_stack.pop
188   @indexer_stack.pop
189   @current_path.pop
190 end