tagify_string 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/tagify_string/core_ext.rb +21 -2
- data/lib/tagify_string/version.rb +1 -1
- data/spec/models/string_spec.rb +44 -10
- metadata +1 -1
@@ -3,10 +3,29 @@ require "active_support/all"
|
|
3
3
|
# Add tagify methods to string
|
4
4
|
String.class_eval do
|
5
5
|
def tagify(options = {})
|
6
|
-
default_options = {:upcase => true}
|
6
|
+
default_options = {:upcase => true, :sep => "-"}
|
7
7
|
opts = default_options.merge(options)
|
8
8
|
|
9
|
-
|
9
|
+
prefix = ""
|
10
|
+
|
11
|
+
if opts.has_key?(:prefix)
|
12
|
+
prefix = opts[:prefix]
|
13
|
+
else
|
14
|
+
# Prefix is exclusive
|
15
|
+
if opts[:gtto]
|
16
|
+
prefix = "gtto"
|
17
|
+
end
|
18
|
+
|
19
|
+
if opts[:gtfrom]
|
20
|
+
prefix = "gtfrom"
|
21
|
+
end
|
22
|
+
|
23
|
+
if opts[:t]
|
24
|
+
prefix = "t"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
output = [prefix, self].join(" ").parameterize(opts[:sep])
|
10
29
|
opts[:upcase] ? output.upcase : output.downcase
|
11
30
|
end
|
12
31
|
|
data/spec/models/string_spec.rb
CHANGED
@@ -36,16 +36,6 @@ describe "String" do
|
|
36
36
|
" +a- .,.,, . &&()@, ##b . . c#-d ".tagify.should eq "A-B-C-D"
|
37
37
|
end
|
38
38
|
|
39
|
-
context "up case and down case" do
|
40
|
-
it "defaults to upcase" do
|
41
|
-
"a".tagify.should eq "A"
|
42
|
-
end
|
43
|
-
|
44
|
-
it "recognizes downcase option" do
|
45
|
-
"A".tagify(:upcase => false).should eq "a"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
39
|
context "with a bang" do
|
50
40
|
it "changes the value of the string" do
|
51
41
|
s = "a,b".tagify!
|
@@ -57,4 +47,48 @@ describe "String" do
|
|
57
47
|
s.should eq "a-b"
|
58
48
|
end
|
59
49
|
end
|
50
|
+
|
51
|
+
context "options" do
|
52
|
+
context "up case and down case" do
|
53
|
+
it "defaults to upcase" do
|
54
|
+
"a".tagify.should eq "A"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "recognizes downcase option" do
|
58
|
+
"A".tagify(:upcase => false).should eq "a"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
it "uses indicated separator character" do
|
63
|
+
"a b".tagify(:sep => "+").should eq "A+B"
|
64
|
+
end
|
65
|
+
|
66
|
+
context "indicated prefix" do
|
67
|
+
it "uses indicated prefix" do
|
68
|
+
"a b".tagify(:prefix => "ho-ho-ho").should eq "HO-HO-HO-A-B"
|
69
|
+
end
|
70
|
+
|
71
|
+
it "uses indicated prefix and smashes other prefixes" do
|
72
|
+
"a b".tagify(:prefix => "ho-ho-ho", :gtto => true).should eq "HO-HO-HO-A-B"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "gt tag" do
|
77
|
+
it "puts a t tag if indicated" do
|
78
|
+
"Cleveland".tagify(:t => true).should eq "T-CLEVELAND"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "puts a gtto tag if indicated" do
|
82
|
+
"Cleveland".tagify(:gtto => true).should eq "GTTO-CLEVELAND"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "puts a gtfrom tag if indicated" do
|
86
|
+
"Cleveland".tagify(:gtfrom => true).should eq "GTFROM-CLEVELAND"
|
87
|
+
end
|
88
|
+
|
89
|
+
it "puts a gt tag with proper case if indicated" do
|
90
|
+
"Cleveland".tagify(:gtfrom => true, :upcase => false).should eq "gtfrom-cleveland"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
60
94
|
end
|