module Inflecto::Refinements

Refines `String` with inflection methods and `Numeric` with `#ordinalize`

@author Alex Semyonov <alex@semyonov.us>

@since 0.1.0

@example

require 'inflecto-refinements'
using Inflecto::Refinements

'data_mapper'.camelize            #=> 'DataMapper'
'data.mapper'.classify            #=> 'DataMapper'
'DataMapper'.constantize          #=> DataMapper
'data_mapper_rspec'.dasherize     #=> 'data-mapper-rspec'
'DataMapper::Inflecto'.demodulize #=> 'Inflecto'
'Message'.foreign_key             #=> 'message_id'
'employee_salary'.humanize        #=> 'Employee salary'
'sample'.pluralize                #=> 'samples'
'forums'.singularize              #=> 'forum'
'news'.singularize                #=> 'news'
'fancy_category'.tableize         #=> 'fancy_categories'
'DataMapper'.underscore           #=> 'data_mapper'
1.ordinalize                      #=> '1st'

Constants

VERSION

Public Instance Methods

camelize() click to toggle source

Convert self to UpperCamelCase

Will also convert '/' to '::' which is useful for converting paths to namespaces.

@example

"data_mapper".camelize        # => "DataMapper"
"data_mapper/errors".camelize # => "DataMapper::Errors"

@return [String]

# File lib/inflecto/refinements.rb, line 40
def camelize
  Inflecto.camelize(self)
end
classify() click to toggle source

Classify self

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.

@examples:

"egg_and_hams".classify # => "EggAndHam"
"posts".classify        # => "Post"

# Singular names are not handled correctly:
"business".classify     # => "Busines"

@return [String]

# File lib/inflecto/refinements.rb, line 185
def classify
  Inflecto.classify(self)
end
constantize() click to toggle source

Find a constant with the name specified in the argument string

The name is assumed to be the one of a top-level constant, constant scope of caller is ignored

@example

"Module".constantize            # => Module
"DataMapper::Error".constantize # => DataMapper::Error

@return [Class, Module]

# File lib/inflecto/refinements.rb, line 101
def constantize
  Inflecto.constantize(self)
end
dasherize() click to toggle source

Convert self underscores to dashes

@example

"foo_bar".dasherize # => "foo-bar"

@return [String]

# File lib/inflecto/refinements.rb, line 63
def dasherize
  Inflecto.dasherize(self)
end
demodulize() click to toggle source

Return unscoped constant name

@example

"DataMapper::Error".demodulize # => "Error"
"DataMapper".demodulize        # => "DataMapper"

@return [String]

# File lib/inflecto/refinements.rb, line 75
def demodulize
  Inflecto.demodulize(self)
end
foreign_key() click to toggle source

Creates a foreign key name

@example

"Message".foreign_key => "message_id"

@return [String]

# File lib/inflecto/refinements.rb, line 86
def foreign_key
  Inflecto.foreign_key(self)
end
humanize() click to toggle source

Humanize string

capitalizes the first word and turns underscores into spaces and strips a # trailing “_id”, if any. Like titleize, this is meant for creating pretty output.

@example

"employee_salary".humanize # => "Employee salary"
"author_id".humanize       # => "Author"

@return [String]

# File lib/inflecto/refinements.rb, line 147
def humanize
  Inflecto.humanize(self)
end
ordinalize() click to toggle source

Convert a number into an ordinal string

@example

1.ordinalize     # => "1st"
2.ordinalize     # => "2nd"
1002.ordinalize  # => "1002nd"
1003.ordinalize  # => "1003rd"

@return [String]

# File lib/inflecto/refinements.rb, line 201
def ordinalize
  Inflecto.ordinalize(self)
end
pluralize() click to toggle source

Convert self word string to plural

@example

"post".pluralize         # => "posts"
"octopus".pluralize      # => "octopi"
"sheep".pluralize        # => "sheep"
"words".pluralize        # => "words"
"CamelOctopus".pluralize # => "CamelOctopi"

@return [String]

# File lib/inflecto/refinements.rb, line 116
def pluralize
  Inflecto.pluralize(self)
end
singularize() click to toggle source

Convert word to singular

@example

"posts".singularize # => "post"
"octopi".singularize # => "octopus"
"sheep".singularize # => "sheep"
"word".singularize # => "word"
"CamelOctopi".singularize # => "CamelOctopus"

@return [String]

# File lib/inflecto/refinements.rb, line 131
def singularize
  Inflecto.singularize(self)
end
tableize() click to toggle source

Tableize string

Create the name of a table like Rails does for models to table names. This method # uses the pluralize method on the last word in the string.

@example

"RawScaledScorer".tableize # => "raw_scaled_scorers"
"egg_and_ham".tableize     # => "egg_and_hams"
"fancyCategory".tableize   # => "fancy_categories"

@return [String]

# File lib/inflecto/refinements.rb, line 164
def tableize
  Inflecto.tableize(self)
end
underscore() click to toggle source

Convert self to underscored, lowercase string

Changes '::' to '/' to convert namespaces to paths.

@example

"DataMapper".underscore         # => "data_mapper"
"DataMapper::Errors".underscore # => "data_mapper/errors"

@return [String]

# File lib/inflecto/refinements.rb, line 53
def underscore
  Inflecto.underscore(self)
end