class Puppet::Pops::Types::Iterable::TreeIterator
Constants
- DEFAULT_CONTAINERS
Public Class Methods
Creates a TreeIterator
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.
Unrecognized options are silently ignored
@param [Hash] options the options @option options [PTypeType] :container_type ('Variant[Hash, Array, Object]') The type(s) that should be treated as containers. The
given type(s) must be assignable to the default container_type.
@option options [Boolean] :include_root ('true') If the root container itself should be included in the iteration (requires
`include_containers` to also be `true` to take effect).
@option options [Boolean] :include_containers ('true') If containers should be included in the iteration @option options [Boolean] :include_values ('true') If non containers (values) should be included in the iteration @option options [Boolean] :include_refs ('false') If (non containment) referenced values in Objects should be included
# File lib/puppet/pops/types/tree_iterators.rb 29 def initialize(enum, options=EMPTY_HASH) 30 @root = enum 31 @element_t = nil 32 @value_stack = [enum] 33 @indexer_stack = [] 34 @current_path = [] 35 @recursed = false 36 @containers_t = options['container_type'] || DEFAULT_CONTAINERS 37 unless DEFAULT_CONTAINERS.assignable?(@containers_t) 38 raise ArgumentError, _("Only Array, Hash, and Object types can be used as container types. Got %{type}") % {type: @containers_t} 39 end 40 @with_root = extract_option(options, 'include_root', true) 41 @with_containers = extract_option(options, 'include_containers', true) 42 @with_values = extract_option(options, 'include_values', true) 43 @with_root = @with_containers && extract_option(options, 'include_root', true) 44 unless @with_containers || @with_values 45 raise ArgumentError, _("Options 'include_containers' and 'include_values' cannot both be false") 46 end 47 @include_refs = !!options['include_refs'] 48 end
Public Instance Methods
Yields each `path, value` if the block arity is 2, and only `value` if arity is 1
# File lib/puppet/pops/types/tree_iterators.rb 52 def each(&block) 53 loop do 54 if block.arity == 1 55 yield(self.next) 56 else 57 yield(*self.next) 58 end 59 end 60 end
# File lib/puppet/pops/types/tree_iterators.rb 82 def reverse_each(&block) 83 r = Iterator.new(PAnyType::DEFAULT, to_array.reverse_each) 84 block_given? ? r.each(&block) : r 85 end
# File lib/puppet/pops/types/tree_iterators.rb 62 def size 63 raise "Not yet implemented - computes size lazily" 64 end
# File lib/puppet/pops/types/tree_iterators.rb 87 def step(step, &block) 88 r = StepIterator.new(PAnyType::DEFAULT, self, step) 89 block_given? ? r.each(&block) : r 90 end
# File lib/puppet/pops/types/tree_iterators.rb 70 def to_a 71 result = [] 72 loop do 73 result << self.next 74 end 75 result 76 end
# File lib/puppet/pops/types/tree_iterators.rb 78 def to_array 79 to_a 80 end
# File lib/puppet/pops/types/tree_iterators.rb 66 def unbounded? 67 false 68 end
Private Instance Methods
# File lib/puppet/pops/types/tree_iterators.rb 118 def extract_option(options, key, default) 119 v = options[key] 120 v.nil? ? default : !!v 121 end
# File lib/puppet/pops/types/tree_iterators.rb 108 def has_next?(iterator) 109 begin 110 iterator.peek 111 true 112 rescue StopIteration 113 false 114 end 115 end
# File lib/puppet/pops/types/tree_iterators.rb 92 def indexer_on(val) 93 return nil unless @containers_t.instance?(val) 94 if val.is_a?(Array) 95 val.size.times 96 elsif val.is_a?(Hash) 97 val.each_key 98 else 99 if @include_refs 100 val._pcore_type.attributes.each_key 101 else 102 val._pcore_type.attributes.reject {|k,v| v.kind == PObjectType::ATTRIBUTE_KIND_REFERENCE }.each_key 103 end 104 end 105 end