diff --git a/README.md b/README.md index 15b20d8..a783c2a 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ Nori lets you specify a custom formula to convert XML tags to Hash keys using `c Nori.new.parse('active') # => {"userResponse"=>{"accountStatus"=>"active"}} -parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym }) +parser = Nori.new(:convert_tags_to => lambda { |tag| Nori::StringUtils.snakecase(tag).to_sym }) parser.parse('active') # => {:user_response=>{:account_status=>"active"}} ``` diff --git a/lib/nori/core_ext.rb b/lib/nori/core_ext.rb index 09cc095..0a5ad5b 100644 --- a/lib/nori/core_ext.rb +++ b/lib/nori/core_ext.rb @@ -1,2 +1,2 @@ -require "nori/core_ext/string" +require "nori/string_utils" require "nori/core_ext/hash" diff --git a/lib/nori/core_ext/hash.rb b/lib/nori/core_ext/hash.rb index 44153d7..b032451 100644 --- a/lib/nori/core_ext/hash.rb +++ b/lib/nori/core_ext/hash.rb @@ -27,7 +27,7 @@ def normalize_param(key, value) # #=> 'one="1" two="TWO"' def to_xml_attributes map do |k, v| - %{#{k.to_s.snakecase.sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"} + %{#{StringUtils.snakecase(k.to_s).sub(/^(.{1,1})/) { |m| m.downcase }}="#{v}"} end.join(' ') end diff --git a/lib/nori/core_ext/string.rb b/lib/nori/core_ext/string.rb deleted file mode 100644 index 27b4308..0000000 --- a/lib/nori/core_ext/string.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Nori - module CoreExt - module String - - # Returns the String in snake_case. - def snakecase - str = dup - str.gsub!(/::/, '/') - str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') - str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') - str.tr! ".", "_" - str.tr! "-", "_" - str.downcase! - str - end unless method_defined?(:snakecase) - - end - end -end - -String.send :include, Nori::CoreExt::String diff --git a/lib/nori/string_utils.rb b/lib/nori/string_utils.rb new file mode 100644 index 0000000..da6d990 --- /dev/null +++ b/lib/nori/string_utils.rb @@ -0,0 +1,18 @@ +class Nori + module StringUtils + # Converts a string to snake case. + # + # @param inputstring [String] The string to be converted to snake case. + # @return [String] A copy of the input string converted to snake case. + def self.snakecase(inputstring) + str = inputstring.dup + str.gsub!(/::/, '/') + str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') + str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') + str.tr!(".", "_") + str.tr!("-", "_") + str.downcase! + str + end + end +end diff --git a/spec/nori/api_spec.rb b/spec/nori/api_spec.rb index b4e8691..bd67627 100644 --- a/spec/nori/api_spec.rb +++ b/spec/nori/api_spec.rb @@ -55,7 +55,7 @@ it "converts all tags by a given formula" do xml = 'active' - snakecase_symbols = lambda { |tag| tag.snakecase.to_sym } + snakecase_symbols = lambda { |tag| Nori::StringUtils.snakecase(tag).to_sym } nori = nori(:convert_tags_to => snakecase_symbols) expect(nori.parse(xml)).to eq({ :user_response => { :@id => "1", :account_status => "active" } }) diff --git a/spec/nori/core_ext/string_spec.rb b/spec/nori/core_ext/string_spec.rb deleted file mode 100644 index 93989b7..0000000 --- a/spec/nori/core_ext/string_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require "spec_helper" - -describe String do - - describe "#snakecase" do - it "lowercases one word CamelCase" do - expect("Merb".snakecase).to eq("merb") - end - - it "makes one underscore snakecase two word CamelCase" do - expect("MerbCore".snakecase).to eq("merb_core") - end - - it "handles CamelCase with more than 2 words" do - expect("SoYouWantContributeToMerbCore".snakecase).to eq("so_you_want_contribute_to_merb_core") - end - - it "handles CamelCase with more than 2 capital letter in a row" do - expect("CNN".snakecase).to eq("cnn") - expect("CNNNews".snakecase).to eq("cnn_news") - expect("HeadlineCNNNews".snakecase).to eq("headline_cnn_news") - end - - it "does NOT change one word lowercase" do - expect("merb".snakecase).to eq("merb") - end - - it "leaves snake_case as is" do - expect("merb_core".snakecase).to eq("merb_core") - end - end - -end diff --git a/spec/nori/string_utils_spec.rb b/spec/nori/string_utils_spec.rb new file mode 100644 index 0000000..b577592 --- /dev/null +++ b/spec/nori/string_utils_spec.rb @@ -0,0 +1,33 @@ +require "spec_helper" + +describe Nori::StringUtils do + + describe ".snakecase" do + it "lowercases one word CamelCase" do + expect(Nori::StringUtils.snakecase("Merb")).to eq("merb") + end + + it "makes one underscore snakecase two word CamelCase" do + expect(Nori::StringUtils.snakecase("MerbCore")).to eq("merb_core") + end + + it "handles CamelCase with more than 2 words" do + expect(Nori::StringUtils.snakecase("SoYouWantContributeToMerbCore")).to eq("so_you_want_contribute_to_merb_core") + end + + it "handles CamelCase with more than 2 capital letter in a row" do + expect(Nori::StringUtils.snakecase("CNN")).to eq("cnn") + expect(Nori::StringUtils.snakecase("CNNNews")).to eq("cnn_news") + expect(Nori::StringUtils.snakecase("HeadlineCNNNews")).to eq("headline_cnn_news") + end + + it "does NOT change one word lowercase" do + expect(Nori::StringUtils.snakecase("merb")).to eq("merb") + end + + it "leaves snake_case as is" do + expect(Nori::StringUtils.snakecase("merb_core")).to eq("merb_core") + end + end + +end