Module: String::Style
- Included in:
- String
- Defined in:
- lib/more/facets/stylize.rb
Instance Method Summary collapse
-
#basename ⇒ Object
(also: #demodulize)
Removes prepend module namespace.
-
#camelcase(first = false, on = '_\s') ⇒ Object
Converts a string to camelcase.
-
#camelize ⇒ Object
Variation of coverting a string to camelcase.
-
#humanize ⇒ Object
Replaces underscores with spaces and capitalizes word.
-
#methodize ⇒ Object
Converts a string into a valid ruby method name This method is geared toward code reflection.
-
#modulize ⇒ Object
Converts a string into a valid ruby class or module name This method is geared toward code reflection.
-
#ordinalize ⇒ Object
(also: #ordinal)
Use end of string to append ordinal suffix.
-
#pathize ⇒ Object
Converts a string into a unix path.
-
#title(pattern = $;, *limit) ⇒ Object
(also: #titleize, #titlecase, #capitalize_all)
Capitalize all words (or other patterned divisions) of a string.
-
#underscore ⇒ Object
Underscore string based on camelcase characteristics.
Instance Method Details
#basename ⇒ Object Also known as: demodulize
Removes prepend module namespace.
"Test::Unit".basename #=> "Unit"
78 79 80 |
# File 'lib/more/facets/stylize.rb', line 78 def basename self.to_s.gsub(/^.*::/, '') end |
#camelcase(first = false, on = '_\s') ⇒ Object
Converts a string to camelcase. By default capitalization occurs on whitespace and underscores. By setting the first parameter to true the first character can also be captizlized. The second parameter can be assigned a valid Regualr Expression characeter set to determine which characters to match for capitalizing subsequent parts of the string.
"this_is a test".camelcase #=> "thisIsATest"
"this_is a test".camelcase(true) #=> "ThisIsATest"
"this_is a test".camelcase(true, ' ') #=> "This_isATest"
60 61 62 63 64 65 66 |
# File 'lib/more/facets/stylize.rb', line 60 def camelcase( first=false, on='_\s' ) if first gsub(/(^|[#{on}]+)([A-Za-z])/){ $2.upcase } else gsub(/([#{on}]+)([A-Za-z])/){ $2.upcase } end end |
#camelize ⇒ Object
Variation of coverting a string to camelcase. This is unlike #camelcase in that it is geared toward code reflection use.
"this/is_a_test".camelize #=> This::IsATest
43 44 45 46 |
# File 'lib/more/facets/stylize.rb', line 43 def camelize #to_s.gsub(/(^|_)(.)/){$2.upcase} to_s.gsub(/\/(.?)/){ "::" + $1.upcase }.gsub(/(^|_)(.)/){ $2.upcase } end |
#humanize ⇒ Object
Replaces underscores with spaces and capitalizes word.
70 71 72 |
# File 'lib/more/facets/stylize.rb', line 70 def humanize self.gsub(/_/, " ").capitalize end |
#methodize ⇒ Object
Converts a string into a valid ruby method name This method is geared toward code reflection.
"MyModule::MyClass".methodize #=> "my_module__my_class"
See also String#modulize, String#pathize – TODO Make sure methodize it is revertible ++
107 108 109 |
# File 'lib/more/facets/stylize.rb', line 107 def methodize to_s.gsub(/([A-Z])/, '_\1').downcase.gsub(/^_/,'').gsub(/(::|\/)_?/, '__') end |
#modulize ⇒ Object
Converts a string into a valid ruby class or module name This method is geared toward code reflection.
"my_module__my_path".modulize #=> "MyModule::MyPath"
See also String#methodize, String#pathize
– TODO Make sure moduleize that all scenarios return a valid ruby class name ++
121 122 123 |
# File 'lib/more/facets/stylize.rb', line 121 def modulize to_s.gsub(/(__|\/)(.?)/){ "::" + $2.upcase }.gsub(/(^|_)(.)/){ $2.upcase } end |
#ordinalize ⇒ Object Also known as: ordinal
Use end of string to append ordinal suffix.
127 128 129 |
# File 'lib/more/facets/stylize.rb', line 127 def ordinalize Integer(self).ordinalize end |
#pathize ⇒ Object
Converts a string into a unix path. This method is geared toward code reflection.
See : String#modulize, String#methodize
"MyModule::MyClass".pathize #=> my_module/my_class
"my_module__my_class".pathize #=> my_module/my_class
TODO :
-
Make sure that all scenarios return a valid unix path
-
Make sure it is revertible
146 147 148 |
# File 'lib/more/facets/stylize.rb', line 146 def pathize to_s.gsub(/([A-Z])/, '_\1').downcase.gsub(/^_/,'').gsub(/(::|__)_?/, '/') end |
#title(pattern = $;, *limit) ⇒ Object Also known as: titleize, titlecase, capitalize_all
Capitalize all words (or other patterned divisions) of a string.
"this is a test".capitalize_all #=> "This Is A Test"
89 90 91 92 |
# File 'lib/more/facets/stylize.rb', line 89 def title( pattern=$;, *limit ) #gsub(/\b\w/){$&.upcase} split(pattern, *limit).select{ |w| w.capitalize! || w }.join(" ") end |
#underscore ⇒ Object
Underscore string based on camelcase characteristics.
152 153 154 |
# File 'lib/more/facets/stylize.rb', line 152 def underscore #(camel_cased_word) self.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase end |