Class: SleepingKingStudios::Tools::Toolbox::Inflector
- Inherits:
-
Object
- Object
- SleepingKingStudios::Tools::Toolbox::Inflector
- 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
-
#rules ⇒ Rules
readonly
The defined rules object for the inflector.
Instance Method Summary collapse
-
#camelize(word, uppercase_first_letter = true) ⇒ String
Transforms the word to CamelCase.
-
#initialize(rules: nil) ⇒ Rules
constructor
An object defining the transformation rules.
-
#pluralize(word) ⇒ String
Transforms the word to a plural, lowercase form.
-
#singularize(word) ⇒ String
Transforms the word to a singular, lowercase form.
-
#underscore(word) ⇒ String
Transforms the word to a lowercase, underscore-separated form.
Constructor Details
Instance Attribute Details
#rules ⇒ Rules (readonly)
Returns 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.
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.
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.
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.
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 |