Class: JmeterPerf::Helpers::String
- Inherits:
-
Object
- Object
- JmeterPerf::Helpers::String
- Defined in:
- lib/jmeter_perf/helpers/string.rb
Class Method Summary collapse
-
.camelize(str, uppercase_first_letter = true) ⇒ String
Converts a string to CamelCase or camelCase.
-
.classify(string) ⇒ String
Converts a string to CamelCase.
-
.strip_heredoc(string) ⇒ String
Strips leading whitespace from each line that is the same as the amount of whitespace on the first line.
-
.underscore(string) ⇒ String
Converts a string to snake_case.
Class Method Details
.camelize(str, uppercase_first_letter = true) ⇒ String
Converts a string to CamelCase or camelCase.
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.
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.
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.
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 |