Class: SleepingKingStudios::Tools::StringTools
- Defined in:
- lib/sleeping_king_studios/tools/string_tools.rb
Overview
Tools for working with strings.
Instance Attribute Summary collapse
-
#inflector ⇒ Object
readonly
Service object for inflecting strings.
Instance Method Summary collapse
-
#camelize(str) ⇒ String
Converts a lowercase, underscore-separated string to CamelCase.
-
#chain(str, *commands) ⇒ String
deprecated
Deprecated.
v1.3.0 Use Object#then {} instead.
-
#indent(str, count = 2) ⇒ String
Adds the specified number of spaces to the start of each line.
-
#initialize(inflector: nil, toolbelt: nil) ⇒ StringTools
constructor
A new instance of StringTools.
-
#map_lines(str) {|line, index| ... } ⇒ String
Yields each line to the provided block and combines the results.
-
#plural?(word) ⇒ Boolean
Determines whether or not the given word is in plural form.
-
#pluralize(str) ⇒ String
Takes a word in singular form and returns the plural form.
-
#singular?(word) ⇒ Boolean
Determines whether or not the given word is in singular form.
-
#singularize(str) ⇒ String
Transforms the word to a singular, lowercase form.
-
#string?(str) ⇒ Boolean
Returns true if the object is a String.
-
#underscore(str) ⇒ String
Converts a mixed-case string to a lowercase, underscore separated string.
Methods inherited from Base
Constructor Details
#initialize(inflector: nil, toolbelt: nil) ⇒ StringTools
Returns a new instance of StringTools.
29 30 31 32 33 34 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 29 def initialize(inflector: nil, toolbelt: nil) super(toolbelt:) @inflector = inflector || SleepingKingStudios::Tools::Toolbox::Inflector.new end |
Instance Attribute Details
#inflector ⇒ Object (readonly)
Returns service object for inflecting strings.
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.
53 54 55 56 57 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 53 def camelize(str) str = require_string!(str) inflector.camelize(str) end |
#chain(str, *commands) ⇒ String
v1.3.0 Use Object#then {} instead.
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.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 75 def chain(str, *commands) toolbelt.core_tools.deprecate( "#{self.class.name}#chain", message: 'Use Object#then {} instead.' ) str = require_string!(str) commands.reduce(str) { |memo, command| public_send(command, memo) } end |
#indent(str, count = 2) ⇒ String
Adds the specified number of spaces to the start of each line.
108 109 110 111 112 113 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 108 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.
143 144 145 146 147 148 149 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 143 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.
168 169 170 171 172 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 168 def plural?(word) word = require_string!(word) word == pluralize(word) end |
#pluralize(str) ⇒ String
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 190 def pluralize(*args) str = require_string!(args.first) unless args.one? toolbelt.core_tools.deprecate( "#{self.class.name}#pluralize with multiple arguments" ) end 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.
219 220 221 222 223 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 219 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.
240 241 242 243 244 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 240 def singularize(str) require_string!(str) inflector.singularize(str) end |
#string?(str) ⇒ Boolean
Returns true if the object is a String.
261 262 263 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 261 def string?(str) str.is_a?(String) end |
#underscore(str) ⇒ String
Converts a mixed-case string to a lowercase, underscore separated string.
279 280 281 282 283 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 279 def underscore(str) str = require_string!(str) inflector.underscore(str) end |