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 30 def initialize(enum, options=EMPTY_HASH) 31 @root = enum 32 @element_t = nil 33 @value_stack = [enum] 34 @indexer_stack = [] 35 @current_path = [] 36 @recursed = false 37 @containers_t = options['container_type'] || DEFAULT_CONTAINERS 38 unless DEFAULT_CONTAINERS.assignable?(@containers_t) 39 raise ArgumentError, _("Only Array, Hash, and Object types can be used as container types. Got %{type}") % {type: @containers_t} 40 end 41 @with_root = extract_option(options, 'include_root', true) 42 @with_containers = extract_option(options, 'include_containers', true) 43 @with_values = extract_option(options, 'include_values', true) 44 @with_root = @with_containers && extract_option(options, 'include_root', true) 45 unless @with_containers || @with_values 46 raise ArgumentError, _("Options 'include_containers' and 'include_values' cannot both be false") 47 end 48 @include_refs = !!options['include_refs'] 49 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 53 def each(&block) 54 loop do 55 if block.arity == 1 56 yield(self.next) 57 else 58 yield(*self.next) 59 end 60 end 61 end
# File lib/puppet/pops/types/tree_iterators.rb 83 def reverse_each(&block) 84 r = Iterator.new(PAnyType::DEFAULT, to_array.reverse_each) 85 block_given? ? r.each(&block) : r 86 end
# File lib/puppet/pops/types/tree_iterators.rb 63 def size 64 raise "Not yet implemented - computes size lazily" 65 end
# File lib/puppet/pops/types/tree_iterators.rb 88 def step(step, &block) 89 r = StepIterator.new(PAnyType::DEFAULT, self, step) 90 block_given? ? r.each(&block) : r 91 end
# File lib/puppet/pops/types/tree_iterators.rb 71 def to_a 72 result = [] 73 loop do 74 result << self.next 75 end 76 result 77 end
# File lib/puppet/pops/types/tree_iterators.rb 79 def to_array 80 to_a 81 end
# File lib/puppet/pops/types/tree_iterators.rb 67 def unbounded? 68 false 69 end
Private Instance Methods
# File lib/puppet/pops/types/tree_iterators.rb 119 def extract_option(options, key, default) 120 v = options[key] 121 v.nil? ? default : !!v 122 end
# File lib/puppet/pops/types/tree_iterators.rb 109 def has_next?(iterator) 110 begin 111 iterator.peek 112 true 113 rescue StopIteration 114 false 115 end 116 end
# File lib/puppet/pops/types/tree_iterators.rb 93 def indexer_on(val) 94 return nil unless @containers_t.instance?(val) 95 if val.is_a?(Array) 96 val.size.times 97 elsif val.is_a?(Hash) 98 val.each_key 99 else 100 if @include_refs 101 val._pcore_type.attributes.each_key 102 else 103 val._pcore_type.attributes.reject {|k,v| v.kind == PObjectType::ATTRIBUTE_KIND_REFERENCE }.each_key 104 end 105 end 106 end