Skip to content

Commit

Permalink
Fix String#camelize(:lower), add specs for all string inflection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Jul 15, 2008
1 parent 6213862 commit 4ea509e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
8 changes: 3 additions & 5 deletions lib/sequel_model/inflector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,9 @@ def self.inflections
# "active_record/errors".camelize #=> "ActiveRecord::Errors"
# "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
def camelize(first_letter_in_uppercase = :upper)
if first_letter_in_uppercase == :upper
gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
else
"#{first}#{camelize[1..-1]}"
end
s = gsub(/\/(.?)/){|x| "::#{x[-1..-1].upcase unless x == '/'}"}.gsub(/(^|_)(.)/){|x| x[-1..-1].upcase}
s[0...1] = s[0...1].downcase unless first_letter_in_uppercase == :upper
s
end
alias_method :camelcase, :camelize

Expand Down
95 changes: 90 additions & 5 deletions spec/sequel_model/inflector_spec.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,119 @@
require File.join(File.dirname(__FILE__), 'spec_helper')

describe String do
it "should transform words from singular to plural" do
it "#camelize and #camelcase should transform the word to CamelCase" do
"egg_and_hams".camelize.should == "EggAndHams"
"egg_and_hams".camelize(false).should == "eggAndHams"
"post".camelize.should == "Post"
"post".camelcase.should == "Post"
end

it "#constantize should eval the string to get a constant" do
"String".constantize.should == String
"String::Inflections".constantize.should == String::Inflections
proc{"BKSDDF".constantize}.should raise_error
proc{"++A++".constantize}.should raise_error
end

it "#dasherize should transform underscores to dashes" do
"egg_and_hams".dasherize.should == "egg-and-hams"
"post".dasherize.should == "post"
end

it "#demodulize should remove any preceding modules" do
"String::Inflections::Blah".demodulize.should == "Blah"
"String::Inflections".demodulize.should == "Inflections"
"String".demodulize.should == "String"
end

it "#humanize should remove _i, transform underscore to spaces, and capitalize" do
"egg_and_hams".humanize.should == "Egg and hams"
"post".humanize.should == "Post"
"post_id".humanize.should == "Post"
end

it "#titleize and #titlecase should underscore, humanize, and capitalize all words" do
"egg-and: hams".titleize.should == "Egg And: Hams"
"post".titleize.should == "Post"
"post".titlecase.should == "Post"
end

it "#underscore should add underscores between CamelCased words, change :: to / and - to _, and downcase" do
"EggAndHams".underscore.should == "egg_and_hams"
"EGGAndHams".underscore.should == "egg_and_hams"
"Egg::And::Hams".underscore.should == "egg/and/hams"
"post".underscore.should == "post"
"post-id".underscore.should == "post_id"
end

it "#pluralize should transform words from singular to plural" do
"post".pluralize.should == "posts"
"octopus".pluralize.should =="octopi"
"the blue mailman".pluralize.should == "the blue mailmen"
"CamelOctopus".pluralize.should == "CamelOctopi"
end

it "should transform words from plural to singular" do
it "#singularize should transform words from plural to singular" do
"posts".singularize.should == "post"
"octopi".singularize.should == "octopus"
"the blue mailmen".singularize.should == "the blue mailman"
"CamelOctopi".singularize.should == "CamelOctopus"
end

it "should transform class names to table names" do
it "#tableize should transform class names to table names" do
"RawScaledScorer".tableize.should == "raw_scaled_scorers"
"egg_and_ham".tableize.should == "egg_and_hams"
"fancyCategory".tableize.should == "fancy_categories"
end

it "should tranform table names to class names" do
it "#classify should tranform table names to class names" do
"egg_and_hams".classify.should == "EggAndHam"
"post".classify.should == "Post"
end

it "should create a foreign key name from a class name" do
it "#foreign_key should create a foreign key name from a class name" do
"Message".foreign_key.should == "message_id"
"Message".foreign_key(false).should == "messageid"
"Admin::Post".foreign_key.should == "post_id"
end
end

describe String::Inflections do
before do
@plurals, @singulars, @uncountables = String.inflections.plurals.dup, String.inflections.singulars.dup, String.inflections.uncountables.dup
end
after do
String.inflections.plurals.replace(@plurals)
String.inflections.singulars.replace(@singulars)
String.inflections.uncountables.replace(@uncountables)
end

it "should be possible to clear the list of singulars, plurals, and uncountables" do
String.inflections.clear(:plurals)
String.inflections.plurals.should == []
String.inflections.plural('blah', 'blahs')
String.inflections.clear
String.inflections.plurals.should == []
String.inflections.singulars.should == []
String.inflections.uncountables.should == []
end

it "should be able to specify new inflection rules" do
String.inflections do |i|
i.plural(/xx$/i, 'xxx')
i.singular(/ttt$/i, 'tt')
i.irregular('yy', 'yyy')
i.uncountable(%w'zz')
end
'roxx'.pluralize.should == 'roxxx'
'rottt'.singularize.should == 'rott'
'yy'.pluralize.should == 'yyy'
'yyy'.singularize.should == 'yy'
'zz'.pluralize.should == 'zz'
'zz'.singularize.should == 'zz'
end

it "should be yielded and returned by String.inflections" do
String.inflections{|i| i.should == String::Inflections}.should == String::Inflections
end
end

0 comments on commit 4ea509e

Please sign in to comment.