Class: NxtVcrHarness::CassetteNameByExample

Inherits:
Object
  • Object
show all
Defined in:
lib/nxt_vcr_harness/cassette_name_by_example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ CassetteNameByExample

Returns a new instance of CassetteNameByExample.



3
4
5
# File 'lib/nxt_vcr_harness/cassette_name_by_example.rb', line 3

def initialize(example)
  @example = example
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



7
8
9
# File 'lib/nxt_vcr_harness/cassette_name_by_example.rb', line 7

def example
  @example
end

Instance Method Details

#call(**options) ⇒ Object



9
10
11
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# 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_descriptionsObject



26
27
28
29
# 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_hierarchyObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nxt_vcr_harness/cassette_name_by_example.rb', line 50

def context_hierarchy
  @context_hierarchy ||= begin
    context_hierarchy = [example.]
    example_group = example.[: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_tagsObject



39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



31
32
33
# File 'lib/nxt_vcr_harness/cassette_name_by_example.rb', line 31

def gsub_whitespace(name)
  name.gsub(/\s+/, '_')
end

#remove_hash_tags(name) ⇒ Object



35
36
37
# File 'lib/nxt_vcr_harness/cassette_name_by_example.rb', line 35

def remove_hash_tags(name)
  name.delete('#')
end