vidibus-core_extensions 0.3.6 → 0.3.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -90,4 +90,24 @@ class String
90
90
  end
91
91
  end
92
92
  end
93
+
94
+ # Truncates string to given length while preserves whole words.
95
+ # If string exceeds given length, an ellipsis will be appended.
96
+ #
97
+ # Example:
98
+ #
99
+ # "O Brother, Where Art Thou?".snip(13) # => "O Brother, Where…"
100
+ #
101
+ def snip(length, ellipsis = "…")
102
+ return self if self.empty?
103
+ self.gsub(/^(.{#{length.to_i-1}})(.)([\w]*)(.*)/m) do
104
+ if $2 == " "
105
+ "#{$1}#{ellipsis}"
106
+ elsif $3.empty?
107
+ self
108
+ else
109
+ "#{$1}#{$2}#{$3}#{ellipsis}"
110
+ end
111
+ end
112
+ end
93
113
  end
@@ -75,4 +75,26 @@ describe "Vidibus::CoreExtensions::String" do
75
75
  string.should eql("Masao, Mutoh")
76
76
  end
77
77
  end
78
+
79
+ describe "snip" do
80
+ it "should truncate string to given length while preserving words" do
81
+ "O Brother, Where Art Thou?".snip(13).should eql("O Brother, Where…")
82
+ end
83
+
84
+ it "should return whole string if it fits in given length" do
85
+ "O Brother, Where Art Thou?".snip(100).should eql("O Brother, Where Art Thou?")
86
+ end
87
+
88
+ it "should return whole string if it equals length" do
89
+ "O Brother, Where Art Thou?".snip(26).should eql("O Brother, Where Art Thou?")
90
+ end
91
+
92
+ it "should return whole string if it equals length" do
93
+ "O Brother, Where Art Thou?".snip(11).should eql("O Brother,…")
94
+ end
95
+
96
+ it "should trim white space" do
97
+ "O Brother, Where Art Thou?".snip(11).should eql("O Brother,…")
98
+ end
99
+ end
78
100
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{vidibus-core_extensions}
8
- s.version = "0.3.6"
8
+ s.version = "0.3.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andre Pankratz"]
12
- s.date = %q{2010-09-23}
12
+ s.date = %q{2010-09-27}
13
13
  s.description = %q{Provides some extensions to the ruby core.}
14
14
  s.email = %q{andre@vidibus.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-core_extensions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 6
10
- version: 0.3.6
9
+ - 7
10
+ version: 0.3.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Pankratz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-23 00:00:00 +02:00
18
+ date: 2010-09-27 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21