Class: Strings::Case
- Inherits:
-
Object
- Object
- Strings::Case
- Extended by:
- Forwardable
- Defined in:
- lib/strings/case.rb,
lib/strings/case/version.rb,
lib/strings/case/acronyms.rb,
lib/strings/case/extensions.rb,
lib/strings/case/configuration.rb
Overview
Responsible for converting strings to common cases
Defined Under Namespace
Modules: Extensions Classes: Acronyms, Configuration, Error
Constant Summary collapse
- VERSION =
"0.4.0"
Instance Method Summary collapse
-
#camelcase(string, acronyms: config.acronyms, separator: "") ⇒ String
(also: #lower_camelcase)
Convert string to camel case.
-
#config ⇒ Strings::Case::Configuration
Access configuration.
-
#configure(acronyms: nil) {|Strings::Case::Configuration| ... } ⇒ void
Configure acronyms.
-
#constcase(string, acronyms: config.acronyms, separator: "_") ⇒ String
(also: #constantcase)
Convert string to constant case.
-
#headercase(string, acronyms: config.acronyms, separator: "-") ⇒ String
Convert string to header case.
-
#initialize(acronyms: nil) ⇒ Case
constructor
Create a Strings::Case instance.
-
#kebabcase(string, acronyms: config.acronyms, separator: "-") ⇒ String
(also: #dashcase)
Convert string to kebab case.
-
#pascalcase(string, acronyms: config.acronyms, separator: "") ⇒ String
(also: #upper_camelcase)
Convert string to Pascal case.
-
#pathcase(string, acronyms: config.acronyms, separator: "/") ⇒ String
Convert string to path case.
-
#sentencecase(string, acronyms: config.acronyms, separator: " ") ⇒ String
Convert string to sentence case.
-
#snakecase(string, acronyms: config.acronyms, separator: "_") ⇒ String
(also: #underscore)
Convert string to snake case.
-
#titlecase(string, acronyms: config.acronyms, separator: " ") ⇒ String
Convert string to title case.
Constructor Details
#initialize(acronyms: nil) ⇒ Case
Create a Strings::Case instance
77 78 79 |
# File 'lib/strings/case.rb', line 77 def initialize(acronyms: nil) configure(acronyms: acronyms) end |
Instance Method Details
#camelcase(string, acronyms: config.acronyms, separator: "") ⇒ String Also known as: lower_camelcase
Convert string to camel case
136 137 138 139 140 141 |
# File 'lib/strings/case.rb', line 136 def camelcase(string, acronyms: config.acronyms, separator: "") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator) do |word, i| acronyms.fetch(word) || (i.zero? ? word.downcase : word.capitalize) end end |
#config ⇒ Strings::Case::Configuration
Access configuration
89 90 91 |
# File 'lib/strings/case.rb', line 89 def config @config ||= Configuration.new end |
#configure(acronyms: nil) {|Strings::Case::Configuration| ... } ⇒ void
This method returns an undefined value.
Configure acronyms
113 114 115 116 117 118 119 |
# File 'lib/strings/case.rb', line 113 def configure(acronyms: nil) if block_given? yield config else config.acronym(*acronyms) end end |
#constcase(string, acronyms: config.acronyms, separator: "_") ⇒ String Also known as: constantcase
Convert string to constant case
159 160 161 162 |
# File 'lib/strings/case.rb', line 159 def constcase(string, acronyms: config.acronyms, separator: "_") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator, &:upcase) end |
#headercase(string, acronyms: config.acronyms, separator: "-") ⇒ String
Convert string to header case
180 181 182 183 184 185 |
# File 'lib/strings/case.rb', line 180 def headercase(string, acronyms: config.acronyms, separator: "-") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator) do |word| (acronym = acronyms.fetch(word)) ? acronym : word.capitalize end end |
#kebabcase(string, acronyms: config.acronyms, separator: "-") ⇒ String Also known as: dashcase
Convert string to kebab case
205 206 207 208 |
# File 'lib/strings/case.rb', line 205 def kebabcase(string, acronyms: config.acronyms, separator: "-") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator, &:downcase) end |
#pascalcase(string, acronyms: config.acronyms, separator: "") ⇒ String Also known as: upper_camelcase
Convert string to Pascal case
226 227 228 229 230 231 |
# File 'lib/strings/case.rb', line 226 def pascalcase(string, acronyms: config.acronyms, separator: "") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator) do |word| acronyms.fetch(word) || word.capitalize end end |
#pathcase(string, acronyms: config.acronyms, separator: "/") ⇒ String
Convert string to path case
252 253 254 255 |
# File 'lib/strings/case.rb', line 252 def pathcase(string, acronyms: config.acronyms, separator: "/") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator, &:downcase) end |
#sentencecase(string, acronyms: config.acronyms, separator: " ") ⇒ String
Convert string to sentence case
272 273 274 275 276 277 |
# File 'lib/strings/case.rb', line 272 def sentencecase(string, acronyms: config.acronyms, separator: " ") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator) do |word, i| acronyms.fetch(word) || (i.zero? ? word.capitalize : word.downcase) end end |
#snakecase(string, acronyms: config.acronyms, separator: "_") ⇒ String Also known as: underscore
Convert string to snake case
300 301 302 303 |
# File 'lib/strings/case.rb', line 300 def snakecase(string, acronyms: config.acronyms, separator: "_") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator, &:downcase) end |
#titlecase(string, acronyms: config.acronyms, separator: " ") ⇒ String
Convert string to title case
321 322 323 324 325 326 |
# File 'lib/strings/case.rb', line 321 def titlecase(string, acronyms: config.acronyms, separator: " ") acronyms = Acronyms.from(acronyms) parsecase(string, acronyms: acronyms, sep: separator) do |word| acronyms.fetch(word) || word.capitalize end end |