Class: JmeterPerf::Helpers::String

Inherits:
Object
  • Object
show all
Defined in:
lib/jmeter_perf/helpers/string.rb

Class Method Summary collapse

Class Method Details

.camelize(str, uppercase_first_letter = true) ⇒ String

Converts a string to CamelCase or camelCase.

Parameters:

  • str (String)

    the string to be converted.

  • uppercase_first_letter (Boolean, Symbol) (defaults to: true)

    whether to capitalize the first letter or not. If ‘:lower`, the first letter will be lowercase.

Returns:

  • (String)

    the CamelCase or camelCase version of the string.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jmeter_perf/helpers/string.rb', line 30

def camelize(str, uppercase_first_letter = true)
  string = str.dup
  # String#camelize takes a symbol (:upper or :lower), so here we also support :lower to keep the methods consistent.
  if !uppercase_first_letter || uppercase_first_letter == :lower
    string = string.sub(/^(?:(?=a)b(?=\b|[A-Z_])|\w)/) { |match| match.downcase! || match }
  elsif string.match?(/\A[a-z\d]*\z/)
    return string.capitalize
  else
    string = string.sub(/^[a-z\d]*/) { |match| match.capitalize || match }
  end

  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
    word = $2
    substituted = word.capitalize! || word
    ($1) ? "_#{substituted}" : substituted
  end

  string.gsub!(/[^a-zA-Z0-9]/, "")
  string
end

.classify(string) ⇒ String

Converts a string to CamelCase.

Parameters:

  • string (String)

    the string to be converted.

Returns:

  • (String)

    the CamelCase version of the string.



8
9
10
# File 'lib/jmeter_perf/helpers/string.rb', line 8

def classify(string)
  camelize(string.gsub(/\s/, "_"))
end

.strip_heredoc(string) ⇒ String

Strips leading whitespace from each line that is the same as the amount of whitespace on the first line.

Parameters:

  • string (String)

    the string to be stripped of leading whitespace.

Returns:

  • (String)

    the string with leading whitespace removed.



55
56
57
# File 'lib/jmeter_perf/helpers/string.rb', line 55

def strip_heredoc(string)
  string.gsub(/^#{string[/\A\s*/]}/, "")
end

.underscore(string) ⇒ String

Converts a string to snake_case.

Parameters:

  • string (String)

    the string to be converted.

Returns:

  • (String)

    the snake_case version of the string.



16
17
18
19
20
21
22
# File 'lib/jmeter_perf/helpers/string.rb', line 16

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