Skip to content

Commit

Permalink
Stop monkey-patching String with #snakecase (savonrb#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchu authored Mar 1, 2024
1 parent af36ee6 commit dbbd948
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Nori lets you specify a custom formula to convert XML tags to Hash keys using `c
Nori.new.parse('<userResponse><accountStatus>active</accountStatus></userResponse>')
# => {"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('<userResponse><accountStatus>active</accountStatus></userResponse>')
# => {:user_response=>{:account_status=>"active"}}
```
Expand Down
2 changes: 1 addition & 1 deletion lib/nori/core_ext.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "nori/core_ext/string"
require "nori/string_utils"
require "nori/core_ext/hash"
2 changes: 1 addition & 1 deletion lib/nori/core_ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 0 additions & 21 deletions lib/nori/core_ext/string.rb

This file was deleted.

18 changes: 18 additions & 0 deletions lib/nori/string_utils.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/nori/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
it "converts all tags by a given formula" do
xml = '<userResponse id="1"><accountStatus>active</accountStatus></userResponse>'

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" } })
Expand Down
33 changes: 0 additions & 33 deletions spec/nori/core_ext/string_spec.rb

This file was deleted.

33 changes: 33 additions & 0 deletions spec/nori/string_utils_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dbbd948

Please sign in to comment.