class NxtVcrHarness::CassetteNameByExample

Attributes

example[R]

Public Class Methods

new(example) click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 3
def initialize(example)
  @example = example
end

Public Instance Methods

call(**options) click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 9
def call(**options)
  cassette_name_and_path(options)
end
cassette_name_and_path(options) click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 13
def cassette_name_and_path(options)
  path = []

  path << options[:prefix]
  spec_path = example.file_path.gsub(/\.rb$/, '').gsub('./spec/', '')

  path << spec_path
  path << cassette_name_from_descriptions
  path << options[:suffix]

  "/#{path.flatten.compact.join('/')}"
end
cassette_name_from_descriptions() click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 26
def cassette_name_from_descriptions
  descriptions = context_hierarchy_with_vcr_cassette_tags.map { |c| c[:description] }
  descriptions.map(&method(:gsub_whitespace)).map(&method(:remove_hash_tags))
end
context_hierarchy() click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 50
def context_hierarchy
  @context_hierarchy ||= begin
    context_hierarchy = [example.metadata]
    example_group = example.metadata[:example_group]
    context_hierarchy << example_group

    while parent_example_group = example_group[:parent_example_group] do
      context_hierarchy << parent_example_group
      example_group = parent_example_group
    end

    context_hierarchy
  rescue StandardError => e
    raise StandardError, "Failed to build context hierarchy for example: #{example} - Error was: #{e.inspect}"
  end
end
context_hierarchy_with_vcr_cassette_tags() click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 39
def context_hierarchy_with_vcr_cassette_tags
  @context_hierarchy_with_vcr_cassette_tags ||= begin
    context_hierarchy.reverse.inject([]) do |acc, context|
      acc << context
      break acc if context[:vcr_cassette]

      acc
    end
  end
end
gsub_whitespace(name) click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 31
def gsub_whitespace(name)
  name.gsub(/\s+/, '_')
end
remove_hash_tags(name) click to toggle source
# File lib/nxt_vcr_harness/cassette_name_by_example.rb, line 35
def remove_hash_tags(name)
  name.delete('#')
end