Class: Strings::Case

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(acronyms: nil) ⇒ Case

Create a Strings::Case instance

Examples:

strings = Strings::Case.new(acronyms: %w[HTTP XML])

Parameters:

  • acronyms (Array<String>) (defaults to: nil)

    the acronyms to use to prevent modifications



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

Examples:

camelcase("foo bar baz") # => "fooBarBaz"

Parameters:

  • string (String)

    the string to convert to camel case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "")

    the words separator, by default an empty string

Returns:

  • (String)


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

#configStrings::Case::Configuration

Access configuration

Examples:

strings.config

Returns:



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

Examples:

strings = Strings::Case.new
strings.configure do |config|
  config.acronym "HTTP"
end
strings = Strings::Case.new
strings.configure(acronyms: %w[HTTP XML])

Parameters:

  • acronyms (Array<String>) (defaults to: nil)

    the acronyms to use to prevent modifications

Yields:



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

Examples:

constantcase("foo bar baz") # => "FOO_BAR_BAZ"

Parameters:

  • string (String)

    the string to convert to constant case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "_")

    the words separator, by default an underscore “_”

Returns:

  • (String)


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

Examples:

headercase("foo bar baz") # = "Foo-Bar-Baz"

Parameters:

  • string (String)

    the string to convert to header case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "-")

    the words separator, by default a hyphen “-”

Returns:

  • (String)


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

Examples:

kebabcase("fooBarBaz") # => "foo-bar-baz"
kebabcase("__FOO_BAR__") # => "foo-bar"

Parameters:

  • string (String)

    the string to convert to kebab case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "-")

    the words separator, by default a hyphen “-”

Returns:

  • (String)


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

Examples:

pascalcase("foo bar baz") # => "FooBarBaz"

Parameters:

  • string (String)

    the string to convert to Pascal case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "")

    the words separator, by default an empty string

Returns:

  • (String)


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

Examples:

pathcase("foo bar baz") # => "foo/bar/baz"
pathcase("FooBarBaz") # => "foo/bar/baz"

Parameters:

  • string (String)

    the string to convert to path case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "/")

    the words separator, by default a forward slash “/”

Returns:

  • (String)


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

Examples:

sentencecase("foo bar baz") # => "Foo bar baz"

Parameters:

  • string (String)

    the string to convert to sentence case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: " ")

    the words separator, by default a space

Returns:

  • (String)


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

Examples:

snakecase("foo bar baz") # => "foo_bar_baz"
snakecase("ЗдравствуйтеПривет") # => "здравствуйте_привет"
snakecase("HTTPResponse") # => "http_response"

Parameters:

  • string (String)

    the string to convert to snake case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "_")

    the words separator, by default an underscore “_”

Returns:

  • (String)


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

Examples:

titlecase("foo bar baz") # => "Foo Bar Baz"

Parameters:

  • string (String)

    the string to convert to title case

  • acronyms (Array<String>) (defaults to: config.acronyms)

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: " ")

    the words separator, by default a space

Returns:

  • (String)


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