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

Instance Method Details

#camelize(str) ⇒ String

Converts a lowercase, underscore-separated string to CamelCase.

Parameters:

  • str (String)

    The string to convert.

Returns:

  • (String)

    The converted string.

See Also:

  • ActiveSupport::Inflector#camelize.


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”.

Parameters:

  • singular (String)

    The singular form of the word.

  • plural (String)

    The plural form of the word.



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.

Parameters:

  • match (Regexp)

    The matching rule.

  • replace (String)

    The replacement string.



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.

Parameters:

  • match (Regexp)

    The matching rule.

  • replace (String)

    The replacement string.



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.

Parameters:

  • word (String)

    The uncountable 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.

Returns:

  • (Boolean)

    True if the word is in plural form, otherwise false.



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

Overloads:

  • #pluralize(str) ⇒ String

    Takes a word in singular form and returns the plural form, based on the defined rules and known irregular/uncountable words.

    Parameters:

    • str (String)

      The word to pluralize.

    Returns:

    • (String)

      The pluralized word.

  • #pluralize(count, single, plural) ⇒ String
    Deprecated.

    This functionality is deprecated as of version 0.4.0 and will be removed in a future version. Use IntegerTools#pluralize instead.

    Returns the singular or the plural value, depending on the provided item count.

    Examples:

    "There are four #{StringTools.pluralize 4, 'light', 'lights'}!"
    #=> 'There are four lights!'
    

    Parameters:

    • count (Integer)

      The number of items.

    • single (String)

      The singular form of the word or phrase.

    • plural (String)

      The plural form of the word or phrase.

    Returns:

    • (String)

      The single form if count == 1; otherwise the plural form.



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.

Returns:

  • (Boolean)

    True if the word is in singular form, otherwise false.



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.

Parameters:

  • str (String)

    The word to singularize.

Returns:

  • (String)

    The singularized word.

See Also:



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.

Parameters:

  • str (Object)

    The object to test.

Returns:

  • (Boolean)

    True if the object is a String, otherwise false.



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.

Parameters:

  • str (String)

    The string to convert.

Returns:

  • (String)

    The converted string.

See Also:

  • ActiveSupport::Inflector#underscore.


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