Module: Spinach::Support
- Defined in:
- lib/spinach/support.rb
Overview
A module to offer helpers for string mangling.
Class Method Summary collapse
-
.camelize(name) ⇒ String
The
name
in camel case. -
.underscore(camel_cased_word) ⇒ Object
Makes an underscored, lowercase form from the expression in the string.
Class Method Details
.camelize(name) ⇒ String
Returns The name
in camel case.
16 17 18 |
# File 'lib/spinach/support.rb', line 16 def self.camelize(name) name.to_s.strip.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join end |
.underscore(camel_cased_word) ⇒ Object
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
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"
32 33 34 35 36 37 38 39 40 |
# File 'lib/spinach/support.rb', line 32 def self.underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |