nrser 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c8731154cddca4f094d4d430d8250c736787828
4
- data.tar.gz: 8e721938ec4c42a38710af59b3a5d2523e7a2a72
3
+ metadata.gz: 5613f267360bdb2ff8af4120116b59d1e348f90c
4
+ data.tar.gz: 5492a2ffd330274e01e7ce83110877c55adae7e4
5
5
  SHA512:
6
- metadata.gz: e2944482bc327588d1a9320768176b26ccb17409a76c4a39f39dac94b5dc9f77c1bd5799009b7eb49dfdeb670b055622ede08906169525d3aee0a81f49301610
7
- data.tar.gz: 274e7e3bf113716008789a1104a3aed27be1338708565587461239a61a030d3a714c13a9f72e137542dc9e265c2f4f157d3242f3619b8ad85402db9d057874c3
6
+ metadata.gz: dc4f080da2d7ab98526674983acc1d4f1c08d1319ff96d2ccc6740dd4b6ab4dbd692c68e36fc51e46db1286131660ccac2fb03493cc7e736ced9a89175ea0288
7
+ data.tar.gz: 1aa2be5f247e7b23808e1ee07d6116627f54182d3507cfffb13f6dd76171d0ade34ca6b83c7dbc44bca9aea15d3024ca4e21fb5ddae82d209f6b87d4a192e794
data/lib/nrser/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NRSER
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/nrser.rb CHANGED
@@ -28,6 +28,10 @@ module NRSER
28
28
  def indent
29
29
  NRSER.indent self
30
30
  end
31
+
32
+ def truncate *args
33
+ NRSER.truncate self, *args
34
+ end
31
35
  end # refine String
32
36
 
33
37
  refine Exception do
@@ -107,4 +111,41 @@ module NRSER
107
111
  str.gsub(re, indent_string * amount)
108
112
  end
109
113
 
114
+ # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
115
+ #
116
+ # 'Once upon a time in a world far far away'.truncate(27)
117
+ # # => "Once upon a time in a wo..."
118
+ #
119
+ # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
120
+ #
121
+ # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
122
+ # # => "Once upon a time in a..."
123
+ #
124
+ # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
125
+ # # => "Once upon a time in a..."
126
+ #
127
+ # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
128
+ # for a total length not exceeding <tt>length</tt>:
129
+ #
130
+ # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
131
+ # # => "And they f... (continued)"
132
+ #
133
+ # adapted from
134
+ #
135
+ # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/filters.rb#L46>
136
+ #
137
+ def self.truncate(str, truncate_at, options = {})
138
+ return str.dup unless str.length > truncate_at
139
+
140
+ omission = options[:omission] || '...'
141
+ length_with_room_for_omission = truncate_at - omission.length
142
+ stop = \
143
+ if options[:separator]
144
+ str.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
145
+ else
146
+ length_with_room_for_omission
147
+ end
148
+
149
+ "#{str[0, stop]}#{omission}"
150
+ end
110
151
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ using NRSER
4
+
5
+ describe "NRSER.truncate" do
6
+ it "truncates a string that's longer than length" do
7
+ expect( NRSER.truncate "blah blah blah blah", 10 ).to eq "blah bl..."
8
+ end
9
+
10
+ it "leaves a string alone that's shorter or equal to lenght" do
11
+ expect( NRSER.truncate "blah", 10 ).to eq "blah"
12
+ end
13
+
14
+ it "refines String" do
15
+ expect( "blah blah blah blah".truncate 10 ).to eq "blah bl..."
16
+ end
17
+ end # indent
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nrser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-18 00:00:00.000000000 Z
11
+ date: 2015-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,7 @@ files:
77
77
  - spec/nrser/format_exception_spec.rb
78
78
  - spec/nrser/indent_spec.rb
79
79
  - spec/nrser/template_spec.rb
80
+ - spec/nrser/truncate_spec.rb
80
81
  - spec/nrser_spec.rb
81
82
  - spec/spec_helper.rb
82
83
  - tmp/.gitkeep
@@ -112,5 +113,6 @@ test_files:
112
113
  - spec/nrser/format_exception_spec.rb
113
114
  - spec/nrser/indent_spec.rb
114
115
  - spec/nrser/template_spec.rb
116
+ - spec/nrser/truncate_spec.rb
115
117
  - spec/nrser_spec.rb
116
118
  - spec/spec_helper.rb