Module: SleepingKingStudios::Tools::StringTools
- Extended by:
- StringTools
- Included in:
- StringTools
- Defined in:
- lib/sleeping_king_studios/tools/string_tools.rb,
lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb
Overview
Tools for working with strings.
Defined Under Namespace
Classes: PluralInflector
Instance Method Summary collapse
-
#camelize(str) ⇒ String
Converts a lowercase, underscore-separated string to CamelCase.
-
#define_irregular_word(singular, plural) ⇒ Object
Defines an irregular word, which is a singular and plural word pair that do not obey any defined rule, such as “goose” and “geese”.
-
#define_plural_rule(match, replace) ⇒ Object
Defines a rule for pluralization.
-
#define_singular_rule(match, replace) ⇒ Object
Defines a rule for singularization.
-
#define_uncountable_word(word) ⇒ Object
Defines an uncountable word, such as “data”.
-
#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) ⇒ String
Takes a word in plural form and returns the singular form, based on the defined rules and known irregular/uncountable words.
-
#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.
Instance Method Details
#camelize(str) ⇒ String
Converts a lowercase, underscore-separated string to CamelCase.
20 21 22 23 24 25 26 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 20 def camelize str require_string! str str = str.dup str.gsub!(/(\b|[_-])([a-z])/) { |match| $2.upcase } str end |
#define_irregular_word(singular, plural) ⇒ Object
Defines an irregular word, which is a singular and plural word pair that do not obey any defined rule, such as “goose” and “geese”.
29 30 31 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 29 def define_irregular_word singular, plural plural_inflector.define_irregular_word singular, plural end |
#define_plural_rule(match, replace) ⇒ Object
Defines a rule for pluralization. The rule will be applied to any words that match the first parameter, performing a replace on the word using the second parameter.
Rules are applied in reverse order of definition, meaning that rules defined later will take precedence over previously defined rules.
34 35 36 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 34 def define_plural_rule match, replace plural_inflector.define_plural_rule match, replace end |
#define_singular_rule(match, replace) ⇒ Object
Defines a rule for singularization. The rule will be applied to any words that match the first parameter, performing a replace on the word using the second parameter.
Rules are applied in reverse order of definition, meaning that rules defined later will take precedence over previously defined rules.
39 40 41 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 39 def define_singular_rule match, replace plural_inflector.define_singular_rule match, replace end |
#define_uncountable_word(word) ⇒ Object
Defines an uncountable word, such as “data”. If #pluralize or #singularize is called with an uncountable word as its parameter, it will return the unmodified word.
44 45 46 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 44 def define_uncountable_word word plural_inflector.define_uncountable_word word 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.
52 53 54 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 52 def plural? word word == pluralize(word) end |
#pluralize(str) ⇒ String #pluralize(count, single, plural) ⇒ String
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 82 def pluralize *args if args.count == 3 CoreTools.deprecate 'StringTools#pluralize with 3 arguments', :message => 'Use IntegerTools#pluralize instead.' return IntegerTools.pluralize(*args) end # if require_string! args.first plural_inflector.pluralize args.first 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.
99 100 101 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 99 def singular? word word == singularize(word) end |
#singularize(str) ⇒ String
Takes a word in plural form and returns the singular form, based on the defined rules and known irregular/uncountable words.
104 105 106 107 108 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 104 def singularize str require_string! str plural_inflector.singularize str end |
#string?(str) ⇒ Boolean
Returns true if the object is a String.
115 116 117 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 115 def string? str String === str end |
#underscore(str) ⇒ String
Converts a mixed-case string expression to a lowercase, underscore separated string.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sleeping_king_studios/tools/string_tools.rb', line 127 def underscore str require_string! str str = str.dup str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') str.tr!("-", "_") str.downcase! str end |