Module: Useful::RubyExtensions::String::FromActivesupport::ClassMethods
- Defined in:
- lib/useful/ruby_extensions/string.rb
Instance Method Summary collapse
-
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object
By default,
camelize
converts strings to UpperCamelCase. -
#classify(class_str) ⇒ Object
Create a class name from a string like Rails does for table names to models.
-
#constantize(camel_cased_word) ⇒ Object
:nodoc:.
-
#dasherize(underscored_word) ⇒ Object
Replaces underscores with dashes in the string.
-
#demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string.
-
#humanize(lower_case_and_underscored_word) ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any.
-
#parameterize(phrase) ⇒ Object
makes string suitable for a dashed url parameter string.
-
#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
The reverse of
camelize
.
Instance Method Details
#camelize(lower_case_and_underscored_word, first_letter_in_uppercase = 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.
Examples:
"active_record".camelize # => "ActiveRecord"
"active_record".camelize(:lower) # => "activeRecord"
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record/error".camelize(:lower) # => "activeRecord::Error"
109 110 111 112 113 114 115 |
# File 'lib/useful/ruby_extensions/string.rb', line 109 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word[0..0].downcase + camelize(lower_case_and_underscored_word)[1..-1] end end |
#classify(class_str) ⇒ Object
Create a class name from a string like Rails does for table names to models.
> Note: unlike Rails, this one does not use inflectors to singularize
> Note: this returns a string and not a Class. (To convert to an actual class
follow classify
with constantize
.)
Examples:
"egg_and_hams".classify # => "EggAndHams"
"active_record/errors".classify # => "ActiveRecord::Errors"
"active_record.error".classify # => "Error"
126 127 128 129 |
# File 'lib/useful/ruby_extensions/string.rb', line 126 def classify(class_str) # strip out any leading schema name camelize(class_str.to_s.sub(/.*\./, '')) end |
#constantize(camel_cased_word) ⇒ Object
:nodoc:
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/useful/ruby_extensions/string.rb', line 214 def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = ::Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#dasherize(underscored_word) ⇒ Object
Replaces underscores with dashes in the string.
Example:
"puni_puni" # => "puni-puni"
172 173 174 |
# File 'lib/useful/ruby_extensions/string.rb', line 172 def dasherize(underscored_word) underscored_word.gsub(/_/, '-') end |
#demodulize(class_name_in_module) ⇒ Object
Removes the module part from the expression in the string.
Examples:
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
"Inflections".demodulize # => "Inflections"
190 191 192 |
# File 'lib/useful/ruby_extensions/string.rb', line 190 def demodulize(class_name_in_module) class_name_in_module.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.
Examples:
"employee_salary" # => "Employee salary"
"author_id" # => "Author"
137 138 139 140 |
# File 'lib/useful/ruby_extensions/string.rb', line 137 def humanize(lower_case_and_underscored_word) result = lower_case_and_underscored_word.to_s.dup result.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
#parameterize(phrase) ⇒ Object
makes string suitable for a dashed url parameter string
Example:
"ActiveRecord" # => "active-record"
"Active Record is_awesome" # => "active-record-is-awesome"
181 182 183 |
# File 'lib/useful/ruby_extensions/string.rb', line 181 def parameterize(phrase) phrase.underscore.gsub(/\s+/, '_').dasherize 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.
Examples:
"man from the boondocks".titleize # => "Man From The Boondocks"
"x-men: the last stand".titleize # => "X Men: The Last Stand"
149 150 151 |
# File 'lib/useful/ruby_extensions/string.rb', line 149 def titleize(word) humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } end |
#underscore(camel_cased_word) ⇒ Object
The reverse of camelize
. Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
Examples:
"ActiveRecord".underscore # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors
160 161 162 163 164 165 166 |
# File 'lib/useful/ruby_extensions/string.rb', line 160 def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |