rahoulb-rujitsu 0.1.7 → 0.1.8
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/CHANGELOG +4 -0
- data/README.rdoc +5 -0
- data/Rakefile +1 -1
- data/lib/rujitsu/string.rb +20 -0
- data/rujitsu.gemspec +2 -2
- data/spec/string_spec.rb +78 -0
- metadata +2 -2
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -41,6 +41,11 @@ The String class has an extension that strips out URL-unfriendly characters.
|
|
41
41
|
|
42
42
|
""$%hello!@ 123 there'".to_url # => "hello-123-there"
|
43
43
|
|
44
|
+
=== Truncating strings
|
45
|
+
|
46
|
+
The String class has an extension that truncates it to a customisable length with a customisable suffix.
|
47
|
+
|
48
|
+
"this is a string".truncate(:length => 15) # => "this is a st..."
|
44
49
|
|
45
50
|
=== Grammar
|
46
51
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('rujitsu', '0.1.
|
5
|
+
Echoe.new('rujitsu', '0.1.8') do | config |
|
6
6
|
config.description = 'Various helper methods to smooth over Ruby development'
|
7
7
|
config.url = 'http://github.com/rahoub/rujitsu'
|
8
8
|
config.author = 'Brightbox Systems Ltd'
|
data/lib/rujitsu/string.rb
CHANGED
@@ -1,6 +1,26 @@
|
|
1
1
|
class String
|
2
2
|
# Return a string that can be used as part of a url
|
3
|
+
# replaces basic "bad" characters with "-"
|
3
4
|
def to_url
|
4
5
|
self.downcase.gsub(/[^\-0-9a-z ]/, '').split.join('-')
|
5
6
|
end
|
7
|
+
|
8
|
+
# Truncates a string to the specified length,
|
9
|
+
# and appends suffix if required
|
10
|
+
#
|
11
|
+
# Options:
|
12
|
+
# * +length+ length to truncate string to. Includes the suffix in the length. Default is 50.
|
13
|
+
# * +suffix+ suffix to append to truncated string. Pass "" or false for no suffix. Default is "...".
|
14
|
+
#
|
15
|
+
def truncate opts = {}
|
16
|
+
opts[:length] ||= 50
|
17
|
+
opts[:suffix] = opts.has_key?(:suffix) ? opts[:suffix] : "..."
|
18
|
+
opts[:suffix] ||= ""
|
19
|
+
opts[:length] -= (opts[:suffix].length+1)
|
20
|
+
if opts[:length] > 0
|
21
|
+
self.length > opts[:length] ? self[0..opts[:length]] + opts[:suffix] : self
|
22
|
+
else
|
23
|
+
opts[:suffix][0..(opts[:length] += opts[:suffix].length)]
|
24
|
+
end
|
25
|
+
end
|
6
26
|
end
|
data/rujitsu.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{rujitsu}
|
5
|
-
s.version = "0.1.
|
5
|
+
s.version = "0.1.8"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Brightbox Systems Ltd"]
|
9
|
-
s.date = %q{
|
9
|
+
s.date = %q{2009-01-24}
|
10
10
|
s.description = %q{Various helper methods to smooth over Ruby development}
|
11
11
|
s.email = %q{hello@brightbox.co.uk}
|
12
12
|
s.extra_rdoc_files = ["CHANGELOG", "lib/rujitsu/fixnum.rb", "lib/rujitsu/grammar.rb", "lib/rujitsu/numeric.rb", "lib/rujitsu/range.rb", "lib/rujitsu/string.rb", "lib/rujitsu.rb", "README.rdoc", "tasks/rspec.rake"]
|
data/spec/string_spec.rb
CHANGED
@@ -18,4 +18,82 @@ describe String, "to_url" do
|
|
18
18
|
"post-number-12345".to_url.should == "post-number-12345"
|
19
19
|
end
|
20
20
|
|
21
|
+
end
|
22
|
+
|
23
|
+
describe String, "truncate" do
|
24
|
+
|
25
|
+
it "should return shorter string unmodified with no params" do
|
26
|
+
str = 30.random_characters
|
27
|
+
# default length is 50
|
28
|
+
str.truncate.should == str
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return shorter string unmodified with length param" do
|
32
|
+
str = "0123456789" # length 10
|
33
|
+
trunced = str.truncate(:length => 30)
|
34
|
+
trunced.length.should == 10
|
35
|
+
trunced.should == str
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return shorter string unmodified with suffix param" do
|
39
|
+
str = "0123456789" # length 10
|
40
|
+
trunced = str.truncate(:suffix => "abcd")
|
41
|
+
trunced.length.should == 10
|
42
|
+
trunced.should == str
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return shorter string unmodified with length and suffix params" do
|
46
|
+
str = "0123456789" # length 10
|
47
|
+
trunced = str.truncate(:length => 30, :suffix => "abcd")
|
48
|
+
trunced.length.should == 10
|
49
|
+
trunced.should == str
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should truncate longer string with no params" do
|
53
|
+
str = "this is a string with a really long length just to get above 50 characters"
|
54
|
+
# default length is 50
|
55
|
+
# default suffix is "..."
|
56
|
+
trunced = str.truncate
|
57
|
+
trunced.length.should == 50
|
58
|
+
trunced.should == "this is a string with a really long length just..."
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should truncate longer string with length param" do
|
62
|
+
str = "123456789123456789" # length 11
|
63
|
+
trunced = str.truncate(:length => 10)
|
64
|
+
trunced.length.should == 10
|
65
|
+
trunced.should == "1234567..."
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should truncate longer string with suffix param" do
|
69
|
+
str = "this is a string with a really long length just to get above 50 characters"
|
70
|
+
# default length is 50
|
71
|
+
trunced = str.truncate(:suffix => "..abc")
|
72
|
+
trunced.length.should == 50
|
73
|
+
trunced.should == "this is a string with a really long length ju..abc"
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should truncate longer string with length and suffix param" do
|
77
|
+
str = "123456789123456789" # length 11
|
78
|
+
trunced = str.truncate(:length => 10, :suffix => "abde")
|
79
|
+
trunced.length.should == 10
|
80
|
+
trunced.should == "123456abde"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should return truncated string with no suffix if suffix param is false" do
|
84
|
+
str = "this is a string with a really long length just to get above 50 characters"
|
85
|
+
# default length is 50
|
86
|
+
trunced = str.truncate(:suffix => false)
|
87
|
+
trunced.length.should == 50
|
88
|
+
trunced.should == "this is a string with a really long length just to"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should return a string of length specified even when the suffix is longer" do
|
92
|
+
str = "this is a string" # => 16
|
93
|
+
suff = "this is a longer string" # => 23
|
94
|
+
trunced = str.truncate(:suffix => suff, :length => 10)
|
95
|
+
trunced.should == "this is a "
|
96
|
+
trunced.length.should == 10
|
97
|
+
end
|
98
|
+
|
21
99
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rahoulb-rujitsu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brightbox Systems Ltd
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-24 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|