Class: Puppet::Pops::Types::Iterable::TreeIterator Private

Inherits:
Object
  • Object
show all
Includes:
Puppet::Pops::Types::Iterable
Defined in:
lib/puppet/pops/types/tree_iterators.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DEFAULT_CONTAINERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TypeFactory.variant(
  PArrayType::DEFAULT,
  PHashType::DEFAULT,
  PObjectType::DEFAULT
)

Instance Method Summary collapse

Methods included from Puppet::Pops::Types::Iterable

asserted_iterable, #element_type, #hash_style?, on, unbounded?

Constructor Details

#initialize(enum, options = EMPTY_HASH) ⇒ TreeIterator

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.

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

Parameters:

  • options (Hash) (defaults to: EMPTY_HASH)

    the options

Options Hash (options):

  • :container_type (PTypeType) — default: '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.

  • :include_root (Boolean) — default: 'true'

    If the root container itself should be included in the iteration (requires ‘include_containers` to also be `true` to take effect).

  • :include_containers (Boolean) — default: 'true'

    If containers should be included in the iteration

  • :include_values (Boolean) — default: 'true'

    If non containers (values) should be included in the iteration

  • :include_refs (Boolean) — default: 'false'

    If (non containment) referenced values in Objects should be included



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet/pops/types/tree_iterators.rb', line 30

def initialize(enum, options = EMPTY_HASH)
  @root = enum
  @element_t = nil
  @value_stack = [enum]
  @indexer_stack = []
  @current_path = []
  @recursed = false
  @containers_t = options['container_type'] || DEFAULT_CONTAINERS
  unless DEFAULT_CONTAINERS.assignable?(@containers_t)
    raise ArgumentError, _("Only Array, Hash, and Object types can be used as container types. Got %{type}") % { type: @containers_t }
  end

  @with_root       = extract_option(options, 'include_root', true)
  @with_containers = extract_option(options, 'include_containers', true)
  @with_values     = extract_option(options, 'include_values', true)
  @with_root       = @with_containers && extract_option(options, 'include_root', true)
  unless @with_containers || @with_values
    raise ArgumentError, _("Options 'include_containers' and 'include_values' cannot both be false")
  end

  @include_refs = !!options['include_refs']
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.

Yields each ‘path, value` if the block arity is 2, and only `value` if arity is 1



55
56
57
58
59
60
61
62
63
# File 'lib/puppet/pops/types/tree_iterators.rb', line 55

def each(&block)
  loop do
    if block.arity == 1
      yield(self.next)
    else
      yield(*self.next)
    end
  end
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.



85
86
87
88
# File 'lib/puppet/pops/types/tree_iterators.rb', line 85

def reverse_each(&block)
  r = Iterator.new(PAnyType::DEFAULT, to_array.reverse_each)
  block_given? ? r.each(&block) : r
end

#sizeObject

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.



65
66
67
# File 'lib/puppet/pops/types/tree_iterators.rb', line 65

def size
  raise "Not yet implemented - computes size lazily"
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.



90
91
92
93
# File 'lib/puppet/pops/types/tree_iterators.rb', line 90

def step(step, &block)
  r = StepIterator.new(PAnyType::DEFAULT, self, step)
  block_given? ? r.each(&block) : r
end

#to_aObject

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.



73
74
75
76
77
78
79
# File 'lib/puppet/pops/types/tree_iterators.rb', line 73

def to_a
  result = []
  loop do
    result << self.next
  end
  result
end

#to_arrayObject

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.



81
82
83
# File 'lib/puppet/pops/types/tree_iterators.rb', line 81

def to_array
  to_a
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.

Returns:

  • (Boolean)


69
70
71
# File 'lib/puppet/pops/types/tree_iterators.rb', line 69

def unbounded?
  false
end