content_truncate 0.0.3 → 0.0.4

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ContentTruncate
2
2
 
3
+ [![build status](https://secure.travis-ci.org/zlx/content-truncate.png)](https://travis-ci.org/zlx/content-truncate)
4
+
3
5
  smart truncate for content
4
6
 
5
7
  ## Installation
@@ -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.pop
7
+ sep = separators.shift
8
8
  if sep
9
- while sep
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
 
@@ -1,3 +1,3 @@
1
1
  module ContentTruncate
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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(20, "<br/>", "\n", " ")
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.3
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-17 00:00:00.000000000 Z
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport