Module: Subby::CaseChanger

Defined in:
lib/subby/case_changer.rb

Overview

CaseChanger is a module for converting strings from one case to another.

Class Method Summary collapse

Class Method Details

.to_camelcase(str) ⇒ String

Convert string to camel-case.

Examples:

CaseChanger.to_camelcase("apple pie") # => "applePie"

18
19
20
# File 'lib/subby/case_changer.rb', line 18

def self.to_camelcase( str )
  str.to_s.camelize(:lower)
end

.to_classcase(str) ⇒ String

Convert string to class-case. to_classcase is also aliased as to_modulecase.

Examples:

CaseChanger.to_classcase("applePie") # => "ApplePie"

28
29
30
# File 'lib/subby/case_changer.rb', line 28

def self.to_classcase( str )
  str.to_s.camelize(:upper)
end

.to_constantcase(str) ⇒ String

Convert string to constant-case.

Examples:

CaseChanger.to_constantcase("applePie") # => "APPLE_PIE"

37
38
39
# File 'lib/subby/case_changer.rb', line 37

def self.to_constantcase( str )
  str.to_s.underscore.upcase
end

.to_dashcase(str) ⇒ String

Convert string to dash-case.

Examples:

CaseChanger.to_constantcase("applePie") # => "apple-pie"

46
47
48
# File 'lib/subby/case_changer.rb', line 46

def self.to_dashcase( str )
  str.to_s.underscore.dasherize
end

.to_lowercase(str) ⇒ String

Convert string to lower-case.

Examples:

CaseChanger.to_lowercase("applePie") # => "apple pie"

55
56
57
# File 'lib/subby/case_changer.rb', line 55

def self.to_lowercase( str )
  str.to_s.underscore.tr('_', ' ')
end

.to_sentencecase(str) ⇒ String

Convert string to sentence-case.

Examples:

CaseChanger.to_sentencecase("applePie") # => "Apple pie"

64
65
66
# File 'lib/subby/case_changer.rb', line 64

def self.to_sentencecase( str )
  str.to_s.underscore.tr('_', ' ').capitalize
end

.to_snakecase(str) ⇒ String

Convert string to underscore-case. to_snakecase is also aliased as to_underscorecase.

Examples:

CaseChanger.to_underscorecase("applePie") # => "apple_pie"

74
75
76
# File 'lib/subby/case_changer.rb', line 74

def self.to_snakecase( str )
  str.to_s.underscore
end

.to_titlecase(str) ⇒ String

Convert string to title-case.

Examples:

CaseChanger.to_titlecase("applePie") # => "Apple Pie"

83
84
85
# File 'lib/subby/case_changer.rb', line 83

def self.to_titlecase( str )
  str.to_s.underscore.tr('_', ' ').split.map(&:capitalize).join(' ')
end

.to_uppercase(str) ⇒ String

Convert string to upper-case.

Examples:

CaseChanger.to_uppercase("applePie") # => "APPLE PIE"

92
93
94
# File 'lib/subby/case_changer.rb', line 92

def self.to_uppercase( str )
  str.to_s.underscore.tr('_', ' ').upcase
end