Class: SleepingKingStudios::Tools::Toolbox::Inflector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sleeping_king_studios/tools/toolbox/inflector.rb,
lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb

Overview

Service object for transforming strings.

Internally, an instance of this class is used to StringTools to perform string transformations.

This class should define a compatible interface with ActiveSupport::Inflector, i.e. all methods defined on Toolbox::Inflector should have a corresponding method on ActiveSupport::Inflector with a superset of the parameters. (The reverse is not true).

See Also:

Defined Under Namespace

Classes: Rules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules: nil) ⇒ Inflector

Returns a new instance of Inflector.

Parameters:

  • rules (SleepingKingStudios::Tools::Toobox::Inflector::Rules) (defaults to: nil)

    an object defining the transformation rules.



40
41
42
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 40

def initialize(rules: nil)
  @rules = rules || Rules.new
end

Instance Attribute Details

#rulesSleepingKingStudios::Tools::Toobox::Inflector::Rules (readonly)

Returns an object defining the transformation rules.

Returns:

  • (SleepingKingStudios::Tools::Toobox::Inflector::Rules)

    an object defining the transformation rules.



46
47
48
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 46

def rules
  @rules
end

Instance Method Details

#camelize(word, uppercase_first_letter = true) ⇒ String

Converts a lowercase, underscore-separated string to CamelCase.

Parameters:

  • word (String)

    the word to transform.

  • uppercase_first_letter (Boolean) (defaults to: true)

    if true, the first letter is capitalized. Defaults to true.

Returns:

  • (String)

    the word in CamelCase.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 55

def camelize(word, uppercase_first_letter = true) # rubocop:disable Style/OptionalBooleanParameter
  return '' if word.nil? || word.empty?

  word =
    word
    .to_s
    .gsub(/(\b|[_-]+)([a-z])/) { Regexp.last_match(2).upcase }
    .gsub(/[_-]+/, '')

  (uppercase_first_letter ? word[0].upcase : word[0].downcase) + word[1..]
end

#pluralize(word) ⇒ String

Transforms the word to a plural, lowercase form.

Parameters:

  • word (String)

    the word to transform.

Returns:

  • (String)

    the word in plural form.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 72

def pluralize(word) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  return '' if word.nil? || word.empty?

  normalized = word.to_s.strip.downcase

  return normalized if uncountable_words.include?(normalized)

  return normalized if irregular_words_reversed.key?(normalized)

  return irregular_words[normalized] if irregular_words.key?(normalized)

  plural_rules.each do |match, replace|
    next unless normalized =~ match

    return normalized.sub(match, replace)
  end

  word
end

#singularize(word) ⇒ String

Transforms the word to a singular, lowercase form.

Parameters:

  • word (String)

    the word to transform.

Returns:

  • (String)

    the word in singular form.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 97

def singularize(word) # rubocop:disable Metrics/MethodLength
  return '' if word.nil? || word.empty?

  normalized = word.to_s.strip.downcase

  return normalized if irregular_words.key?(normalized)

  if irregular_words_reversed.key?(normalized)
    return irregular_words_reversed[normalized]
  end

  singular_rules.each do |match, replace|
    next unless normalized =~ match

    return normalized.sub(match, replace)
  end

  word
end

#underscore(word) ⇒ String

Transforms the word to a lowercase, underscore-separated form.

Parameters:

  • word (String)

    the word to transform.

Returns:

  • (String)

    the word in underscored form.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 122

def underscore(word)
  return '' if word.nil? || word.empty?

  word
    .to_s
    .gsub(/([A-Z0-9\d]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z])([A-Z\d])/, '\1_\2')
    .gsub(/(\d)([A-Z])/, '\1_\2')
    .gsub(/\A_+|_+\z/, '')
    .tr('-', '_')
    .downcase
end