Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/padrino-support/core_ext/string/colorize.rb,
lib/padrino-support/core_ext/string/inflections.rb
Overview
This is an adapted version of active_support/core_ext/string/inflections.rb to prevent loading several dependencies including I18n gem.
Defined Under Namespace
Classes: Colorizer
Instance Method Summary collapse
-
#camelize(first_letter = :upper) ⇒ Object
(also: #camelcase)
By default,
camelize
converts strings to UpperCamelCase. -
#classify ⇒ Object
Create a class name from a plural table name like Rails does for table names to models.
-
#colorize(args) ⇒ Object
colorize(:red).
-
#constantize ⇒ Object
constantize
tries to find a declared constant with the name specified in the string. -
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
-
#humanize(options = {}) ⇒ Object
Capitalizes the first word, turns underscores into spaces, and strips a trailing ‘_id’ if present.
-
#pluralize ⇒ Object
Returns the plural form of the word in the string.
-
#singularize ⇒ Object
Returns the singular form of the word in the string.
-
#underscore ⇒ Object
The reverse of
camelize
.
Instance Method Details
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
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_record".camelize # => "ActiveRecord"
"active_record".camelize(:lower) # => "activeRecord"
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"
75 76 77 78 79 80 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 75 def camelize(first_letter = :upper) case first_letter when :upper then ActiveSupport::Inflector.camelize(self, true) when :lower then ActiveSupport::Inflector.camelize(self, false) end end |
#classify ⇒ Object
Create a class name from a plural table name like Rails does for table names to models. Note that this returns a string and not a class. (To convert to an actual class follow classify
with constantize
.)
"egg_and_hams".classify # => "EggAndHam"
"posts".classify # => "Post"
Singular names are not handled correctly.
"business".classify # => "Busines"
95 96 97 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 95 def classify ActiveSupport::Inflector.classify(self) end |
#colorize(args) ⇒ Object
colorize(:red)
17 18 19 20 21 22 23 24 |
# File 'lib/padrino-support/core_ext/string/colorize.rb', line 17 def colorize(args) case args when Symbol Colorizer.send(args, self) when Hash Colorizer.send(args[:color], self, args[:mode]) end end |
#constantize ⇒ Object
constantize
tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
"Module".constantize # => Module
"Class".constantize # => Class
48 49 50 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 48 def constantize ActiveSupport::Inflector.constantize(self) end |
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
109 110 111 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 109 def dasherize ActiveSupport::Inflector.dasherize(self) end |
#humanize(options = {}) ⇒ Object
Capitalizes the first word, turns underscores into spaces, and strips a trailing ‘_id’ if present.
102 103 104 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 102 def humanize( = {}) ActiveSupport::Inflector.humanize(self, ) end |
#pluralize ⇒ Object
Returns the plural form of the word in the string.
"post".pluralize # => "posts"
"octopus".pluralize # => "octopi"
"sheep".pluralize # => "sheep"
"words".pluralize # => "words"
"the blue mailman".pluralize # => "the blue mailmen"
"CamelOctopus".pluralize # => "CamelOctopi"
22 23 24 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 22 def pluralize ActiveSupport::Inflector.pluralize(self) end |
#singularize ⇒ Object
Returns the singular form of the word in the string.
"posts".singularize # => "post"
"octopi".singularize # => "octopus"
"sheep".singularize # => "sheep"
"words".singularize # => "word"
"the blue mailmen".singularize # => "the blue mailman"
"CamelOctopi".singularize # => "CamelOctopus"
36 37 38 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 36 def singularize ActiveSupport::Inflector.singularize(self) end |
#underscore ⇒ Object
The reverse of camelize
. Makes an underscored, lowercase form from the expression in the string.
underscore
will also change ‘::’ to ‘/’ to convert namespaces to paths.
"ActiveRecord".underscore # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors
60 61 62 |
# File 'lib/padrino-support/core_ext/string/inflections.rb', line 60 def underscore ActiveSupport::Inflector.underscore(self) end |