class Chef::ResourceCollection::StepableIterator

Attributes

collection[RW]
position[R]

Public Class Methods

for_collection(new_collection) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 22
def self.for_collection(new_collection)
  new(new_collection)
end
new(collection = []) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 29
def initialize(collection = [])
  @position = 0
  @paused = false
  @collection = collection
end

Public Instance Methods

each(&block) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 39
def each(&block)
  reset_iteration(block)
  @iterator_type = :element
  iterate
end
each_index(&block) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 45
def each_index(&block)
  reset_iteration(block)
  @iterator_type = :index
  iterate
end
each_with_index(&block) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 51
def each_with_index(&block)
  reset_iteration(block)
  @iterator_type = :element_with_index
  iterate
end
iterate_on(iteration_type, &block) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 89
def iterate_on(iteration_type, &block)
  @iterator_type = iteration_type
  @iterator_block = block
end
pause() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 61
def pause
  @paused = true
end
paused?() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 57
def paused?
  @paused
end
resume() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 65
def resume
  @paused = false
  iterate
end
rewind() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 70
def rewind
  @position = 0
end
size() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 35
def size
  collection.size
end
skip_back(skips = 1) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 74
def skip_back(skips = 1)
  @position -= skips
end
skip_forward(skips = 1) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 78
def skip_forward(skips = 1)
  @position += skips
end
step() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 82
def step
  return nil if @position == size

  call_iterator_block
  @position += 1
end

Private Instance Methods

call_iterator_block() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 107
def call_iterator_block
  case @iterator_type
  when :element
    @iterator_block.call(collection[@position])
  when :index
    @iterator_block.call(@position)
  when :element_with_index
    @iterator_block.call(collection[@position], @position)
  else
    raise "42error: someone forgot to set @iterator_type, wtf?"
  end
end
iterate() click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 102
def iterate
  step while @position < size && !paused?
  collection
end
reset_iteration(iterator_block) click to toggle source
# File lib/chef/resource_collection/stepable_iterator.rb, line 96
def reset_iteration(iterator_block)
  @iterator_block = iterator_block
  @position = 0
  @paused = false
end