Module: English::StyleORM
- Included in:
- String
- Defined in:
- lib/gems/english-0.3.1/lib/english/style_orm.rb
Defined Under Namespace
Modules: Replace
Instance Method Summary collapse
-
#camelize(first_letter_in_uppercase = true) ⇒ Object
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.
-
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
-
#foreign_key(separate_id_with_underscore = true) ⇒ Object
Creates a foreign key name from a class name.
-
#humanize ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id.
-
#tableize ⇒ Object
Create the name of a table like Rails does for models to table names.
-
#titleize ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
-
#underscore ⇒ Object
The reverse of
camelize
.
Instance Method Details
#camelize(first_letter_in_uppercase = true) ⇒ Object
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(true) #=> "activeRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"
"active_record/errors".camelize(true) #=> "activeRecord::Errors"
27 28 29 30 31 32 33 34 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 27 def camelize(first_letter_in_uppercase = true) if first_letter_in_uppercase to_s.gsub(/\/(.?)/){ "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/){ $1.upcase } else lowercase_and_underscored = to_s lowercase_and_underscored[0,1] + lowercase_and_underscored.camelize[1..-1] 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"
73 74 75 76 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 73 def classify # strip out any leading schema name to_s.sub(/.*\./, '').singularize.camelize end |
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
"puni_puni".dasherize #=> "puni-puni"
57 58 59 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 57 def dasherize to_s.gsub(/_/, '-') end |
#foreign_key(separate_id_with_underscore = true) ⇒ Object
Creates a foreign key name from a class name. separate_id_with_underscore
sets whether the method should put ‘_’ between the name and ‘id’.
"Message".foreign_key #=> "message_id"
"Message".foreign_key(false) #=> "messageid"
"Admin::Post".foreign_key #=> "post_id"
120 121 122 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 120 def foreign_key(separate_id_with_underscore = true) to_s.demodulize.underscore + (separate_id_with_underscore ? "_id" : "id") end |
#humanize ⇒ Object
Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.
"employee_salary".humanize #=> "Employee salary"
"author_id".humanize #=> "Author"
84 85 86 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 84 def humanize to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end |
#tableize ⇒ Object
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.
"RawScaledScorer".tableize #=> "raw_scaled_scorers"
"egg_and_ham".tableize #=> "egg_and_hams"
"fancyCategory".tableize #=> "fancy_categories"
95 96 97 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 95 def tableize to_s.underscore.pluralize end |
#titleize ⇒ Object
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. Titleize is meant for creating pretty output. It is not used in the Rails internals.
titleize is also aliased as as titlecase
"man from the boondocks".titleize #=> "Man From The Boondocks"
"x-men: the last stand".titleize #=> "X Men: The Last Stand"
108 109 110 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 108 def titleize to_s.underscore.humanize.gsub(/\b('?[a-z])/){ $1.capitalize } end |
#underscore ⇒ Object
The reverse of camelize
. Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> active_record/errors
44 45 46 47 48 49 50 51 |
# File 'lib/gems/english-0.3.1/lib/english/style_orm.rb', line 44 def underscore to_s. gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |