lib/truck/string_inflections.rb in truck-0.8.6 vs lib/truck/string_inflections.rb in truck-0.8.7

- old
+ new

@@ -1,30 +1,30 @@ module Truck module StringInflections - refine String do - def to_camel_case - str = "_#{self}" - str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase } - str.gsub!('/', '::') - str - end + extend self - def to_snake_case - str = gsub '::', '/' - # Convert FOOBar => FooBar - str.gsub! %r{[[:upper:]]{2,}} do |uppercase| - bit = uppercase[0] - bit << uppercase[1...-1].downcase - bit << uppercase[-1] - bit - end - str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel| - bit = camel[0] - bit << '_' - bit << camel[1..-1].downcase - end - str.downcase! - str + def to_camel_case(str) + str = "_#{str}" + str.gsub!(%r{_[a-z]}) { |snake| snake.slice(1).upcase } + str.gsub!('/', '::') + str + end + + def to_snake_case(str) + str = str.gsub '::', '/' + # Convert FOOBar => FooBar + str.gsub! %r{[[:upper:]]{2,}} do |uppercase| + bit = uppercase[0] + bit << uppercase[1..-1].downcase + bit end + # Convert FooBar => foo_bar + str.gsub! %r{[[:lower:]][[:upper:]]+[[:lower:]]} do |camel| + bit = camel[0] + bit << '_' + bit << camel[1..-1].downcase + end + str.downcase! + str end end end