Class: SleepingKingStudios::Tools::StringTools

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/string_tools.rb

Overview

Tools for working with strings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

instance

Constructor Details

#initialize(inflector: nil) ⇒ StringTools

Returns a new instance of StringTools.

Parameters:

  • inflector (Object) (defaults to: nil)

    service object for inflecting strings. The inflector must be an object that conforms to the interface used by by SleepingKingStudios::Tools::Toolbox::Inflector, such as an instance of ActiveSupport::Inflector .



27
28
29
30
31
32
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 27

def initialize(inflector: nil)
  super()

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

Instance Attribute Details

#inflectorObject (readonly)

Returns service object for inflecting strings.

Returns:

  • (Object)

    service object for inflecting strings.



35
36
37
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 35

def inflector
  @inflector
end

Instance Method Details

#camelize(str) ⇒ String

Converts a lowercase, underscore-separated string to CamelCase.

Examples:

StringTools#camelize 'valhalla'
#=> 'Valhalla'

StringTools#camelize 'muspelheimr_and_niflheimr'
#=> 'MuspelheimrAndNiflheimr'

Parameters:

  • str (String)

    the string to convert.

Returns:

  • (String)

    the converted string.

See Also:

  • Toolbox::Inflector#camelize.


51
52
53
54
55
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 51

def camelize(str)
  str = require_string!(str)

  inflector.camelize(str)
end

#chain(str, *commands) ⇒ String

Performs a series of operations on the string.

Use #chain to call each specified method in the chain in sequence, passing the output of each method to the next method.

Examples:

# Equivalent to `StringTools.underscore(StringTools.pluralize str)`.
StringTools#chain 'ArchivedPeriodical', :underscore, :pluralize
# => 'archived_periodicals'

Parameters:

  • str (String)

    the string to process.

  • commands (Array<String, Symbol>)

    the string operations to apply.

Returns:

  • (String)

    the processed string.



71
72
73
74
75
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 71

def chain(str, *commands)
  str = require_string!(str)

  commands.reduce(str) { |memo, command| send(command, memo) }
end

#indent(str, count = 2) ⇒ String

Adds the specified number of spaces to the start of each line.

Examples:

string = 'The Hobbit'
StringTools.indent(string)
#=> '  The Hobbit'

titles = [
  "The Fellowship of the Ring",
  "The Two Towers",
  "The Return of the King"
]
string = titles.join "\n"
StringTools.indent(string, 4)
#=> "    The Fellowship of the Ring\n"\
    "    The Two Towers\n"\
    "    The Return of the King"

Parameters:

  • str (String)

    the string to indent.

  • count (Integer) (defaults to: 2)

    the number of spaces to add. Defaults to 2.

Returns:

  • (String)

    the indented string.



99
100
101
102
103
104
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 99

def indent(str, count = 2)
  str = require_string!(str)
  pre = ' ' * count

  map_lines(str) { |line| "#{pre}#{line}" }
end

#map_lines(str) {|line, index| ... } ⇒ String

Yields each line to the provided block and combines the results.

The results of each line are combined back into a new multi-line string.

Examples:

string = 'The Hobbit'
StringTools.map_lines(string) { |line| "  #{line}" }
#=> '- The Hobbit'

titles = [
  "The Fellowship of the Ring",
  "The Two Towers",
  "The Return of the King"
]
string = titles.join "\n"
StringTools.map_lines(string) { |line, index| "#{index}. #{line}" }
#=> "0. The Fellowship of the Ring\n"\
    "1. The Two Towers\n"\
    "2. The Return of the King"

Parameters:

  • str (String)

    the string to map.

Yield Parameters:

  • line (String)

    the current line.

  • index (Integer)

    the index of the current line.

Yield Returns:

  • (String)

    the modified line.

Returns:

  • (String)

    the mapped and recombined string.



134
135
136
137
138
139
140
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 134

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) ⇒ Boolean

Determines whether or not the given word is in plural form.

If calling #pluralize(word) is equal to word, the word is considered plural.

Examples:

StringTools.plural? 'light'
#=> false

StringTools.plural? 'lights'
#=> true

Parameters:

  • word (String)

    the word to check.

Returns:

  • (Boolean)

    true if the word is in plural form, otherwise false.

See Also:



159
160
161
162
163
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 159

def plural?(word)
  word = require_string!(word)

  word == pluralize(word)
end

#pluralize(str) ⇒ String

Takes a word in singular form and returns the plural form.

This method delegates to the configured inflector, which converts the given word based on the defined rules and known irregular/uncountable words.

Examples:

StringTools.pluralize 'light'
#=> 'lights'

Parameters:

  • str (String)

    the word to pluralize.

Returns:

  • (String)

    the pluralized word.

See Also:

  • Toolbox::Inflector#pluralize.


181
182
183
184
185
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 181

def pluralize(*args)
  str = require_string!(args.first)

  inflector.pluralize(str)
end

#singular?(word) ⇒ Boolean

Determines whether or not the given word is in singular form.

If calling #singularize(word) is equal to word, the word is considered singular.

Examples:

StringTools.singular? 'light'
#=> true

StringTools.singular? 'lights'
#=> false

Parameters:

  • word (String)

    the word to check.

Returns:

  • (Boolean)

    true if the word is in singular form, otherwise false.

See Also:



204
205
206
207
208
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 204

def singular?(word)
  word = require_string!(word)

  word == singularize(word)
end

#singularize(str) ⇒ String

Transforms the word to a singular, lowercase form.

This method delegates to the configured inflector, which converts the given word based on the defined rules and known irregular/uncountable words.

Examples:

StringTools.singularize 'lights'
#=> 'light'

Parameters:

  • str (String)

    the word to transform.

Returns:

  • (String)

    the word in singular form.

    @see SleepingKingStudios::Tools::Toolbox::Inflector#singularize.



225
226
227
228
229
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 225

def singularize(str)
  require_string!(str)

  inflector.singularize(str)
end

#string?(str) ⇒ Boolean

Returns true if the object is a String.

Examples:

StringTools.string?(nil)
#=> false
StringTools.string?([])
#=> false
StringTools.string?('Greetings, programs!')
#=> true
StringTools.string?(:greetings_starfighter)
#=> false

Parameters:

  • str (Object)

    the object to test.

Returns:

  • (Boolean)

    true if the object is a String, otherwise false.



246
247
248
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 246

def string?(str)
  str.is_a?(String)
end

#underscore(str) ⇒ String

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

Examples:

StringTools#underscore 'Bifrost'
#=> 'bifrost'

StringTools#underscore 'FenrisWolf'
#=> 'fenris_wolf'

Parameters:

  • str (String)

    the string to convert.

Returns:

  • (String)

    the converted string.

See Also:

  • Toolbox::Inflector#underscore.


264
265
266
267
268
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 264

def underscore(str)
  str = require_string!(str)

  inflector.underscore(str)
end