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

Transforms words (e.g. from singular to plural).

Should maintain the same interface as ActiveSupport::Inflector.

Defined Under Namespace

Classes: Rules

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules: nil) ⇒ Rules

Returns An object defining the transformation rules.



31
32
33
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 31

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

Instance Attribute Details

#rulesRules (readonly)

Returns The defined rules object for the inflector.

Returns:

  • (Rules)

    The defined rules object for the inflector.



36
37
38
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 36

def rules
  @rules
end

Instance Method Details

#camelize(word, uppercase_first_letter = true) ⇒ String

Transforms the word 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.



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

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 }

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 61

def pluralize(word)
  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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 88

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.



113
114
115
116
117
118
119
120
121
122
# File 'lib/sleeping_king_studios/tools/toolbox/inflector.rb', line 113

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

  word = word.to_s.gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')

  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end