Module: Insta::SnapshotName

Defined in:
lib/insta/snapshot_name.rb,
sig/insta/snapshot_name.rbs

Class Method Summary collapse

Class Method Details

.derive(test_name, counter: 1, options: {}) ⇒ String

: (String, ?counter: Integer, ?options: Hash[Symbol, untyped]) -> String

Parameters:

  • (String)
  • counter: (Integer) (defaults to: 1)
  • options: (Hash[Symbol, untyped]) (defaults to: {})

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/insta/snapshot_name.rb', line 38

def self.derive(test_name, counter: 1, options: {})
  stripped = strip_test_prefix(test_name)
  clean_name = sanitize(stripped)
  suffix = counter > 1 ? "-#{counter}" : ""

  if options && !options.empty?
    options_hash = Digest::MD5.hexdigest(options.inspect)
    "#{clean_name}#{suffix}-#{options_hash}"
  else
    "#{clean_name}#{suffix}"
  end
end

.sanitize(name) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (String)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/insta/snapshot_name.rb', line 8

def self.sanitize(name)
  sanitizer = Insta.configuration.snapshot_sanitizer

  if sanitizer
    sanitizer.call(name)
  else
    name
      .gsub(/[^a-zA-Z0-9_\-.]/, "_")
      .gsub(/_+/, "_")
      .gsub(/\A_|_\z/, "")
  end
end

.strip_test_prefix(name) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (String)


33
34
35
# File 'lib/insta/snapshot_name.rb', line 33

def self.strip_test_prefix(name)
  name.sub(/\Atest_(\d+_)?/, "")
end

.underscore(string) ⇒ String

: (String) -> String

Parameters:

  • (String)

Returns:

  • (String)


22
23
24
25
26
27
28
29
30
# File 'lib/insta/snapshot_name.rb', line 22

def self.underscore(string)
  string
    .gsub("::", "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .tr(" ", "_")
    .downcase
end