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/inflections.rb,
lib/dry/inflector/inflections/defaults.rb
Overview
dry-inflector
Defined Under Namespace
Classes: Inflections, Rules
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Method Summary collapse
-
#camelize(input) ⇒ String
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.
-
#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(input) ⇒ String
Camelize a string
49 50 51 |
# File 'lib/dry/inflector.rb', line 49 def camelize(input) input.to_s.gsub(/\/(.?)/) { "::#{Regexp.last_match(1).upcase}" }.gsub(/(?:\A|_)(.)/) { Regexp.last_match(1).upcase } end |
#classify(input) ⇒ String
Classify a string
85 86 87 |
# File 'lib/dry/inflector.rb', line 85 def classify(input) camelize(singularize(input.to_s.sub(/.*\./, ""))) 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
69 70 71 |
# File 'lib/dry/inflector.rb', line 69 def constantize(input) Object.const_get(input) end |
#dasherize(input) ⇒ String
Dasherize a string
101 102 103 |
# File 'lib/dry/inflector.rb', line 101 def dasherize(input) input.to_s.tr("_", "-") end |
#demodulize(input) ⇒ String
Demodulize a string
117 118 119 |
# File 'lib/dry/inflector.rb', line 117 def demodulize(input) input.to_s.split("::").last end |
#foreign_key(input) ⇒ String
Creates a foreign key name
153 154 155 |
# File 'lib/dry/inflector.rb', line 153 def foreign_key(input) "#{underscorize(demodulize(input))}_id" end |
#humanize(input) ⇒ String
Humanize a string
134 135 136 137 138 139 140 141 |
# File 'lib/dry/inflector.rb', line 134 def humanize(input) input = input.to_s result = inflections.humans.apply_to(input) result.gsub!(/_id\z/, "") result.tr!("_", " ") result.capitalize! result end |
#ordinalize(number) ⇒ String
Ordinalize a number
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/dry/inflector.rb', line 173 def ordinalize(number) # rubocop:disable Metrics/MethodLength abs_value = number.abs if ORDINALIZE_TH.key?(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
201 202 203 204 205 |
# File 'lib/dry/inflector.rb', line 201 def pluralize(input) input = input.to_s return input if uncountable?(input) inflections.plurals.apply_to(input) end |
#singularize(input) ⇒ String
Singularize a string
220 221 222 223 224 |
# File 'lib/dry/inflector.rb', line 220 def singularize(input) input = input.to_s return input if uncountable?(input) inflections.singulars.apply_to(input) end |
#tableize(input) ⇒ String
Tableize a string
238 239 240 241 |
# File 'lib/dry/inflector.rb', line 238 def tableize(input) input = input.to_s.gsub(/::/, "_") pluralize(underscorize(input)) 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
267 268 269 |
# File 'lib/dry/inflector.rb', line 267 def uncountable?(input) !(input =~ /\A[[:space:]]*\z/).nil? || inflections.uncountables.include?(input.downcase) end |
#underscore(input) ⇒ String
Underscore a string
255 256 257 258 |
# File 'lib/dry/inflector.rb', line 255 def underscore(input) input = input.to_s.gsub(/::/, "/") underscorize(input) end |