content_truncate 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/lib/content_truncate.rb +17 -12
- data/lib/content_truncate/version.rb +1 -1
- data/test/content_truncate_test.rb +2 -2
- metadata +2 -2
data/README.md
CHANGED
data/lib/content_truncate.rb
CHANGED
@@ -4,23 +4,28 @@ module ContentTruncate
|
|
4
4
|
module String
|
5
5
|
def content_truncate limit_length, *separators
|
6
6
|
sub_string = self.dup
|
7
|
-
sep = separators.
|
7
|
+
sep = separators.shift
|
8
8
|
if sep
|
9
|
-
|
10
|
-
index = sub_string.index(sep)
|
11
|
-
if index <= limit_length
|
12
|
-
while index && index <= limit_length
|
13
|
-
prev_index, index = index, sub_string.index(sep, index+1)
|
14
|
-
end
|
15
|
-
return sub_string[0...prev_index].strip
|
16
|
-
else
|
17
|
-
sep = separators.pop
|
18
|
-
end
|
19
|
-
end
|
9
|
+
sub_string.send(:smart_truncate, limit_length, sep, separators)
|
20
10
|
else
|
21
11
|
sub_string.truncate(limit_length, separator: sep)
|
22
12
|
end
|
23
13
|
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def smart_truncate limit_length, sep, *separators
|
17
|
+
while sep
|
18
|
+
index = self.index(sep)
|
19
|
+
if index <= limit_length
|
20
|
+
while index && index <= limit_length
|
21
|
+
prev_index, index = index, self.index(sep, index+1)
|
22
|
+
end
|
23
|
+
return self[0...prev_index].strip
|
24
|
+
else
|
25
|
+
sep = separators.shift
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
@@ -4,7 +4,7 @@ require_relative '../lib/content_truncate'
|
|
4
4
|
class ContentTruncateTest < Test::Unit::TestCase
|
5
5
|
|
6
6
|
def mock_string
|
7
|
-
"Truncate a string down to \n x characters. <br/> Beware of truncating text that has HTML elements in it."
|
7
|
+
"Truncate a string down to \n x characters. <br/> Beware of truncating \n text that has HTML elements in it."
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_content_truncate_for_no_separator
|
@@ -16,6 +16,6 @@ class ContentTruncateTest < Test::Unit::TestCase
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_content_truncate_for_separators
|
19
|
-
assert_equal "Truncate a string", mock_string.content_truncate(
|
19
|
+
assert_equal "Truncate a string down to", mock_string.content_truncate(60, "\n", "<br/>", " ")
|
20
20
|
end
|
21
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: content_truncate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|