nrser 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/nrser.rb +98 -98
- data/lib/nrser/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20bf26571dcbd1728392f3c893af504e485766f7
|
4
|
+
data.tar.gz: eb0517a62c2a5388b3a630d4e2bbc7054efbbfda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4002c2dfaeae3ea5c9ca01f8cc34fc5c0800452668040cf5367e2eb20ce1521f25466cca25f5fa807a3d5a858b0b88392a50756d0b116464391bcb27789f5ac
|
7
|
+
data.tar.gz: e1b2af920dfd54ac531d07ba34fe86cf1993311affe4958cc470d900af8b587d437b390c881f1ef93f951e1567c9e75b2b5d1e3001d8aca1f40d7b46e56f914c
|
data/lib/nrser.rb
CHANGED
@@ -1,115 +1,115 @@
|
|
1
1
|
require "nrser/version"
|
2
2
|
|
3
3
|
module NRSER
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
51
|
-
|
52
|
-
end # filter_repeated_blank_lines
|
52
|
+
out.join
|
53
|
+
end # filter_repeated_blank_lines
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
60
|
-
def self.tpl bnd, str
|
61
|
-
template bnd, str
|
62
|
-
end # tpl
|
60
|
+
alias_method :tpl, :template
|
63
61
|
|
64
|
-
|
65
|
-
|
66
|
-
|
62
|
+
def format_exception e
|
63
|
+
"#{ e.message } (#{ e.class }):\n #{ e.backtrace.join("\n ") }"
|
64
|
+
end
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
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
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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
|
-
|
114
|
-
end
|
114
|
+
end # class << self
|
115
115
|
end
|
data/lib/nrser/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2015-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|