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.



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

def initialize(inflector: nil)
  @inflector =
    inflector || SleepingKingStudios::Tools::Toolbox::Inflector.new
end

Instance Attribute Details

#inflectorObject (readonly)

Returns the value of attribute inflector.



32
33
34
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 32

def inflector
  @inflector
end

Instance Method Details

#camelize(str) ⇒ String

Converts a lowercase, underscore-separated string to CamelCase.

Parameters:

  • str (String)

    The string to convert.

Returns:

  • (String)

    The converted string.

See Also:

  • ActiveSupport::Inflector#camelize.


41
42
43
44
45
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 41

def camelize(str)
  str = require_string! str

  inflector.camelize(str)
end

#chain(str, *commands) ⇒ String

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

Parameters:

  • str (String)

    The string to process.

  • commands (Array<String, Symbol>)

    The string operations to apply.

Returns:

  • (String)

    The processed string.



54
55
56
57
58
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 54

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

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

#define_irregular_word(singular, plural) ⇒ Object



61
62
63
64
65
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 61

def define_irregular_word(singular, plural)
  CoreTools.deprecate 'StringTools#define_irregular_word'

  inflector.rules.define_irregular_word singular, plural
end

#define_plural_rule(match, replace) ⇒ Object



68
69
70
71
72
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 68

def define_plural_rule(match, replace)
  CoreTools.deprecate 'StringTools#define_plural_rule'

  inflector.rules.define_plural_rule match, replace
end

#define_singular_rule(match, replace) ⇒ Object



75
76
77
78
79
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 75

def define_singular_rule(match, replace)
  CoreTools.deprecate 'StringTools#define_singular_rule'

  inflector.rules.define_singular_rule match, replace
end

#define_uncountable_word(word) ⇒ Object



82
83
84
85
86
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 82

def define_uncountable_word(word)
  CoreTools.deprecate 'StringTools#define_uncountable_word'

  inflector.rules.define_uncountable_word word
end

#indent(str, count = 2) ⇒ String

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

Parameters:

  • str (String)

    The string to indent.

  • count (Integer) (defaults to: 2)

    The number of spaces to add.

Returns:

  • (String)

    The indented string.



95
96
97
98
99
100
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 95

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 of the string to the provided block and combines the results into a new multiline string.

Parameters:

  • str (String)

    The string to map.

Yield Parameters:

  • line (String)

    The current line.

  • index (Integer)

    The index of the current line.

Returns:

  • (String)

    The mapped string.



111
112
113
114
115
116
117
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 111

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.

Returns:

  • (Boolean)

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



123
124
125
126
127
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 123

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

  word == pluralize(word)
end

#pluralize(str) ⇒ String #pluralize(count, single, plural) ⇒ String

Overloads:

  • #pluralize(str) ⇒ String

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

    Parameters:

    • str (String)

      The word to pluralize.

    Returns:

    • (String)

      The pluralized word.

  • #pluralize(count, single, plural) ⇒ String
    Deprecated.

    This functionality is deprecated as of version 0.4.0 and will be removed in a future version. Use IntegerTools#pluralize instead.

    Returns the singular or the plural value, depending on the provided item count.

    Examples:

    "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
    #=> 'There are four lights!'
    

    Parameters:

    • count (Integer)

      The number of items.

    • single (String)

      The singular form of the word or phrase.

    • plural (String)

      The plural form of the word or phrase.

    Returns:

    • (String)

      The single form if count == 1; otherwise the plural form.



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 155

def pluralize(*args)
  if args.count == 3
    CoreTools.deprecate 'StringTools#pluralize with 3 arguments',
      message: 'Use IntegerTools#pluralize instead.'

    return IntegerTools.pluralize(*args)
  end

  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.

Returns:

  • (Boolean)

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



172
173
174
175
176
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 172

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

  word == singularize(word)
end

#singularize(str) ⇒ Object



179
180
181
182
183
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 179

def singularize(str)
  require_string! str

  inflector.singularize str
end

#string?(str) ⇒ Boolean

Returns true if the object is a String.

Parameters:

  • str (Object)

    The object to test.

Returns:

  • (Boolean)

    True if the object is a String, otherwise false.



190
191
192
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 190

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

#underscore(str) ⇒ String

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

Parameters:

  • str (String)

    The string to convert.

Returns:

  • (String)

    The converted string.

See Also:

  • ActiveSupport::Inflector#underscore.


202
203
204
205
206
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 202

def underscore(str)
  str = require_string! str

  inflector.underscore(str)
end