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.
-
#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(str) ⇒ String
Takes a word in singular form and returns the plural form, based on the defined rules and known irregular/uncountable words.
-
#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 expression to a lowercase, underscore separated string.
Methods inherited from Base
Constructor Details
#initialize(inflector: nil) ⇒ StringTools
Returns a new instance of StringTools.
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
#inflector ⇒ Object (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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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 |