Permalink
Browse files

Added option to String#camelize to generate lower-cased camel case by…

… passing in :lower, like "super_man".camelize(:lower) # => "superMan" [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3986 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Mar 20, 2006
1 parent f49ba11 commit 4e7c6f58fb14ad8783062303191eebed7699965b
@@ -738,7 +738,7 @@ def method_missing(method, *arguments)
if method.to_s =~ /(.*)=$/
assign($1, arguments.first)
else
- call("#{method.to_s.first}#{method.to_s.camelize[1..-1]}", *arguments)
+ call("#{method.to_s.camelize(:lower)}", *arguments)
end
end
@@ -865,7 +865,7 @@ def enumerate(enumerable, options = {}, &block)
method_args = arguments_for_call options[:method_args] # foo, bar, function
method_args << ', ' unless method_args.blank?
add_variable_assignment!(options[:variable]) if options[:variable]
- append_enumerable_function!("#{enumerable.to_s.first}#{enumerable.to_s.camelize[1..-1]}(#{method_args}function(#{yield_args}) {")
+ append_enumerable_function!("#{enumerable.to_s.camelize(:lower)}(#{method_args}function(#{yield_args}) {")
# only yield as many params as were passed in the block
yield *options[:yield_args].collect { |p| JavaScriptVariableProxy.new(@generator, p) }[0..block.arity-1]
add_return_statement! if options[:return]
View
@@ -1,5 +1,7 @@
*SVN*
+* Added option to String#camelize to generate lower-cased camel case by passing in :lower, like "super_man".camelize(:lower) # => "superMan" [DHH]
+
* Added Hash#diff to show the difference between two hashes [Chris McGrath]
* Fixed HashWithIndifferentAccess#delete to work with both symbols and strings #2176 [Caio Chassot]
@@ -12,8 +12,11 @@ def singularize
Inflector.singularize(self)
end
- def camelize
- Inflector.camelize(self)
+ def camelize(first_letter = :upper)
+ case first_letter
+ when :upper then Inflector.camelize(self, true)
+ when :lower then Inflector.camelize(self, false)
+ end
end
alias_method :camelcase, :camelize
@@ -109,8 +109,12 @@ def singularize(word)
end
end
- def camelize(lower_case_and_underscored_word)
- lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
+ if first_letter_in_uppercase
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
+ else
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
+ end
end
def titleize(word)
@@ -110,7 +110,14 @@ class InflectorTest < Test::Unit::TestCase
"Product" => "product",
"SpecialGuest" => "special_guest",
"ApplicationController" => "application_controller",
- "Area51Controller" => "area51_controller",
+ "Area51Controller" => "area51_controller"
+ }
+
+ UnderscoreToLowerCamel = {
+ "product" => "product",
+ "special_guest" => "specialGuest",
+ "application_controller" => "applicationController",
+ "area51_controller" => "area51Controller"
}
CamelToUnderscoreWithoutReverse = {
@@ -308,4 +315,10 @@ def test_underscore_as_reverse_of_dasherize
assert_equal(underscored, Inflector.underscore(Inflector.dasherize(underscored)))
end
end
+
+ def test_underscore_to_lower_camel
+ UnderscoreToLowerCamel.each do |underscored, lower_camel|
+ assert_equal(lower_camel, Inflector.camelize(underscored, false))
+ end
+ end
end

0 comments on commit 4e7c6f5

Please sign in to comment.