Class: AwsCftTools::TemplateSet::EachSliceState

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cft_tools/template_set/each_slice_state.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(slice_size) {|Array<AwsCftTools::Template>| ... } ⇒ EachSliceState

Returns a new instance of EachSliceState.

Parameters:

  • slice_size (Integer)

    maximum number of templates to yield at once

Yields:



13
14
15
16
17
18
# 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

Instance Method Details

#add_template(template, dependencies = []) ⇒ Object

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

Parameters:



35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Boolean

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

Parameters:

  • deps (Array<String>)

Returns:

  • (Boolean)


26
27
28
# File 'lib/aws_cft_tools/template_set/each_slice_state.rb', line 26

def fulfilled?(deps)
  (deps - @seen).empty?
end

#process_sliceInteger

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

Returns:

  • (Integer)

    number of templates processed in this batch



51
52
53
54
55
# 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