Sha256: 05eb462f7dc93c45ccc5b7502a6ff88b51855efbfef3301b481ed5840f3392d6

Contents?: true

Size: 1.23 KB

Versions: 30

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Kitchen
  module Mixins
    # A mixin for including the block_error_if method
    #
    # @example
    #   class SomeClass
    #     include Mixins::BlockErrorIf
    #
    #     def foo
    #       block_error_if(block_given?)
    #     end
    #   end
    module BlockErrorIf
      # All Ruby methods can take blocks, but not all of them use the block.  If a block is given but
      # not expected, we want to raise an error to help the developer figure out why their block isn't
      # doing what they expect.  The method does some work to figure out where the block was errantly
      # given to help the developer find the errant line of code.
      #
      # @param block_given [Boolean] true if block was given
      # @raise [RecipeError] if a block was given
      #
      def block_error_if(block_given)
        return unless block_given

        calling_method = begin
          this_method_location_index = caller_locations.find_index do |location|
            location.label == 'block_error_if'
          end

          caller_locations[(this_method_location_index || -1) + 1].label
        end

        raise(RecipeError, "The `#{calling_method}` method does not take a block")
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
openstax_kitchen-19.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-18.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-17.1.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-17.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-16.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-15.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-14.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-13.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-12.2.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-12.1.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-12.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-11.2.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-11.1.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-11.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-10.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-9.2.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-9.1.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-9.0.0 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-8.0.1 lib/kitchen/mixins/block_error_if.rb
openstax_kitchen-8.0.0 lib/kitchen/mixins/block_error_if.rb