Sha256: d78854507477fcaabc1009d7ad29a143d10f84833fb8b052eede832d6acb8fc8
Contents?: true
Size: 1.53 KB
Versions: 2
Compression:
Stored size: 1.53 KB
Contents
# String inflections define new methods on the String class to transform names for different purposes. # For instance, you can figure out the name of a database from the name of a class. # # "ScaleScore".underscore # => "scale_score" class String # +camelize+ converts strings to UpperCamelCase. # # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces. # # Examples: # "scale_score".camelize # => "ScaleScore" # "scale_score/errors".camelize # => "ScaleScore::Errors" def camelize self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end # Makes an underscored, lowercase form from the expression in the string. # # +underscore+ will also change '::' to '/' to convert namespaces to paths. # # "ScaleScore".underscore # => "scale_score" # "Couch::Generators".underscore # => couch/generators def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end # Capitalizes the first word and turns underscores into spaces. # This is meant for creating pretty output. # # Examples: # "employee_salary" # => "Employee salary" def humanize self.gsub(/_/, " ").capitalize end # Create a class name # Note that this returns a string and not a Class. (To convert to an actual class # follow +classify+ with +constantize+.) # # Examples: # "post".classify # => "Post" def classify self.camelize end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
couch-0.0.2 | lib/couch/core_ext/string/inflections.rb |
couch-0.0.1 | lib/couch/core_ext/string/inflections.rb |