From 55659f547afb1ed903295784f9f4ae99624810d1 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 3 Feb 2012 18:38:45 +0000 Subject: [PATCH] Labels are now split on numbers --- lib/erd_handler/label.rb | 13 +++++++++- spec/erd_handler/label_spec.rb | 43 +++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/erd_handler/label.rb b/lib/erd_handler/label.rb index ad81618..6c19758 100644 --- a/lib/erd_handler/label.rb +++ b/lib/erd_handler/label.rb @@ -18,6 +18,11 @@ class Label else split_camel_case = true end + if opts.has_key? :numbers + split_numbers = opts[:numbers] + else + split_numbers = true + end end @processed = @processed.map do |segment| segment.split(regexp) @@ -25,7 +30,13 @@ class Label if split_camel_case @processed = @processed.map do |segment| - segment.split(/(?=[A-Z])/) + segment.split(/(?<=[a-z])(?=[A-Z])/) + end.flatten + end + + if split_numbers + @processed = @processed.map do |segment| + segment.split(/(?:(? true + l1.processed.should == ["Test", "123", "Label"] + l1.original.should == "Test123Label" + + l2 = Label.new "test1label" + l2.split :numbers => true + l2.processed.should == ["test", "1", "label"] + l2.original.should == "test1label" + end + + it "doesn't split the original on numbers if asked not to" do + l1 = Label.new "Test123Label" + l1.split :numbers => false + l1.processed.should == ["Test123Label"] + l1.original.should == "Test123Label" + + l2 = Label.new "Test123Label" + l2.split :numbers => nil + l2.processed.should == ["Test123Label"] + l2.original.should == "Test123Label" + end it "splits the original using a default regexp" do l1 = Label.new "Test label_string" @@ -72,19 +96,26 @@ module ErdHandler l1.original.should == "TestLabel" end - it "splits the original on punctuation and camel case by default" do - l1 = Label.new "TestLabel is_split, he,said" + it "splits the original on numbers by default" do + l1 = Label.new "Test123Label" + l1.split + l1.processed.should == ["Test", "123", "Label"] + l1.original.should == "Test123Label" + end + + it "splits the original on punctuation, whitespace, camel case, and numbers by default" do + l1 = Label.new "TestLabel is_split, 123 he,said456Fred" l1.split - l1.processed.should == ["Test", "Label", "is", "split", "he", "said"] - l1.original.should == "TestLabel is_split, he,said" + l1.processed.should == ["Test", "Label", "is", "split","123", "he", "said", "456", "Fred"] + l1.original.should == "TestLabel is_split, 123 he,said456Fred" end it "is idempotent" do - l1 = Label.new "TestLabel is_split, he,said" + l1 = Label.new "TestLabel is_split, 123 he,said456Fred" res1 = l1.split.dup res2 = l1.split res1.processed.should == res2.processed - l1.original.should == "TestLabel is_split, he,said" + l1.original.should == "TestLabel is_split, 123 he,said456Fred" end end # split -- 2.34.1