Class: Dry::Inflector
- Inherits:
-
Object
- Object
- Dry::Inflector
- Defined in:
- lib/dry/inflector.rb,
lib/dry/inflector/rules.rb,
lib/dry/inflector/version.rb,
lib/dry/inflector/acronyms.rb,
lib/dry/inflector/inflections.rb,
lib/dry/inflector/inflections/defaults.rb
Overview
dry-inflector
Defined Under Namespace
Classes: Acronyms, Inflections, Rules
Constant Summary collapse
- VERSION =
"1.1.0"
Instance Method Summary collapse
-
#camelize_lower(input) ⇒ String
Lower camelize a string.
-
#camelize_upper(input) ⇒ String
(also: #camelize)
Upper camelize a string.
-
#classify(input) ⇒ String
Classify a string.
-
#constantize(input) ⇒ Class, Module
Find a constant with the name specified in the argument string.
-
#dasherize(input) ⇒ String
Dasherize a string.
-
#demodulize(input) ⇒ String
Demodulize a string.
-
#foreign_key(input) ⇒ String
Creates a foreign key name.
-
#humanize(input) ⇒ String
Humanize a string.
-
#initialize(&blk) {|the| ... } ⇒ Dry::Inflector
constructor
Instantiate the inflector.
-
#ordinalize(number) ⇒ String
Ordinalize a number.
-
#pluralize(input) ⇒ String
Pluralize a string.
-
#singularize(input) ⇒ String
Singularize a string.
-
#tableize(input) ⇒ String
Tableize a string.
- #to_s ⇒ String (also: #inspect)
-
#uncountable?(input) ⇒ TrueClass, FalseClass
private
Check if the input is an uncountable word.
-
#underscore(input) ⇒ String
Underscore a string.
Constructor Details
#initialize(&blk) {|the| ... } ⇒ Dry::Inflector
Instantiate the inflector
33 34 35 |
# File 'lib/dry/inflector.rb', line 33 def initialize(&blk) @inflections = Inflections.build(&blk) end |
Instance Method Details
#camelize_lower(input) ⇒ String
Lower camelize a string
49 50 51 |
# File 'lib/dry/inflector.rb', line 49 def camelize_lower(input) internal_camelize(input, false) end |
#camelize_upper(input) ⇒ String Also known as: camelize
Upper camelize a string
66 67 68 |
# File 'lib/dry/inflector.rb', line 66 def camelize_upper(input) internal_camelize(input, true) end |
#classify(input) ⇒ String
Classify a string
104 105 106 |
# File 'lib/dry/inflector.rb', line 104 def classify(input) camelize(singularize(input.to_s.split(".").last)) end |
#constantize(input) ⇒ Class, Module
Find a constant with the name specified in the argument string
The name is assumed to be the one of a top-level constant, constant scope of caller is ignored
88 89 90 |
# File 'lib/dry/inflector.rb', line 88 def constantize(input) Object.const_get(input, false) end |
#dasherize(input) ⇒ String
Dasherize a string
120 121 122 |
# File 'lib/dry/inflector.rb', line 120 def dasherize(input) input.to_s.tr("_", "-") end |
#demodulize(input) ⇒ String
Demodulize a string
136 137 138 |
# File 'lib/dry/inflector.rb', line 136 def demodulize(input) input.to_s.split("::").last end |
#foreign_key(input) ⇒ String
Creates a foreign key name
175 176 177 |
# File 'lib/dry/inflector.rb', line 175 def foreign_key(input) "#{underscore(demodulize(input))}_id" end |
#humanize(input) ⇒ String
Humanize a string
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/dry/inflector.rb', line 153 def humanize(input) input = input.to_s result = inflections.humans.apply_to(input) result.delete_suffix!("_id") result.tr!("_", " ") match = /(\W)/.match(result) separator = match ? match[0] : DEFAULT_SEPARATOR result.split(separator).map.with_index { |word, index| inflections.acronyms.apply_to(word, capitalize: index.zero?) }.join(separator) end |
#ordinalize(number) ⇒ String
Ordinalize a number
195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dry/inflector.rb', line 195 def ordinalize(number) abs_value = number.abs if ORDINALIZE_TH[abs_value % 100] "#{number}th" else case abs_value % 10 when 1 then "#{number}st" when 2 then "#{number}nd" when 3 then "#{number}rd" else "#{number}th" end end end |
#pluralize(input) ⇒ String
Pluralize a string
223 224 225 226 227 228 |
# File 'lib/dry/inflector.rb', line 223 def pluralize(input) input = input.to_s return input if uncountable?(input) inflections.plurals.apply_to(input) end |
#singularize(input) ⇒ String
Singularize a string
243 244 245 246 247 248 |
# File 'lib/dry/inflector.rb', line 243 def singularize(input) input = input.to_s return input if uncountable?(input) inflections.singulars.apply_to(input) end |
#tableize(input) ⇒ String
Tableize a string
262 263 264 265 |
# File 'lib/dry/inflector.rb', line 262 def tableize(input) input = input.to_s.gsub("::", "_") pluralize(underscore(input)) end |
#to_s ⇒ String Also known as: inspect
310 311 312 |
# File 'lib/dry/inflector.rb', line 310 def to_s "#<Dry::Inflector>" end |
#uncountable?(input) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the input is an uncountable word
300 301 302 303 304 |
# File 'lib/dry/inflector.rb', line 300 def uncountable?(input) input.match?(/\A[[:space:]]*\z/) || inflections.uncountables.include?(input.downcase) || inflections.uncountables.include?(input.split(/_|\b/).last.downcase) end |
#underscore(input) ⇒ String
Underscore a string
279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/dry/inflector.rb', line 279 def underscore(input) input = input.to_s.gsub("::", "/") input.gsub!(inflections.acronyms.regex) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1 ? "_" : ""}#{m2.downcase}" end input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') input.gsub!(/([a-z\d])([A-Z])/, '\1_\2') input.tr!("-", "_") input.downcase! input end |