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
Returns the value of attribute inflector.
Instance Method Summary collapse
-
#camelize(str) ⇒ String
Converts a lowercase, underscore-separated string to CamelCase.
-
#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.
- #define_irregular_word(singular, plural) ⇒ Object
- #define_plural_rule(match, replace) ⇒ Object
- #define_singular_rule(match, replace) ⇒ Object
- #define_uncountable_word(word) ⇒ Object
-
#indent(str, count = 2) ⇒ String
Adds the specified number of spaces to the start of each line of the string.
-
#initialize(inflector: nil) ⇒ StringTools
constructor
A new instance of StringTools.
-
#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.
-
#plural?(word) ⇒ Boolean
Determines whether or not the given word is in plural form.
- #pluralize(*args) ⇒ Object
-
#singular?(word) ⇒ Boolean
Determines whether or not the given word is in singular form.
- #singularize(str) ⇒ Object
-
#string?(str) ⇒ Boolean
Returns true if the object is a String.
-
#underscore(str) ⇒ String
Converts a mixed-case string expression 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 |
# 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
#inflector ⇒ Object (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.
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.
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.
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.
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.
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
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.
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.
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.
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 |