class SleepingKingStudios::Tools::StringTools

Tools for working with strings.

Attributes

inflector[R]

Public Class Methods

new(inflector: nil) click to toggle source

@param inflector [Object] An object that conforms to the interface used

by SleepingKingStudios::Tools::Toolbox::Inflector, such as
ActiveSupport::Inflector .
Calls superclass method
# File lib/sleeping_king_studios/tools/string_tools.rb, line 30
def initialize(inflector: nil)
  super()

  @inflector =
    inflector || SleepingKingStudios::Tools::Toolbox::Inflector.new
end

Public Instance Methods

camelize(str) click to toggle source

Converts a lowercase, underscore-separated string to CamelCase.

@param str [String] The string to convert.

@return [String] The converted string.

@see ActiveSupport::Inflector#camelize.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 46
def camelize(str)
  str = require_string! str

  inflector.camelize(str)
end
chain(str, *commands) click to toggle source

Performs multiple string tools operations in sequence, starting with the given string and passing the result of each operation to the next.

@param str [String] The string to process. @param commands [Array<String, Symbol>] The string operations to apply.

@return [String] The processed string.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 59
def chain(str, *commands)
  str = require_string! str

  commands.reduce(str) { |memo, command| send(command, memo) }
end
indent(str, count = 2) click to toggle source

Adds the specified number of spaces to the start of each line of the string. Defaults to 2 spaces.

@param str [String] The string to indent. @param count [Integer] The number of spaces to add.

@return [String] The indented string.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 72
def indent(str, count = 2)
  str = require_string! str
  pre = ' ' * count

  map_lines(str) { |line| "#{pre}#{line}" }
end
map_lines(str) { |line, index| ... } click to toggle source

Yields each line of the string to the provided block and combines the results into a new multiline string.

@param str [String] The string to map.

@yieldparam line [String] The current line. @yieldparam index [Integer] The index of the current line.

@return [String] The mapped string.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 88
def map_lines(str)
  str = require_string! str

  str.each_line.with_index.reduce(+'') do |memo, (line, index)|
    memo << yield(line, index)
  end
end
plural?(word) click to toggle source

Determines whether or not the given word is in plural form. If calling pluralize(word) is equal to word, the word is considered plural.

@return [Boolean] True if the word is in plural form, otherwise false.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 100
def plural?(word)
  word = require_string!(word)

  word == pluralize(word)
end
pluralize(*args) click to toggle source

@overload pluralize(str)

Takes a word in singular form and returns the plural form, based on the
defined rules and known irregular/uncountable words.

@param str [String] The word to pluralize.

@return [String] The pluralized word.
# File lib/sleeping_king_studios/tools/string_tools.rb, line 113
def pluralize(*args)
  str = require_string! args.first

  inflector.pluralize str
end
singular?(word) click to toggle source

Determines whether or not the given word is in singular form. If calling singularize(word) is equal to word, the word is considered singular.

@return [Boolean] True if the word is in singular form, otherwise false.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 123
def singular?(word)
  word = require_string!(word)

  word == singularize(word)
end
singularize(str) click to toggle source

Transforms the word to a singular, lowercase form.

@param str [String] The word to transform.

@return [String] The word in singular form.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 134
def singularize(str)
  require_string! str

  inflector.singularize str
end
string?(str) click to toggle source

Returns true if the object is a String.

@param str [Object] The object to test.

@return [Boolean] True if the object is a String, otherwise false.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 145
def string?(str)
  str.is_a?(String)
end
underscore(str) click to toggle source

Converts a mixed-case string expression to a lowercase, underscore separated string.

@param str [String] The string to convert.

@return [String] The converted string.

@see ActiveSupport::Inflector#underscore.

# File lib/sleeping_king_studios/tools/string_tools.rb, line 157
def underscore(str)
  str = require_string! str

  inflector.underscore(str)
end

Private Instance Methods

require_string!(value) click to toggle source
# File lib/sleeping_king_studios/tools/string_tools.rb, line 165
def require_string!(value)
  return value if string?(value)

  return value.to_s if value.is_a?(Symbol)

  raise ArgumentError, 'argument must be a string', caller[1..-1]
end