Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove String core method addition.

  • Loading branch information...
commit 982863759429b5890ef3c0ed77f66439c80d66c4 1 parent bbca3b9
@jnunemaker authored
View
1  lib/crack.rb
@@ -4,5 +4,6 @@ class ParseError < StandardError; end
end
require 'crack/core_extensions'
+require 'crack/util'
require 'crack/json'
require 'crack/xml'
View
10 lib/crack/core_extensions.rb
@@ -13,14 +13,6 @@ def blank?
end unless method_defined?(:blank?)
end # class Object
-class String #:nodoc:
- def snake_case
- return self.downcase if self =~ /^[A-Z]+$/
- self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
- return $+.downcase
- end unless method_defined?(:snake_case)
-end # class String
-
class Hash #:nodoc:
# @return <String> This hash as a query string
#
@@ -77,7 +69,7 @@ def normalize_param(key, value)
# #=> 'one="1" two="TWO"'
def to_xml_attributes
map do |k,v|
- %{#{k.to_s.snake_case.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '&quot;')}"}
+ %{#{Crack::Util.snake_case(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}="#{v.to_s.gsub('"', '&quot;')}"}
end.join(' ')
end
end
View
11 lib/crack/util.rb
@@ -0,0 +1,11 @@
+module Crack
+ module Util
+ def snake_case(str)
+ return str.downcase if str =~ /^[A-Z]+$/
+ str.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
+ return $+.downcase
+ end
+
+ extend self
+ end
+end
View
16 test/string_test.rb
@@ -3,29 +3,29 @@
class CrackTest < Test::Unit::TestCase
context "snake_case" do
should "lowercases one word CamelCase" do
- "Merb".snake_case.should == "merb"
+ Crack::Util.snake_case("Merb").should == "merb"
end
should "makes one underscore snake_case two word CamelCase" do
- "MerbCore".snake_case.should == "merb_core"
+ Crack::Util.snake_case("MerbCore").should == "merb_core"
end
should "handles CamelCase with more than 2 words" do
- "SoYouWantContributeToMerbCore".snake_case.should == "so_you_want_contribute_to_merb_core"
+ Crack::Util.snake_case("SoYouWantContributeToMerbCore").should == "so_you_want_contribute_to_merb_core"
end
should "handles CamelCase with more than 2 capital letter in a row" do
- "CNN".snake_case.should == "cnn"
- "CNNNews".snake_case.should == "cnn_news"
- "HeadlineCNNNews".snake_case.should == "headline_cnn_news"
+ Crack::Util.snake_case("CNN").should == "cnn"
+ Crack::Util.snake_case("CNNNews").should == "cnn_news"
+ Crack::Util.snake_case("HeadlineCNNNews").should == "headline_cnn_news"
end
should "does NOT change one word lowercase" do
- "merb".snake_case.should == "merb"
+ Crack::Util.snake_case("merb").should == "merb"
end
should "leaves snake_case as is" do
- "merb_core".snake_case.should == "merb_core"
+ Crack::Util.snake_case("merb_core").should == "merb_core"
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.