stringex 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTgxMjBjNTlhYmEyMGIzZDYyZmI4MmU2M2UwMTU1OGJmZGZlMzQ0YQ==
4
+ OGQxMmNjMmIwMzU1MzY0YzdmMWEzZjhiYjI0MTk5ZGE4M2ZiODY0Mg==
5
5
  data.tar.gz: !binary |-
6
- Nzc2MjZlZWE3MDJhNDg3MDJjNGU3NTkyMzNlOWQxODU1NDFmMmMzNw==
6
+ YzkxMzZiYTBmZWM0YWUyYTMyYWY0MDgzNDRhM2IxNmJjZWM2MDZmOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZDEyZjQ0ZWI3OTM5MjBiOTY1N2IxNTY1MGQ4ZTQ2NGEwMmFjYTU2YjgxZmQ3
10
- YWM4ZGNlZDIyMzk4ZjNhNWM0YzZlODg4NmNmZjA2MzhiOWYwNWVkNGExNGIz
11
- Y2YzNDEwZTU0YjQyZWI0Y2M0YjQ5YTBmODEwN2U1NzI4MWY3OWU=
9
+ MmMxMmRmNTg3NGY0Nzg1M2Q2MWM4NTk5M2I0NjAxMjgwNmIwZjljZmEzZmNj
10
+ MGE4MWYwOGNjYTRkMzM5NzRhMTdjMzI3MWVhNzY4NzBkNTQ2ZTM0YWEzYzE1
11
+ ZmM1YWY2NmY0MzM5ZDk4MTc2MjQzNWVkZTU5NDNmNjZkZmNlMDM=
12
12
  data.tar.gz: !binary |-
13
- ZDdkOWJhMjFjYjc4YzE1NDc0ZGM1ZjNhMjRhNDAwZTA2MDkxMjE5ZjQwOWQx
14
- NmY2YWU3Yjg0MDkxMWM5OGNiYTdhNmNkODE3MDE5NzAxNDEwZWRkOWRiNTJj
15
- MzI3NzM1YTdlZmI5NThkNjRlODYzNDU2OTcyNzEyNTg0ZGRkZjk=
13
+ MTA2MjBkMTVkYWVlMzM0MDA4Y2JmODNjMGM4NWM5MjBkZjM3YmJiMmFkZjA4
14
+ YjQyNDBkOWMwMDE3YmI4ZDkwNjE1ZWQ2ODdjMzMzZTU4OTBmZjE1NDU2NTQ0
15
+ OGY4ZDA2MWU2MjUyYWM1NTZhNTNjZDRmOTk5NDYwZjcxNTQyMzY=
data/README.rdoc CHANGED
@@ -28,6 +28,8 @@ which will populate the <tt>url</tt> attribute on the object with the converted
28
28
  if you are routing solely on the generated slug as you will no longer
29
29
  be guaranteed to lookup the expected record based on a duplicate slug.</em>
30
30
  <tt>:limit</tt>:: If set, will limit length of url generated. Default is nil.
31
+ <tt>:truncate_words</tt>:: Used with :limit. If set to false, the url will be truncated to the last
32
+ whole word before the limit was reached. Default is true.
31
33
 
32
34
  In order to use the generated url attribute, you will probably want to override <tt>to_param</tt> like so, in your Model:
33
35
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.2
1
+ 2.2.0
@@ -14,7 +14,8 @@ module Stringex
14
14
  :exclude,
15
15
  :force_downcase,
16
16
  :limit,
17
- :replace_whitespace_with
17
+ :replace_whitespace_with,
18
+ :truncate_words,
18
19
  ].inject(Hash.new){|m, x| m[x] = settings.send(x); m}
19
20
  end
20
21
 
@@ -11,7 +11,8 @@ module Stringex
11
11
  :exclude => [],
12
12
  :force_downcase => true,
13
13
  :limit => nil,
14
- :replace_whitespace_with => "-"
14
+ :replace_whitespace_with => "-",
15
+ :truncate_words => true
15
16
  }
16
17
  end
17
18
  end
@@ -94,10 +94,31 @@ module Stringex
94
94
  end
95
95
 
96
96
  # Returns the string limited in size to the value of limit.
97
- def limit(limit = nil)
98
- limit.nil? ? self : self[0...limit]
97
+ def limit(limit = nil, truncate_words = true)
98
+ if limit.nil?
99
+ self
100
+ else
101
+ truncate_words == false ? self.whole_word_limit(limit) : self[0...limit]
102
+ end
99
103
  end
100
104
 
105
+ def whole_word_limit(limit)
106
+ whole_words = []
107
+ words = self.split('-')
108
+
109
+ words.each do |word|
110
+ if word.size > limit
111
+ break
112
+ else
113
+ whole_words << word
114
+ limit -= (word.size + 1)
115
+ end
116
+ end
117
+
118
+ whole_words.join('-')
119
+ end
120
+
121
+
101
122
  # Performs multiple text manipulations. Essentially a shortcut for typing them all. View source
102
123
  # below to see which methods are run.
103
124
  def remove_formatting(options = {})
@@ -167,7 +188,7 @@ module Stringex
167
188
  dummy = remove_formatting(options).
168
189
  replace_whitespace(whitespace_replacement_token).
169
190
  collapse("-").
170
- limit(options[:limit])
191
+ limit(options[:limit], options[:truncate_words])
171
192
  dummy.downcase! unless options[:force_downcase] == false
172
193
  dummy
173
194
  end
data/stringex.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "stringex"
8
- s.version = "2.1.2"
8
+ s.version = "2.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Russell Norris"]
12
- s.date = "2013-11-25"
12
+ s.date = "2014-02-03"
13
13
  s.description = "Some [hopefully] useful extensions to Ruby's String class. Stringex is made up of three libraries: ActsAsUrl [permalink solution with better character translation], Unidecoder [Unicode to ASCII transliteration], and StringExtensions [miscellaneous helper methods for the String class]."
14
14
  s.email = "rsl@luckysneaks.com"
15
15
  s.extra_rdoc_files = [
@@ -326,4 +326,22 @@ class ActsAsUrlIntegrationTest < Test::Unit::TestCase
326
326
  @doc = Document.create(:title => "a b/c d")
327
327
  assert_equal "a-b/c-d", @doc.url
328
328
  end
329
+
330
+ def test_should_truncate_words_by_default
331
+ Document.class_eval do
332
+ acts_as_url :title, :limit => 20
333
+ end
334
+
335
+ @doc = Document.create(:title => "title with many whole words")
336
+ assert_equal 'title-with-many-whol', @doc.url
337
+ end
338
+
339
+ def test_should_not_truncate_words
340
+ Document.class_eval do
341
+ acts_as_url :title, :limit => 20, :truncate_words => false
342
+ end
343
+
344
+ @doc = Document.create(:title => "title with many whole words")
345
+ assert_equal 'title-with-many', @doc.url
346
+ end
329
347
  end
@@ -24,7 +24,7 @@ class DanishYAMLLocalizationTest < Test::Unit::TestCase
24
24
  "100%" => "100 procent",
25
25
  "cost+tax" => "cost plus tax",
26
26
  "batman/robin fan fiction" => "batman skråstreg robin fan fiction",
27
- "dial *69" => "dial stjerne 69",
27
+ "dial *69" => "dial stjerne 69"
28
28
  # " i leave whitespace on ends unchanged " => " i leave whitespace on ends unchanged "
29
29
  }.each do |original, converted|
30
30
  define_method "test_character_conversion: '#{original}'" do
@@ -97,6 +97,11 @@ class StringExtensionsTest < Test::Unit::TestCase
97
97
  end
98
98
  end
99
99
 
100
+ def test_to_url_with_danish_characters
101
+ Stringex::Localization.locale = :da
102
+ assert_equal "roedgroed-med-floede", "Rødgrød med fløde".to_url
103
+ end
104
+
100
105
  def test_to_url_with_excludes
101
106
  assert_equal "So Fucking Special", "So Fucking Special".to_url(:exclude => "So Fucking Special")
102
107
  end
@@ -154,11 +159,11 @@ class StringExtensionsTest < Test::Unit::TestCase
154
159
 
155
160
  def test_localized_vulgar_fractions_conversion
156
161
  Stringex::Localization.backend = :internal
157
- Stringex::Localization.store_translations :de, :vulgar_fractions, {
162
+ Stringex::Localization.store_translations :da, :vulgar_fractions, {
158
163
  :one_fourth => "en fjerdedel",
159
164
  :half => "en halv"
160
165
  }
161
- Stringex::Localization.locale = :de
166
+ Stringex::Localization.locale = :da
162
167
 
163
168
  {
164
169
  "&frac14;" => "en fjerdedel",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stringex
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Norris
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-25 00:00:00.000000000 Z
11
+ date: 2014-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord