nuggets 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog +6 -0
- data/README +2 -2
- data/lib/nuggets/array/format.rb +1 -1
- data/lib/nuggets/array/histogram_mixin.rb +4 -4
- data/lib/nuggets/hash/in_order.rb +2 -2
- data/lib/nuggets/hash/nest_mixin.rb +2 -2
- data/lib/nuggets/hash/unroll_mixin.rb +4 -4
- data/lib/nuggets/io/redirect_mixin.rb +2 -4
- data/lib/nuggets/log_parser/rails.rb +18 -18
- data/lib/nuggets/string/format.rb +5 -0
- data/lib/nuggets/string/format_mixin.rb +59 -0
- data/lib/nuggets/string/highlight.rb +5 -0
- data/lib/nuggets/{io/null_mixin.rb → string/highlight_mixin.rb} +32 -12
- data/lib/nuggets/version.rb +1 -1
- data/spec/nuggets/array/format_spec.rb +1 -1
- data/spec/nuggets/env/set_spec.rb +2 -2
- data/spec/nuggets/hash/at_spec.rb +1 -1
- data/spec/nuggets/hash/deep_fetch_spec.rb +1 -1
- data/spec/nuggets/hash/deep_merge_spec.rb +16 -16
- data/spec/nuggets/hash/seen_spec.rb +1 -1
- data/spec/nuggets/hash/unroll_spec.rb +6 -18
- data/spec/nuggets/object/blank_spec.rb +2 -2
- data/spec/nuggets/object/singleton_class_spec.rb +3 -2
- data/spec/nuggets/string/format_spec.rb +85 -0
- data/spec/nuggets/string/highlight_spec.rb +43 -0
- metadata +14 -9
- data/lib/nuggets/io/null.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5af6231048355058097a53aac9443148a84e7bbc
|
4
|
+
data.tar.gz: c6976ef57d142740afed95d49bfdd366992d5bbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea703b2f4bd23a05391a67d6ce8694fa63ab59d02d8e7f7766de2516838a1a7bd8ce31c6a42e09ea78cfb4ddc4009956852e3845ec61f4eccbe6d6f793ef1259
|
7
|
+
data.tar.gz: 702b6c308c17b5fd8e54f7538bf6057f573cadf60326ace6a55b40cbe805e45b67e581a13a9618e5900f8ec461512d85ff287fa2e39a6ee83f532cf922844510
|
data/ChangeLog
CHANGED
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
== VERSION
|
4
4
|
|
5
|
-
This documentation refers to nuggets version 1.
|
5
|
+
This documentation refers to nuggets version 1.5.0.
|
6
6
|
|
7
7
|
|
8
8
|
== DESCRIPTION
|
@@ -58,7 +58,7 @@ Travis CI:: https://travis-ci.org/blackwinter/nuggets
|
|
58
58
|
|
59
59
|
== LICENSE AND COPYRIGHT
|
60
60
|
|
61
|
-
Copyright (C) 2007-
|
61
|
+
Copyright (C) 2007-2016 Jens Wille
|
62
62
|
|
63
63
|
nuggets is free software: you can redistribute it and/or modify it
|
64
64
|
under the terms of the GNU Affero General Public License as published by
|
data/lib/nuggets/array/format.rb
CHANGED
@@ -40,7 +40,7 @@ class Array
|
|
40
40
|
# Applies to string argument accordingly: First string in _array_ applied to
|
41
41
|
# _str_; empty string if _str_ is empty.
|
42
42
|
def %(args)
|
43
|
-
opts = { :
|
43
|
+
opts = { sep: ', ' }
|
44
44
|
opts.update(pop) if last.is_a?(::Hash)
|
45
45
|
|
46
46
|
default = lambda { |n| ['%s'] * n * opts[:sep] }
|
@@ -39,10 +39,10 @@ module Nuggets
|
|
39
39
|
#
|
40
40
|
# The "numeric" variants format the item as a (decimal) number.
|
41
41
|
FORMATS = {
|
42
|
-
:
|
43
|
-
:
|
44
|
-
:
|
45
|
-
:
|
42
|
+
default: '%-*s [%s]%*s %*d',
|
43
|
+
percent: '%-*s [%s]%*s %*d (%.2f%%)',
|
44
|
+
numeric: '%*d [%s]%*s %*d',
|
45
|
+
numeric_percent: '%*d [%s]%*s %*d (%.2f%%)'
|
46
46
|
}
|
47
47
|
|
48
48
|
# Encapsulates a #histogram item and provides the following attributes (see
|
@@ -34,8 +34,8 @@ class Hash
|
|
34
34
|
# Returns <tt>hash#to_a</tt>, in forced order (cf. Array#in_order).
|
35
35
|
#
|
36
36
|
# Examples:
|
37
|
-
# { :
|
38
|
-
# { :
|
37
|
+
# { a: 1, b: 2, c: 3 }.in_order(:b, :c) #=> [[:b, 2], [:c, 3], [:a, 1]]
|
38
|
+
# { a: 1, b: 2, c: 3 }.in_order(:b, :d) #=> [[:b, 2], [:a, 1], [:c, 3]]
|
39
39
|
def in_order(*ordered)
|
40
40
|
keys.in_order(*ordered).map { |key| [key, self[key]] }
|
41
41
|
end
|
@@ -45,8 +45,8 @@ module Nuggets
|
|
45
45
|
# Example:
|
46
46
|
#
|
47
47
|
# hash = Hash.nest(2)
|
48
|
-
# hash[:foo][:bar][:a] = { :
|
49
|
-
# hash[:foo][:bar][:b] = { :
|
48
|
+
# hash[:foo][:bar][:a] = { x: 1, y: 2 }
|
49
|
+
# hash[:foo][:bar][:b] = { x: 0, y: 3 }
|
50
50
|
# hash
|
51
51
|
# #=> {:foo=>{:bar=>{:b=>{:y=>3, :x=>0}, :a=>{:y=>2, :x=>1}}}}
|
52
52
|
def nest(depth = 0, value = default = true)
|
@@ -30,7 +30,7 @@ module Nuggets
|
|
30
30
|
|
31
31
|
# call-seq:
|
32
32
|
# hash.unroll(*value_keys) => anArray
|
33
|
-
# hash.unroll(*value_keys, :
|
33
|
+
# hash.unroll(*value_keys, sort_by: ...) => anArray
|
34
34
|
# hash.unroll(*value_keys) { |value_hash| ... } => anArray
|
35
35
|
#
|
36
36
|
# "Unrolls" a nested hash, so that each path through _hash_ results in a
|
@@ -46,13 +46,13 @@ module Nuggets
|
|
46
46
|
#
|
47
47
|
# Examples:
|
48
48
|
#
|
49
|
-
# { :
|
49
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll
|
50
50
|
# #=> [[:foo, :bar, :b, 3, 0], [:foo, :bar, :a, 2, 1]]
|
51
51
|
#
|
52
|
-
# { :
|
52
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll(sort_by: :to_s)
|
53
53
|
# #=> [[:foo, :bar, :a, 1, 2], [:foo, :bar, :b, 0, 3]]
|
54
54
|
#
|
55
|
-
# { :
|
55
|
+
# { foo: { bar: { a: { x: 1, y: 2 }, b: { x: 0, y: 3 } } } }.unroll { |data| data[:x] = nil; data[:y] *= 2 }
|
56
56
|
# #=> [[:foo, :bar, :b, 6, nil], [:foo, :bar, :a, 4, nil]]
|
57
57
|
def unroll(*value_keys, &block)
|
58
58
|
args = value_keys.dup
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# #
|
4
4
|
# nuggets -- Extending Ruby #
|
5
5
|
# #
|
6
|
-
# Copyright (C) 2007-
|
6
|
+
# Copyright (C) 2007-2016 Jens Wille #
|
7
7
|
# #
|
8
8
|
# Authors: #
|
9
9
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -24,13 +24,11 @@
|
|
24
24
|
###############################################################################
|
25
25
|
#++
|
26
26
|
|
27
|
-
require 'nuggets/io/null_mixin'
|
28
|
-
|
29
27
|
module Nuggets
|
30
28
|
class IO
|
31
29
|
module RedirectMixin
|
32
30
|
|
33
|
-
def redirect(target =
|
31
|
+
def redirect(target = ::File::NULL, mode = 'w')
|
34
32
|
unless target.is_a?(self.class)
|
35
33
|
target = ::File.open(target, mode)
|
36
34
|
close_target = true
|
@@ -54,7 +54,7 @@ module Nuggets
|
|
54
54
|
# Log line patterns
|
55
55
|
ITEMS = [
|
56
56
|
[:processing, {
|
57
|
-
:
|
57
|
+
re: %r{
|
58
58
|
#{PREFIX_RE} # pid, host
|
59
59
|
Processing\s+
|
60
60
|
(\w+) # controller
|
@@ -72,23 +72,23 @@ module Nuggets
|
|
72
72
|
(\w+) # request_method
|
73
73
|
\]
|
74
74
|
}xo,
|
75
|
-
:
|
75
|
+
keys: [:controller, :action, :ip, :datetime, :request_method]
|
76
76
|
}],
|
77
77
|
[:session_id, {
|
78
|
-
:
|
78
|
+
re: %r{
|
79
79
|
#{PREFIX_RE} # pid, host
|
80
80
|
Session\sID:\s+
|
81
81
|
(\S+) # sid
|
82
82
|
}xo,
|
83
|
-
:
|
83
|
+
keys: [:sid]
|
84
84
|
}],
|
85
85
|
[:parameters, {
|
86
|
-
:
|
86
|
+
re: %r{
|
87
87
|
#{PREFIX_RE} # pid, host
|
88
88
|
Parameters:\s+
|
89
89
|
(\{.*\}) # params
|
90
90
|
}xo, #}
|
91
|
-
:
|
91
|
+
proc: lambda { |entry, md|
|
92
92
|
entry[:params_hash] = md[3].hash
|
93
93
|
entry[:params] = begin
|
94
94
|
eval("$SAFE = 3\n#{md[3].gsub(/#<.*?>/, '%q{\&}')}", nil, __FILE__, __LINE__) # !!!
|
@@ -98,7 +98,7 @@ module Nuggets
|
|
98
98
|
}
|
99
99
|
}],
|
100
100
|
[:client_info, {
|
101
|
-
:
|
101
|
+
re: %r{
|
102
102
|
#{PREFIX_RE} # pid, host
|
103
103
|
Client\sinfo:\s+
|
104
104
|
UA\s+=\s+
|
@@ -107,18 +107,18 @@ module Nuggets
|
|
107
107
|
LANG\s+=\s+
|
108
108
|
(.*) # accept_language
|
109
109
|
}xo,
|
110
|
-
:
|
110
|
+
keys: [:user_agent, :accept_language]
|
111
111
|
}],
|
112
112
|
[:referer, {
|
113
|
-
:
|
113
|
+
re: %r{
|
114
114
|
#{PREFIX_RE} # pid, host
|
115
115
|
Referer:\s+
|
116
116
|
(.*) # referer
|
117
117
|
}xo,
|
118
|
-
:
|
118
|
+
keys: [:referer]
|
119
119
|
}],
|
120
120
|
[:meta, {
|
121
|
-
:
|
121
|
+
re: %r{
|
122
122
|
#{PREFIX_RE} # pid, host
|
123
123
|
Meta:\s+
|
124
124
|
User\s+=\s+
|
@@ -127,20 +127,20 @@ module Nuggets
|
|
127
127
|
Institution\s+=\s+
|
128
128
|
(.*) # institution_id
|
129
129
|
}xo,
|
130
|
-
:
|
130
|
+
keys: [:user_id, :institution_id]
|
131
131
|
}],
|
132
132
|
[:stats, {
|
133
|
-
:
|
133
|
+
re: %r{
|
134
134
|
#{PREFIX_RE} # pid, host
|
135
135
|
Stats:\s+
|
136
136
|
(.*) # flags
|
137
137
|
}xo,
|
138
|
-
:
|
138
|
+
proc: lambda { |entry, md|
|
139
139
|
entry[:flags] = md[3].split(SEPARATOR_RE)
|
140
140
|
}
|
141
141
|
}],
|
142
142
|
[:oauth, {
|
143
|
-
:
|
143
|
+
re: %r{
|
144
144
|
#{PREFIX_RE} # pid, host
|
145
145
|
OAuth:\s+
|
146
146
|
Token\s+=\s+
|
@@ -152,10 +152,10 @@ module Nuggets
|
|
152
152
|
Client\s+=\s+
|
153
153
|
(.*) # client_id
|
154
154
|
}xo,
|
155
|
-
:
|
155
|
+
keys: [:token_type, :token, :user_id, :client_id]
|
156
156
|
}],
|
157
157
|
[:benchmark, {
|
158
|
-
:
|
158
|
+
re: %r{
|
159
159
|
#{PREFIX_RE} # pid, host
|
160
160
|
Completed\sin\s+
|
161
161
|
(\S+) # runtime
|
@@ -185,7 +185,7 @@ module Nuggets
|
|
185
185
|
(.*) # request_uri
|
186
186
|
\]
|
187
187
|
}xo,
|
188
|
-
:
|
188
|
+
keys: [:runtime, :rendering_runtime, :db_runtime, :status, :request_uri]
|
189
189
|
}]
|
190
190
|
]
|
191
191
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#--
|
2
|
+
###############################################################################
|
3
|
+
# #
|
4
|
+
# nuggets -- Extending Ruby #
|
5
|
+
# #
|
6
|
+
# Copyright (C) 2007-2016 Jens Wille #
|
7
|
+
# #
|
8
|
+
# Authors: #
|
9
|
+
# Jens Wille <jens.wille@gmail.com> #
|
10
|
+
# #
|
11
|
+
# nuggets is free software; you can redistribute it and/or modify it under #
|
12
|
+
# the terms of the GNU Affero General Public License as published by the Free #
|
13
|
+
# Software Foundation; either version 3 of the License, or (at your option) #
|
14
|
+
# any later version. #
|
15
|
+
# #
|
16
|
+
# nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
|
17
|
+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
|
18
|
+
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
|
19
|
+
# more details. #
|
20
|
+
# #
|
21
|
+
# You should have received a copy of the GNU Affero General Public License #
|
22
|
+
# along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
|
23
|
+
# #
|
24
|
+
###############################################################################
|
25
|
+
#++
|
26
|
+
|
27
|
+
module Nuggets
|
28
|
+
class String
|
29
|
+
module FormatMixin
|
30
|
+
|
31
|
+
PREFIX = '%'
|
32
|
+
|
33
|
+
FORMAT_RE = %r{#{PREFIX}(?:\{(.*?)\}|(.))}o
|
34
|
+
|
35
|
+
# call-seq:
|
36
|
+
# str.format(hash) => aString
|
37
|
+
# str.format { |directive| ... } => aString
|
38
|
+
#
|
39
|
+
# Replace format directives in _str_.
|
40
|
+
#
|
41
|
+
# If +hash+ given, uses value of directive key (String or Symbol)
|
42
|
+
# as replacement value. Raises KeyError if directive key not found.
|
43
|
+
#
|
44
|
+
# If block given, uses result of +directive+ yielded to block as
|
45
|
+
# replacement value. Raises ArgumentError if block returns no value.
|
46
|
+
def format(hash = nil)
|
47
|
+
replace = lambda { |&b| gsub(FORMAT_RE) {
|
48
|
+
(k = $1 || $2) == PREFIX ? k : b[k] } }
|
49
|
+
|
50
|
+
hash ? replace.() { |k| hash.fetch(k) {
|
51
|
+
hash.fetch(k.to_sym) { raise(KeyError, "key not found: #{k}") } } } :
|
52
|
+
block_given? ? replace.() { |k| yield k or
|
53
|
+
raise(ArgumentError, "malformed format string - #{$&}") } :
|
54
|
+
raise(ArgumentError, 'wrong number of arguments (given 0, expected 1)')
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# #
|
4
4
|
# nuggets -- Extending Ruby #
|
5
5
|
# #
|
6
|
-
# Copyright (C) 2007-
|
6
|
+
# Copyright (C) 2007-2016 Jens Wille #
|
7
7
|
# #
|
8
8
|
# Authors: #
|
9
9
|
# Jens Wille <jens.wille@gmail.com> #
|
@@ -24,18 +24,38 @@
|
|
24
24
|
###############################################################################
|
25
25
|
#++
|
26
26
|
|
27
|
-
require 'nuggets/ruby_mixin'
|
28
|
-
|
29
27
|
module Nuggets
|
30
|
-
class
|
31
|
-
module
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
28
|
+
class String
|
29
|
+
module HighlightMixin
|
30
|
+
|
31
|
+
# call-seq:
|
32
|
+
# str.highlight(needle[, prefix[, postfix]]) => new_string
|
33
|
+
#
|
34
|
+
# Highlight occurrences of +needle+ (String(s) and/or Regexp(s))
|
35
|
+
# in _str_ by surrounding them with +prefix+ and +postfix+.
|
36
|
+
def highlight(needle, prefix = '|', postfix = prefix)
|
37
|
+
offsets = []
|
38
|
+
|
39
|
+
Array(needle).each { |arg|
|
40
|
+
while index = index(arg, index || 0)
|
41
|
+
offsets << [index, index += ($& || arg).length]
|
42
|
+
end
|
43
|
+
}
|
44
|
+
|
45
|
+
flattened = [current = offsets.sort!.shift]
|
46
|
+
|
47
|
+
offsets.each { |offset|
|
48
|
+
i1, j1 = current
|
49
|
+
i2, j2 = offset
|
50
|
+
|
51
|
+
i2 > j1 ? flattened << current = offset :
|
52
|
+
j2 > j1 ? current[-1] = j2 : nil
|
53
|
+
}
|
54
|
+
|
55
|
+
dup.tap { |result| flattened.reverse_each { |i, j|
|
56
|
+
result.insert(j, postfix).insert(i, prefix)
|
57
|
+
} }
|
58
|
+
end
|
39
59
|
|
40
60
|
end
|
41
61
|
end
|
data/lib/nuggets/version.rb
CHANGED
@@ -12,12 +12,12 @@ describe_extended ENV, Nuggets::Env::SetMixin, true do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
example do
|
15
|
-
ENV.with(:
|
15
|
+
ENV.with(lang: 'C') { ENV['LANG'].should == 'C' }
|
16
16
|
ENV['LANG'].should == @original['LANG']
|
17
17
|
end
|
18
18
|
|
19
19
|
example do
|
20
|
-
ENV.set(:
|
20
|
+
ENV.set(lang: 'C').to_hash.should == @original
|
21
21
|
ENV.to_hash.should == { 'LANG' => 'C' }
|
22
22
|
end
|
23
23
|
|
@@ -54,7 +54,7 @@ describe_extended Hash, Nuggets::Hash::DeepFetchMixin do
|
|
54
54
|
|
55
55
|
context do
|
56
56
|
|
57
|
-
let(:hash) { { :
|
57
|
+
let(:hash) { { foo: { bar: { baz: 42 } } } }
|
58
58
|
|
59
59
|
example do
|
60
60
|
lambda { hash.deep_fetch('foo/bar/baz') }.should raise_error(KeyError)
|
@@ -3,58 +3,58 @@ require 'nuggets/hash/deep_merge'
|
|
3
3
|
describe_extended Hash, Nuggets::Hash::DeepMergeMixin do
|
4
4
|
|
5
5
|
example do
|
6
|
-
{ :
|
6
|
+
{ a: 1 }.deep_merge(a: 2).should == { a: 2 }
|
7
7
|
end
|
8
8
|
|
9
9
|
example do
|
10
|
-
{ :
|
10
|
+
{ a: 1 }.deep_merge(b: 2).should == { a: 1, b: 2 }
|
11
11
|
end
|
12
12
|
|
13
13
|
example do
|
14
|
-
{ :
|
14
|
+
{ a: { b: 1 } }.deep_merge(b: 2).should == { a: { b: 1 }, b: 2 }
|
15
15
|
end
|
16
16
|
|
17
17
|
example do
|
18
|
-
{ :
|
18
|
+
{ a: { b: 1 } }.deep_merge(a: { b: 2 }).should == { a: { b: 2 } }
|
19
19
|
end
|
20
20
|
|
21
21
|
example do
|
22
|
-
lambda { { :
|
22
|
+
lambda { { a: { b: { c: 1 } } }.deep_merge(a: { b: 2 }) }.should raise_error(TypeError)
|
23
23
|
end
|
24
24
|
|
25
25
|
example do
|
26
|
-
{ :
|
26
|
+
{ a: { b: { c: 1 } } }.deep_merge(a: { b: { c: 2 } }).should == { a: { b: { c: 2 } } }
|
27
27
|
end
|
28
28
|
|
29
29
|
example do
|
30
|
-
{ :
|
30
|
+
{ a: { b: { c: 1 } } }.deep_merge(a: { b: { d: 2 } }).should == { a: { b: { c: 1, d: 2 } } }
|
31
31
|
end
|
32
32
|
|
33
33
|
example do
|
34
|
-
{ :
|
34
|
+
{ a: { b: { c: 1 } } }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 } }
|
35
35
|
end
|
36
36
|
|
37
37
|
example do
|
38
|
-
{ :
|
38
|
+
{ a: { b: { c: 1 }, d: 2 } }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 } }
|
39
39
|
end
|
40
40
|
|
41
41
|
example do
|
42
|
-
{ :
|
42
|
+
{ a: { b: { c: 1 }, d: 2 }, e: 3 }.deep_merge(a: { b: { c: 2 }, d: 3 }).should == { a: { b: { c: 2 }, d: 3 }, e: 3 }
|
43
43
|
end
|
44
44
|
|
45
45
|
example do
|
46
|
-
{ :
|
46
|
+
{ a: { b: { c: 1 } }, d: { e: 3 } }.deep_merge(a: { b: { c: 2 } }, d: { e: 4 }).should == { a: { b: { c: 2 } }, d: { e: 4 } }
|
47
47
|
end
|
48
48
|
|
49
49
|
context do
|
50
50
|
|
51
51
|
before :each do
|
52
|
-
@sub_hash1 = { :
|
53
|
-
@sub_hash2 = { :
|
52
|
+
@sub_hash1 = { b: 1 }
|
53
|
+
@sub_hash2 = { b: 2 }
|
54
54
|
|
55
|
-
@hash1 = { :
|
56
|
-
@hash2 = { :
|
57
|
-
@hash3 = { :
|
55
|
+
@hash1 = { a: @sub_hash1 }
|
56
|
+
@hash2 = { a: @sub_hash2 }
|
57
|
+
@hash3 = { a: { b: 2} }
|
58
58
|
end
|
59
59
|
|
60
60
|
example do
|
@@ -18,12 +18,12 @@ describe_extended Hash, Nuggets::Hash::UnrollMixin do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
example do
|
21
|
-
hash = { :
|
21
|
+
hash = { a: { b: 1 } }
|
22
22
|
hash.unroll.should == [[:a, 1]]
|
23
23
|
end
|
24
24
|
|
25
25
|
example do
|
26
|
-
hash = { :
|
26
|
+
hash = { a: { b: 1, c: 2 } }
|
27
27
|
|
28
28
|
result = hash.unroll.first
|
29
29
|
result.size.should == 3
|
@@ -33,36 +33,24 @@ describe_extended Hash, Nuggets::Hash::UnrollMixin do
|
|
33
33
|
result.should include(2)
|
34
34
|
end
|
35
35
|
|
36
|
-
if RUBY_VERSION < '1.9'
|
37
|
-
example do
|
38
|
-
hash = { :a => { :b => 1, :c => 2 } }
|
39
|
-
lambda { hash.unroll(:sort => true) }.should raise_error(NoMethodError, /<=>/)
|
40
|
-
end
|
41
|
-
|
42
|
-
example do
|
43
|
-
hash = { :a => { :b => 1, :c => 2 } }
|
44
|
-
lambda { hash.unroll(:sort_by => lambda { |h| h.to_s }) }.should_not raise_error(NoMethodError)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
36
|
example do
|
49
37
|
hash = { 'a' => { 'b' => 1, 'c' => 2 } }
|
50
|
-
hash.unroll(:
|
38
|
+
hash.unroll(sort: true).should == [['a', 1, 2]]
|
51
39
|
end
|
52
40
|
|
53
41
|
example do
|
54
42
|
hash = { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } }
|
55
|
-
hash.unroll('b', 'c', :
|
43
|
+
hash.unroll('b', 'c', sort: true).should == [['a', 1, 2], ['d', 0, 3]]
|
56
44
|
end
|
57
45
|
|
58
46
|
example do
|
59
47
|
hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
|
60
|
-
hash.unroll('b', :
|
48
|
+
hash.unroll('b', sort_by: lambda { |h| h.to_s }).should == [['z', 'a', 1], ['z', 'd', 0]]
|
61
49
|
end
|
62
50
|
|
63
51
|
example do
|
64
52
|
hash = { 'z' => { 'a' => { 'b' => 1, 'c' => 2 }, 'd' => { 'b' => 0, 'c' => 3 } } }
|
65
|
-
hash.unroll(:
|
53
|
+
hash.unroll(sort: true) { |h| h['b'] = nil; h['c'] *= 2 }.should == [['z', 'a', nil, 4], ['z', 'd', nil, 6]]
|
66
54
|
end
|
67
55
|
|
68
56
|
end
|
@@ -23,11 +23,11 @@ describe_extended Object, Nuggets::Object::BlankMixin do
|
|
23
23
|
example { o.should be_vain }
|
24
24
|
}
|
25
25
|
|
26
|
-
[['', [], [nil], {}], { :
|
26
|
+
[['', [], [nil], {}], { x: nil, y: [], z: { zz: nil } }].each { |o|
|
27
27
|
example { o.should_not be_void }
|
28
28
|
}
|
29
29
|
|
30
|
-
[['', [], [nil], {}], { :
|
30
|
+
[['', [], [nil], {}], { x: nil, y: [], z: { zz: nil } }].each { |o|
|
31
31
|
example { o.should be_vain }
|
32
32
|
}
|
33
33
|
|
@@ -2,12 +2,13 @@ require 'nuggets/object/singleton_class'
|
|
2
2
|
|
3
3
|
describe_extended Object, Nuggets::Object::SingletonClassMixin do
|
4
4
|
|
5
|
-
objects = [Class, Object.new, Class.new, 's', [1, 2], { :
|
5
|
+
objects = [Class, Object.new, Class.new, 's', [1, 2], { a: 'b' }]
|
6
6
|
objects.unshift(Object) unless %w[jruby rbx].include?(RUBY_ENGINE)
|
7
7
|
|
8
8
|
objects.each { |o|
|
9
9
|
example { o.should_not be_a_singleton_class }
|
10
|
-
example
|
10
|
+
example(nil, if: o != Object || RUBY_VERSION < '2.3') {
|
11
|
+
lambda { o.singleton_object }.should raise_error(TypeError) }
|
11
12
|
|
12
13
|
s = o.singleton_class
|
13
14
|
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'nuggets/string/format'
|
2
|
+
|
3
|
+
describe_extended String, Nuggets::String::FormatMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
expect{'a%bc'.format}.to raise_error(ArgumentError, 'wrong number of arguments (given 0, expected 1)')
|
7
|
+
end
|
8
|
+
|
9
|
+
example do
|
10
|
+
expect('a%bc'.format('b' => 'foo')).to eq('afooc')
|
11
|
+
end
|
12
|
+
|
13
|
+
example do
|
14
|
+
expect('a%bc'.format(b: 'foo')).to eq('afooc')
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
expect('a%bc'.format(b: :foo)).to eq('afooc')
|
19
|
+
end
|
20
|
+
|
21
|
+
example do
|
22
|
+
expect('a%bc'.format(b: 42)).to eq('a42c')
|
23
|
+
end
|
24
|
+
|
25
|
+
example do
|
26
|
+
expect('a%bc'.format(b: '')).to eq('ac')
|
27
|
+
end
|
28
|
+
|
29
|
+
example do
|
30
|
+
expect('a%bc'.format(b: nil)).to eq('ac')
|
31
|
+
end
|
32
|
+
|
33
|
+
example do
|
34
|
+
expect('a%{foo}c'.format('foo' => 'bar')).to eq('abarc')
|
35
|
+
end
|
36
|
+
|
37
|
+
example do
|
38
|
+
expect('a%{foo}c'.format(foo: 'bar')).to eq('abarc')
|
39
|
+
end
|
40
|
+
|
41
|
+
example do
|
42
|
+
expect('a%{foo}c'.format(foo: :bar)).to eq('abarc')
|
43
|
+
end
|
44
|
+
|
45
|
+
example do
|
46
|
+
expect(''.format({})).to eq('')
|
47
|
+
end
|
48
|
+
|
49
|
+
example do
|
50
|
+
expect('%'.format({})).to eq('%')
|
51
|
+
end
|
52
|
+
|
53
|
+
example do
|
54
|
+
expect('%%'.format({})).to eq('%')
|
55
|
+
end
|
56
|
+
|
57
|
+
example do
|
58
|
+
expect{'%a'.format({})}.to raise_error(KeyError, 'key not found: a')
|
59
|
+
end
|
60
|
+
|
61
|
+
example do
|
62
|
+
expect{'%a'.format('b' => 'foo')}.to raise_error(KeyError, 'key not found: a')
|
63
|
+
end
|
64
|
+
|
65
|
+
example do
|
66
|
+
expect{'%a'.format(Hash.new(42))}.to raise_error(KeyError, 'key not found: a')
|
67
|
+
end
|
68
|
+
|
69
|
+
example do
|
70
|
+
expect('a%bc'.format(&:upcase)).to eq('aBc')
|
71
|
+
end
|
72
|
+
|
73
|
+
example do
|
74
|
+
expect('a%bc'.format{''}).to eq('ac')
|
75
|
+
end
|
76
|
+
|
77
|
+
example do
|
78
|
+
expect{'a%bc'.format{}}.to raise_error(ArgumentError, 'malformed format string - %b')
|
79
|
+
end
|
80
|
+
|
81
|
+
example do
|
82
|
+
expect('a%bc%d%{foo}%%{baz}'.format(b: 42, d: 'D', 'foo' => :bar)).to eq('a42cDbar%{baz}')
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'nuggets/string/highlight'
|
2
|
+
|
3
|
+
describe_extended String, Nuggets::String::HighlightMixin do
|
4
|
+
|
5
|
+
example do
|
6
|
+
s = 'lingo go do the go go'; t = s.dup
|
7
|
+
expect(s.highlight('lingo')).to eq('|lingo| go do the go go')
|
8
|
+
expect(s).to eq(t)
|
9
|
+
|
10
|
+
expect(s.highlight(/lingo/)).to eq('|lingo| go do the go go')
|
11
|
+
expect(s.highlight(/l.*ngo?/)).to eq('|lingo| go do the go go')
|
12
|
+
|
13
|
+
q = ['lingo', 'go', 'go go']
|
14
|
+
expect(s.highlight(q)).to eq('|lingo go| do the |go go|')
|
15
|
+
end
|
16
|
+
|
17
|
+
example do
|
18
|
+
s = 'lingö go do the go go'
|
19
|
+
q = ['lingö', 'go', 'go go']
|
20
|
+
expect(s.highlight(q)).to eq('|lingö| |go| do the |go go|')
|
21
|
+
end
|
22
|
+
|
23
|
+
example do
|
24
|
+
s = 'lingo go do the go go'
|
25
|
+
q = ['lingo', 'go', 'go go']
|
26
|
+
expect(s.highlight(q, '^')).to eq('^lingo go^ do the ^go go^')
|
27
|
+
expect(s.highlight(q, '<', '>')).to eq('<lingo go> do the <go go>')
|
28
|
+
expect(s.highlight(q, '<i>', '</i>')).to eq('<i>lingo go</i> do the <i>go go</i>')
|
29
|
+
end
|
30
|
+
|
31
|
+
example do
|
32
|
+
s = 'The fox went out on a chilly night'
|
33
|
+
q = [/[eo]n/]
|
34
|
+
expect(s.highlight(q)).to eq('The fox w|en|t out |on| a chilly night')
|
35
|
+
q << 'went'
|
36
|
+
expect(s.highlight(q)).to eq('The fox |went| out |on| a chilly night')
|
37
|
+
q << /ou.*a/
|
38
|
+
expect(s.highlight(q)).to eq('The fox |went| |out on a| chilly night')
|
39
|
+
q << 'went out'
|
40
|
+
expect(s.highlight(q)).to eq('The fox |went out on a| chilly night')
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nuggets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Wille
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|
@@ -205,8 +205,6 @@ files:
|
|
205
205
|
- lib/nuggets/io/interact.rb
|
206
206
|
- lib/nuggets/io/interact_mixin.rb
|
207
207
|
- lib/nuggets/io/modes.rb
|
208
|
-
- lib/nuggets/io/null.rb
|
209
|
-
- lib/nuggets/io/null_mixin.rb
|
210
208
|
- lib/nuggets/io/redirect.rb
|
211
209
|
- lib/nuggets/io/redirect_mixin.rb
|
212
210
|
- lib/nuggets/lazy_attr.rb
|
@@ -258,6 +256,10 @@ files:
|
|
258
256
|
- lib/nuggets/string/case.rb
|
259
257
|
- lib/nuggets/string/evaluate.rb
|
260
258
|
- lib/nuggets/string/evaluate_mixin.rb
|
259
|
+
- lib/nuggets/string/format.rb
|
260
|
+
- lib/nuggets/string/format_mixin.rb
|
261
|
+
- lib/nuggets/string/highlight.rb
|
262
|
+
- lib/nuggets/string/highlight_mixin.rb
|
261
263
|
- lib/nuggets/string/msub.rb
|
262
264
|
- lib/nuggets/string/nsub.rb
|
263
265
|
- lib/nuggets/string/sub_with_md.rb
|
@@ -339,6 +341,8 @@ files:
|
|
339
341
|
- spec/nuggets/string/capitalize_first_spec.rb
|
340
342
|
- spec/nuggets/string/case_spec.rb
|
341
343
|
- spec/nuggets/string/evaluate_spec.rb
|
344
|
+
- spec/nuggets/string/format_spec.rb
|
345
|
+
- spec/nuggets/string/highlight_spec.rb
|
342
346
|
- spec/nuggets/string/msub_spec.rb
|
343
347
|
- spec/nuggets/string/nsub_spec.rb
|
344
348
|
- spec/nuggets/string/sub_with_md_spec.rb
|
@@ -354,14 +358,15 @@ licenses:
|
|
354
358
|
metadata: {}
|
355
359
|
post_install_message: |2+
|
356
360
|
|
357
|
-
nuggets-1.
|
361
|
+
nuggets-1.5.0 [2016-04-19]:
|
358
362
|
|
359
|
-
* Added
|
360
|
-
* Added
|
363
|
+
* Added String#format.
|
364
|
+
* Added String#highlight.
|
365
|
+
* Dropped IO::NULL, available as File::NULL since Ruby 1.9.3.
|
361
366
|
|
362
367
|
rdoc_options:
|
363
368
|
- "--title"
|
364
|
-
- nuggets Application documentation (v1.
|
369
|
+
- nuggets Application documentation (v1.5.0)
|
365
370
|
- "--charset"
|
366
371
|
- UTF-8
|
367
372
|
- "--line-numbers"
|
@@ -382,7 +387,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
382
387
|
version: '0'
|
383
388
|
requirements: []
|
384
389
|
rubyforge_project:
|
385
|
-
rubygems_version: 2.
|
390
|
+
rubygems_version: 2.6.3
|
386
391
|
signing_key:
|
387
392
|
specification_version: 4
|
388
393
|
summary: Extending Ruby.
|