Parent

Files

String

This is an adapted version of active_support/core_ext/string/inflections.rb to prevent loading several dependencies including I18n gem.

Issue: github.com/rails/rails/issues/1526


Removes indentation Add colors

@example

help <<-EOS.undent
  Here my help usage
   sample_code

  Fix
EOS
puts help.red.bold

Public Class Methods

colors() click to toggle source
# File lib/padrino-core/support_lite.rb, line 165
def self.colors
  @_colors ||= {
    :clear   => 0,
    :bold    => 1,
    :black   => 30,
    :red     => 31,
    :green   => 32,
    :yellow  => 33,
    :blue    => 34,
    :magenta => 35,
    :cyan    => 36,
    :white   => 37
  }
end

Public Instance Methods

camelcase(first_letter = :upper) click to toggle source
Alias for: camelize
camelize(first_letter = :upper) click to toggle source

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"
# File lib/padrino-core/support_lite.rb, line 86
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
Also aliased as: camelcase
classify() click to toggle source

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"
# File lib/padrino-core/support_lite.rb, line 106
def classify
  ActiveSupport::Inflector.classify(self)
end
constantize() click to toggle source

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
# File lib/padrino-core/support_lite.rb, line 59
def constantize
  ActiveSupport::Inflector.constantize(self)
end
pluralize() click to toggle source

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"
# File lib/padrino-core/support_lite.rb, line 33
def pluralize
  ActiveSupport::Inflector.pluralize(self)
end
singularize() click to toggle source

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"
# File lib/padrino-core/support_lite.rb, line 47
def singularize
  ActiveSupport::Inflector.singularize(self)
end
undent() click to toggle source
# File lib/padrino-core/support_lite.rb, line 186
def undent
  gsub(/^.{#{slice(/^ +/).size}}/, '')
end
underscore() click to toggle source

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
# File lib/padrino-core/support_lite.rb, line 71
def underscore
  ActiveSupport::Inflector.underscore(self)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.