class AwsCftTools::TemplateSet::EachSliceState

Keeps track of state for the .each_slice(n) method.

Public Class Methods

new(slice_size, &block) click to toggle source

@param slice_size [Integer] maximum number of templates to yield at once @yield [Array<AwsCftTools::Template>]

# File lib/aws_cft_tools/template_set/each_slice_state.rb, line 13
def initialize(slice_size, &block)
  @seen = []
  @size = slice_size
  @slice = []
  @block = block
end

Public Instance Methods

add_template(template, dependencies = []) click to toggle source

Add the template to the current slice and process the slice if it reaches the maximum slice size.

@param template [AwsCftTools::Template]

# File lib/aws_cft_tools/template_set/each_slice_state.rb, line 35
def add_template(template, dependencies = [])
  process_slice unless fulfilled?(dependencies)
  unless fulfilled?(dependencies)
    raise AwsCftTools::UnsatisfiedDependencyError, "Unable to process #{template.filename}"
  end

  @slice << template

  process_slice if @slice.count == @size
end
fulfilled?(deps) click to toggle source

Have all of the listed dependencies been seen in prior yields?

@param deps [Array<String>] @return [Boolean]

# File lib/aws_cft_tools/template_set/each_slice_state.rb, line 26
def fulfilled?(deps)
  (deps - @seen).empty?
end
process_slice() click to toggle source

Pass the current slice through the block and reset for the next slice.

@return [Integer] number of templates processed in this batch

# File lib/aws_cft_tools/template_set/each_slice_state.rb, line 51
def process_slice
  @block.call(@slice) if @slice.any?
  @seen |= @slice.map(&:filename).map(&:to_s)
  @slice = []
end