Module: Jsus::Util::Inflection
- Defined in:
- lib/jsus/util/inflection.rb
Overview
Various inflection helpers
Class Method Summary collapse
-
.capitalize(string) ⇒ Object
Capitalizes first letter (doesn't do anything else to other letters, unlike String#capitalize).
-
.decapitalize(string) ⇒ Object
Downcases first letter.
-
.random_case_to_mixed_case(string) ⇒ Object
Converts strings with various punctuation to pascal case.
-
.random_case_to_mixed_case_preserve_dots(string) ⇒ Object
Same as #random_case_to_mixed_case, but preserves dots.
-
.snake_case(string) ⇒ Object
Translates MixedCase string to camel-case.
Class Method Details
.capitalize(string) ⇒ Object
Capitalizes first letter (doesn't do anything else to other letters, unlike String#capitalize)
27 28 29 |
# File 'lib/jsus/util/inflection.rb', line 27 def capitalize(string) string[0,1].capitalize + string[1..-1].to_s end |
.decapitalize(string) ⇒ Object
Downcases first letter
33 34 35 |
# File 'lib/jsus/util/inflection.rb', line 33 def decapitalize(string) string[0,1].downcase + string[1..-1].to_s end |
.random_case_to_mixed_case(string) ⇒ Object
Converts strings with various punctuation to pascal case
13 14 15 |
# File 'lib/jsus/util/inflection.rb', line 13 def random_case_to_mixed_case(string) string.split(/[^a-zA-Z]+/).map {|chunk| capitalize(chunk) }.join end |
.random_case_to_mixed_case_preserve_dots(string) ⇒ Object
Same as #random_case_to_mixed_case, but preserves dots
21 22 23 |
# File 'lib/jsus/util/inflection.rb', line 21 def random_case_to_mixed_case_preserve_dots(string) string.split(".").map {|c| random_case_to_mixed_case(c) }.join(".") end |
.snake_case(string) ⇒ Object
Translates MixedCase string to camel-case
39 40 41 42 |
# File 'lib/jsus/util/inflection.rb', line 39 def snake_case(string) decapitalize(string.gsub(/(.)([A-Z])([a-z]+)/) {|_| "#{$1}_#{$2.downcase}#{$3}"}. gsub(/[^A-Za-z_]+/, "_")) end |