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
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).
Defined Under Namespace
Classes: Rules
Instance Attribute Summary collapse
-
#rules ⇒ SleepingKingStudios::Tools::Toobox::Inflector::Rules
readonly
An object defining the transformation rules.
Instance Method Summary collapse
-
#camelize(word, uppercase_first_letter = true) ⇒ String
Converts a lowercase, underscore-separated string to CamelCase.
-
#initialize(rules: nil) ⇒ Inflector
constructor
A new instance of Inflector.
-
#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 ⇒ SleepingKingStudios::Tools::Toobox::Inflector::Rules (readonly)
Returns 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.
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.
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.
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.
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 |