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)

    An object that conforms to the interface used by SleepingKingStudios::Tools::Toolbox::Inflector, such as ActiveSupport::Inflector .



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

def initialize(inflector: nil)
  super()

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

Instance Attribute Details

#inflectorObject (readonly)

Returns the value of attribute inflector.



37
38
39
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 37

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.


46
47
48
49
50
# 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) ⇒ 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.



59
60
61
62
63
# 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) ⇒ 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.



72
73
74
75
76
77
# 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| ... } ⇒ 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.



88
89
90
91
92
93
94
# 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) ⇒ 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.



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

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, based on the defined rules and known irregular/uncountable words.

Parameters:

  • str (String)

    The word to pluralize.

Returns:

  • (String)

    The pluralized word.



113
114
115
116
117
# 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) ⇒ 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.



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

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

  word == singularize(word)
end

#singularize(str) ⇒ String

Transforms the word to a singular, lowercase form.

Parameters:

  • str (String)

    The word to transform.

Returns:

  • (String)

    The word in singular form.



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

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.



145
146
147
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 145

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.


157
158
159
160
161
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 157

def underscore(str)
  str = require_string! str

  inflector.underscore(str)
end