nrser 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nrser.rb +98 -98
  3. data/lib/nrser/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fb0f908c7143a3d04705284022e9bf269ae148c
4
- data.tar.gz: e70e72f42055266a81019e6faae143b88dff6cbd
3
+ metadata.gz: 20bf26571dcbd1728392f3c893af504e485766f7
4
+ data.tar.gz: eb0517a62c2a5388b3a630d4e2bbc7054efbbfda
5
5
  SHA512:
6
- metadata.gz: 194b3069a1468b6d763cfbeb0b47d4a0e12857483fb2b311a9afe72ce51406877ce76e67ba57ad1ef54977148b602d32dd9f2225d021713097d2b1b49133e7c6
7
- data.tar.gz: a92470823133fa31f9c154bee199828f754f7be31350a00e748331a8c5ba4cb62a8b85d7907306a790ee6c82fcbe02be4475ffdb7c523a40ff201658f00dbd7b
6
+ metadata.gz: b4002c2dfaeae3ea5c9ca01f8cc34fc5c0800452668040cf5367e2eb20ce1521f25466cca25f5fa807a3d5a858b0b88392a50756d0b116464391bcb27789f5ac
7
+ data.tar.gz: e1b2af920dfd54ac531d07ba34fe86cf1993311affe4958cc470d900af8b587d437b390c881f1ef93f951e1567c9e75b2b5d1e3001d8aca1f40d7b46e56f914c
@@ -1,115 +1,115 @@
1
1
  require "nrser/version"
2
2
 
3
3
  module NRSER
4
- def self.unblock str
5
- parts = str.split(/\n\s+/)
6
- if m = parts[0].match(/^\s+/)
7
- parts[0] = parts[0][m.end(0)..-1]
8
- end
9
- if m = parts[-1].match(/\s+$/)
10
- parts[-1] = parts[-1][0..m.begin(0)]
11
- end
12
- parts.join ' '
13
- end # unblock
4
+ class << self
5
+
6
+ # turn a multi-line string into a single line, collapsing whitespace
7
+ # to a single space.
8
+ #
9
+ # same as ActiveSupport's String.squish, adapted from there.
10
+ def squish str
11
+ str.gsub(/[[:space:]]+/, ' ').strip
12
+ end # squish
14
13
 
15
- def self.common_prefix strings
16
- raise ArgumentError.new("argument can't be empty") if strings.empty?
17
- sorted = strings.sort.reject {|line| line == "\n"}
18
- i = 0
19
- while sorted.first[i] == sorted.last[i] &&
20
- i < [sorted.first.length, sorted.last.length].min
21
- i = i + 1
22
- end
23
- strings.first[0...i]
24
- end # common_prefix
14
+ alias_method :unblock, :squish
25
15
 
26
- def self.dedent str
27
- return str if str.empty?
28
- lines = str.lines
29
- indent = common_prefix(lines).match(/^\s*/)[0]
30
- return str if indent.empty?
31
- lines.map {|line|
32
- line = line[indent.length..line.length] if line.start_with? indent
33
- }.join
34
- end # dedent
16
+ def common_prefix strings
17
+ raise ArgumentError.new("argument can't be empty") if strings.empty?
18
+ sorted = strings.sort.reject {|line| line == "\n"}
19
+ i = 0
20
+ while sorted.first[i] == sorted.last[i] &&
21
+ i < [sorted.first.length, sorted.last.length].min
22
+ i = i + 1
23
+ end
24
+ strings.first[0...i]
25
+ end # common_prefix
26
+
27
+ def dedent str
28
+ return str if str.empty?
29
+ lines = str.lines
30
+ indent = common_prefix(lines).match(/^\s*/)[0]
31
+ return str if indent.empty?
32
+ lines.map {|line|
33
+ line = line[indent.length..line.length] if line.start_with? indent
34
+ }.join
35
+ end # dedent
35
36
 
36
- def self.filter_repeated_blank_lines str
37
- out = []
38
- lines = str.lines
39
- skipping = false
40
- str.lines.each do |line|
41
- if line =~ /^\s*$/
42
- unless skipping
37
+ def filter_repeated_blank_lines str
38
+ out = []
39
+ lines = str.lines
40
+ skipping = false
41
+ str.lines.each do |line|
42
+ if line =~ /^\s*$/
43
+ unless skipping
44
+ out << line
45
+ end
46
+ skipping = true
47
+ else
48
+ skipping = false
43
49
  out << line
44
50
  end
45
- skipping = true
46
- else
47
- skipping = false
48
- out << line
49
51
  end
50
- end
51
- out.join
52
- end # filter_repeated_blank_lines
52
+ out.join
53
+ end # filter_repeated_blank_lines
53
54
 
54
- def self.template bnd, str
55
- require 'erb'
56
- filter_repeated_blank_lines ERB.new(dedent(str)).result(bnd)
57
- end # template
55
+ def template bnd, str
56
+ require 'erb'
57
+ filter_repeated_blank_lines ERB.new(dedent(str)).result(bnd)
58
+ end # template
58
59
 
59
- # alias
60
- def self.tpl bnd, str
61
- template bnd, str
62
- end # tpl
60
+ alias_method :tpl, :template
63
61
 
64
- def self.format_exception e
65
- "#{ e.message } (#{ e.class }):\n #{ e.backtrace.join("\n ") }"
66
- end
62
+ def format_exception e
63
+ "#{ e.message } (#{ e.class }):\n #{ e.backtrace.join("\n ") }"
64
+ end
67
65
 
68
- # adapted from acrive_support 4.2.0
69
- #
70
- # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/indent.rb>
71
- #
72
- def self.indent str, amount = 2, indent_string=nil, indent_empty_lines=false
73
- indent_string = indent_string || str[/^[ \t]/] || ' '
74
- re = indent_empty_lines ? /^/ : /^(?!$)/
75
- str.gsub(re, indent_string * amount)
76
- end
66
+ # adapted from acrive_support 4.2.0
67
+ #
68
+ # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/indent.rb>
69
+ #
70
+ def indent str, amount = 2, indent_string=nil, indent_empty_lines=false
71
+ indent_string = indent_string || str[/^[ \t]/] || ' '
72
+ re = indent_empty_lines ? /^/ : /^(?!$)/
73
+ str.gsub(re, indent_string * amount)
74
+ end
77
75
 
78
- # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
79
- #
80
- # 'Once upon a time in a world far far away'.truncate(27)
81
- # # => "Once upon a time in a wo..."
82
- #
83
- # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
84
- #
85
- # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
86
- # # => "Once upon a time in a..."
87
- #
88
- # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
89
- # # => "Once upon a time in a..."
90
- #
91
- # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
92
- # for a total length not exceeding <tt>length</tt>:
93
- #
94
- # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
95
- # # => "And they f... (continued)"
96
- #
97
- # adapted from
98
- #
99
- # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/filters.rb#L46>
100
- #
101
- def self.truncate(str, truncate_at, options = {})
102
- return str.dup unless str.length > truncate_at
76
+ # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
77
+ #
78
+ # 'Once upon a time in a world far far away'.truncate(27)
79
+ # # => "Once upon a time in a wo..."
80
+ #
81
+ # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
82
+ #
83
+ # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
84
+ # # => "Once upon a time in a..."
85
+ #
86
+ # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
87
+ # # => "Once upon a time in a..."
88
+ #
89
+ # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
90
+ # for a total length not exceeding <tt>length</tt>:
91
+ #
92
+ # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
93
+ # # => "And they f... (continued)"
94
+ #
95
+ # adapted from
96
+ #
97
+ # <https://github.com/rails/rails/blob/7847a19f476fb9bee287681586d872ea43785e53/activesupport/lib/active_support/core_ext/string/filters.rb#L46>
98
+ #
99
+ def truncate(str, truncate_at, options = {})
100
+ return str.dup unless str.length > truncate_at
103
101
 
104
- omission = options[:omission] || '...'
105
- length_with_room_for_omission = truncate_at - omission.length
106
- stop = \
107
- if options[:separator]
108
- str.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
109
- else
110
- length_with_room_for_omission
111
- end
102
+ omission = options[:omission] || '...'
103
+ length_with_room_for_omission = truncate_at - omission.length
104
+ stop = \
105
+ if options[:separator]
106
+ str.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
107
+ else
108
+ length_with_room_for_omission
109
+ end
110
+
111
+ "#{str[0, stop]}#{omission}"
112
+ end
112
113
 
113
- "#{str[0, stop]}#{omission}"
114
- end
114
+ end # class << self
115
115
  end
@@ -1,3 +1,3 @@
1
1
  module NRSER
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
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.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-21 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler