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
Performs a series of operations on the string.
-
#indent(str, count = 2) ⇒ String
Adds the specified number of spaces to the start of each line.
-
#initialize(inflector: 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) ⇒ StringTools
Returns a new instance of StringTools.
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
#inflector ⇒ Object (readonly)
Returns 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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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 |