Module: Functional::Inflect
Instance Method Summary collapse
-
#camelize(term, uppercase_first_letter = true) ⇒ Object
By default,
camelizeconverts strings to UpperCamelCase. -
#dasherize(underscored_word) ⇒ Object
Replaces underscores with dashes in the string.
-
#extensionize(fname, ext, opts = {}) ⇒ String
Add the given extension to the given file name.
-
#humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any.
-
#titleize(word) ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
-
#underscore(camel_cased_word) ⇒ Object
Makes an underscored, lowercase form from the expression in the string.
Instance Method Details
#camelize(term, uppercase_first_letter = true) ⇒ Object
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
'active_model'.camelize # => "ActiveModel"
'active_model'.camelize(:lower) # => "activeModel"
'active_model/errors'.camelize # => "ActiveModel::Errors"
'active_model/errors'.camelize(:lower) # => "activeModel::Errors"
As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:
'SSLError'.underscore.camelize # => "SslError"
24 25 26 27 28 29 30 31 32 |
# File 'lib/functional/inflect.rb', line 24 def camelize(term, uppercase_first_letter = true) string = term.to_s if uppercase_first_letter == true string = string.sub(/^[a-z\d]*/) { $&.capitalize } else string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase } end string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::') end |
#dasherize(underscored_word) ⇒ Object
Replaces underscores with dashes in the string.
'puni_puni'.dasherize # => "puni-puni"
95 96 97 |
# File 'lib/functional/inflect.rb', line 95 def dasherize(underscored_word) underscored_word.to_s.dup.tr('_', '-') end |
#extensionize(fname, ext, opts = {}) ⇒ String
Add the given extension to the given file name. Removes the current extension if one exists. Strips trailing characters from the file name if present. Does nothing if the file name already has the given extension.
120 121 122 123 124 125 |
# File 'lib/functional/inflect.rb', line 120 def extensionize(fname, ext, opts={}) extname = File.extname(fname) return fname if (extname =~ /\.?#{ext}$/i) == 0 fname = fname.gsub(/#{extname}$/, '') if opts[:chomp] == true return fname.strip + '.' + ext.to_s.gsub(/^\./, '') end |
#humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any. Like titleize, this is meant for creating pretty output.
'employee_salary'.humanize # => "Employee salary"
'author_id'.humanize # => "Author"
66 67 68 69 70 71 |
# File 'lib/functional/inflect.rb', line 66 def humanize(lower_case_and_underscored_word) result = lower_case_and_underscored_word.to_s.dup result.gsub!(/_id$/, "") result.tr!('_', ' ') result.gsub(/([a-z\d]*)/i) { |match| "#{match.downcase}" }.gsub(/^\w/) { $&.upcase } end |
#titleize(word) ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize is meant for creating pretty output. It is not used in the Rails internals.
titleize is also aliased as titlecase.
'man from the boondocks'.titleize # => "Man From The Boondocks"
'x-men: the last stand'.titleize # => "X Men: The Last Stand"
'TheManWithoutAPast'.titleize # => "The Man Without A Past"
'raiders_of_the_lost_ark'.titleize # => "Raiders Of The Lost Ark"
85 86 87 88 |
# File 'lib/functional/inflect.rb', line 85 def titleize(word) #humanize(underscore(word)).gsub(/\b(?<!['`])[a-z]/) { $&.capitalize } humanize(underscore(word)).gsub(/\b["'`]?[a-z]/) { $&.capitalize } end |
#underscore(camel_cased_word) ⇒ Object
Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
'ActiveModel'.underscore # => "active_model"
'ActiveModel::Errors'.underscore # => "active_model/errors"
As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold:
'SSLError'.underscore.camelize # => "SslError"
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/functional/inflect.rb', line 47 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!('::', '/') word.gsub!(/\s+/, '_') word.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 |