module Dockerspec::Helper::RSpecExampleHelpers

Some Helper methods to work with RSpec Examples.

Public Class Methods

metadata_has_parent?(metadata) click to toggle source

Checks if the parent RSpec example information exists in the metadata.

@param metadata [Hash] RSpec metadata.

@return [Boolean] Returns true if the parent metadata is available.

@api private

# File lib/dockerspec/helper/rspec_example_helpers.rb, line 35
def self.metadata_has_parent?(metadata)
  metadata.key?(:parent_example_group) || metadata.key?(:example_group)
end
metadata_parent(metadata) click to toggle source

Get the parent RSpec example metadata if available.

@param metadata [Hash] RSpec metadata.

@return [Hash] RSpec metadata from the parent example.

@api private

# File lib/dockerspec/helper/rspec_example_helpers.rb, line 48
def self.metadata_parent(metadata)
  if metadata.key?(:parent_example_group)
    metadata[:parent_example_group]
  elsif metadata.key?(:example_group)
    metadata[:example_group]
  end
end
restore_rspec_context(metadata) click to toggle source

Restores the Docker running container instance in the Specinfra internal reference.

Gets the correct {Runner::Base} reference from the RSpec metadata.

@example Restore Specinfra Backend

RSpec.configure do |c|
  c.before(:each) do
    metadata = RSpec.current_example.metadata
    Dockerspec::Runner::Base.restore_rspec_context(metadata)
  end
end

@param metadata [Hash] RSpec metadata.

@return void

@api public

@see restore

# File lib/dockerspec/helper/rspec_example_helpers.rb, line 100
def self.restore_rspec_context(metadata)
  o_ary =
    Helper::RSpecExampleHelpers
    .search_objects_with(metadata, :restore_rspec_context)
  o_ary.each(&:restore_rspec_context)
end
search_objects_with(metadata, meth) click to toggle source

Searches for an object in the description of the parent RSpec examples that implements a specific method.

@param metadata [Hash] RSpec metadata. @param meth [Symbol] The method name.

@return [Array<Object>] Returns the objects list.

@api public

# File lib/dockerspec/helper/rspec_example_helpers.rb, line 67
def self.search_objects_with(metadata, meth)
  o_ary = []
  return o_ary if metadata.nil?
  if metadata[:described_class].respond_to?(meth) &&
     metadata[:described_class] != self
    o_ary << metadata[:described_class]
  end
  return o_ary unless metadata_has_parent?(metadata)
  (search_objects_with(metadata_parent(metadata), meth) + o_ary).uniq
end