scrawl 2.1.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/scrawl.rb +26 -12
- data/lib/scrawl/version.rb +1 -1
- data/spec/lib/scrawl_spec.rb +200 -122
- metadata +34 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a512d0f4fbdc135ac1abd41c6ecd4178c06acf2
|
4
|
+
data.tar.gz: f7bee93b9ea82ca2bd2411ac5077132a239601ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1513eabf269f6d398661d52bf65c48697bab3fc518fd8d649e8fc92a9f6d2e7c8d41f5aa46cd8aec6f684c5cd018597b4b730b56f0e8c4ce361f3a27107cc87f
|
7
|
+
data.tar.gz: c0882a43a7725eb851b235e868861d3766e850c6cfb7e50b2ffe1752454960bfb13d1e67fa9d3ef776eb63c87a3a2815007e57e98f79a011dc1bff0f5d4c1950
|
data/lib/scrawl.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
class Scrawl
|
2
|
+
include Enumerable
|
3
|
+
|
2
4
|
KEY_VALUE_DELIMITER = "="
|
3
5
|
PAIR_DELIMITER = " "
|
4
6
|
NAMESPACE_DELIMITER = "."
|
5
|
-
|
6
|
-
require_relative "scrawl/version"
|
7
|
-
|
8
|
-
attr_reader :tree
|
7
|
+
DEFAULT_SPLAT_CHARACTER="*"
|
9
8
|
|
10
9
|
def initialize(*trees)
|
11
10
|
@tree = trees.inject({}) { |global, tree| global.merge(tree) }
|
@@ -16,22 +15,25 @@ class Scrawl
|
|
16
15
|
end
|
17
16
|
|
18
17
|
def inspect(namespace = nil)
|
19
|
-
|
18
|
+
tree.map do |key, value|
|
20
19
|
case
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
Scrawl.new(value).inspect(key)
|
25
|
-
|
26
|
-
label(namespace, key) + KEY_VALUE_DELIMITER + element(value)
|
20
|
+
when value.nil? then nil
|
21
|
+
when value.respond_to?(:none?) && value.none? then nil
|
22
|
+
when value.respond_to?(:push) then itemize(namespace, key, value)
|
23
|
+
when value.respond_to?(:merge) then Scrawl.new(value).inspect(key)
|
24
|
+
else label(namespace, key) + KEY_VALUE_DELIMITER + element(value)
|
27
25
|
end
|
28
|
-
end.join(PAIR_DELIMITER)
|
26
|
+
end.flatten.compact.join(PAIR_DELIMITER)
|
29
27
|
end
|
30
28
|
|
31
29
|
def to_s(namespace = nil)
|
32
30
|
inspect(namespace)
|
33
31
|
end
|
34
32
|
|
33
|
+
def each(&block)
|
34
|
+
tree.each(&block)
|
35
|
+
end
|
36
|
+
|
35
37
|
def to_hash
|
36
38
|
tree.to_hash
|
37
39
|
end
|
@@ -51,4 +53,16 @@ class Scrawl
|
|
51
53
|
else value.inspect
|
52
54
|
end
|
53
55
|
end
|
56
|
+
|
57
|
+
private def tree
|
58
|
+
@tree
|
59
|
+
end
|
60
|
+
|
61
|
+
private def itemize(namespace, key, list)
|
62
|
+
list.map do |item|
|
63
|
+
label(namespace, label(key, DEFAULT_SPLAT_CHARACTER)) + KEY_VALUE_DELIMITER + element(item)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
require_relative "scrawl/version"
|
54
68
|
end
|
data/lib/scrawl/version.rb
CHANGED
data/spec/lib/scrawl_spec.rb
CHANGED
@@ -1,189 +1,267 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
RSpec.describe Scrawl do
|
4
|
-
let(:
|
5
|
-
let(:
|
6
|
-
let(:scrawl) { described_class.new(*trees) }
|
7
|
-
let(:internal_tree) { scrawl.instance_variable_get(:@tree) }
|
4
|
+
let(:inputs) { [input] }
|
5
|
+
let(:scrawl) { described_class.new(*inputs) }
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
expect(internal_tree).to eq(input)
|
14
|
-
end
|
15
|
-
end
|
7
|
+
shared_context "for single input" do
|
8
|
+
let(:input) do
|
9
|
+
{ "a" => "a" }
|
10
|
+
end
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
let(:output) do
|
13
|
+
'a="a"'
|
14
|
+
end
|
15
|
+
end
|
20
16
|
|
21
|
-
|
22
|
-
|
17
|
+
shared_context "for multiple input" do
|
18
|
+
let(:input) do
|
19
|
+
{ "a" => "a", "b" => "b" }
|
20
|
+
end
|
23
21
|
|
24
|
-
|
25
|
-
|
22
|
+
let(:output) do
|
23
|
+
'a="a" b="b"'
|
26
24
|
end
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
27
|
+
shared_examples "for output" do
|
28
|
+
it "joins the key value together with an =" do
|
29
|
+
expect(function).to eq(output)
|
30
|
+
end
|
31
|
+
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
describe "#inspect" do
|
34
|
+
let(:namespace) do
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
let(:function) do
|
38
|
+
scrawl.inspect(namespace)
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with single input" do
|
42
|
+
context "with a namespace" do
|
43
|
+
include_context "for single input"
|
44
|
+
|
45
|
+
let(:namespace) do
|
46
|
+
"a"
|
47
|
+
end
|
48
|
+
let(:output) do
|
49
|
+
'a.a="a"'
|
34
50
|
end
|
35
|
-
end
|
36
51
|
|
37
|
-
|
38
|
-
|
52
|
+
it "combines the namespace keys" do
|
53
|
+
expect(function).to eq(output)
|
54
|
+
end
|
39
55
|
end
|
40
56
|
|
41
|
-
context "
|
42
|
-
let(:
|
57
|
+
context "that is a string" do
|
58
|
+
let(:input) do
|
59
|
+
{ "a" => "a" }
|
60
|
+
end
|
61
|
+
let(:output) do
|
62
|
+
'a="a"'
|
63
|
+
end
|
64
|
+
|
65
|
+
include_examples "for output"
|
43
66
|
end
|
44
|
-
end
|
45
|
-
end
|
46
67
|
|
47
|
-
|
48
|
-
|
68
|
+
context "that is a nil" do
|
69
|
+
let(:input) do
|
70
|
+
{ "a" => nil }
|
71
|
+
end
|
72
|
+
let(:output) do
|
73
|
+
''
|
74
|
+
end
|
49
75
|
|
50
|
-
|
51
|
-
it "combines with the internal tree" do
|
52
|
-
expect(merge).to eq(a: 1, b: 2)
|
76
|
+
include_examples "for output"
|
53
77
|
end
|
54
|
-
end
|
55
78
|
|
56
|
-
|
57
|
-
|
79
|
+
context "that is a numeric" do
|
80
|
+
let(:input) do
|
81
|
+
{ "a" => 1 }
|
82
|
+
end
|
83
|
+
let(:output) do
|
84
|
+
'a=1'
|
85
|
+
end
|
58
86
|
|
59
|
-
|
60
|
-
|
87
|
+
include_examples "for output"
|
88
|
+
end
|
61
89
|
|
62
|
-
|
63
|
-
|
90
|
+
context "that is a lambda" do
|
91
|
+
let(:input) do
|
92
|
+
{ "a" => -> { "a" + "a" } }
|
93
|
+
end
|
94
|
+
let(:output) do
|
95
|
+
'a="aa"'
|
96
|
+
end
|
64
97
|
|
65
|
-
|
66
|
-
|
67
|
-
end
|
98
|
+
include_examples "for output"
|
99
|
+
end
|
68
100
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
101
|
+
context "that is an empty hash" do
|
102
|
+
let(:input) do
|
103
|
+
{ "a" => {} }
|
104
|
+
end
|
105
|
+
let(:output) do
|
106
|
+
''
|
107
|
+
end
|
73
108
|
|
74
|
-
|
75
|
-
it "joins the key value together with an =" do
|
76
|
-
expect(inspect).to eq(stream)
|
109
|
+
include_examples "for output"
|
77
110
|
end
|
78
111
|
|
79
|
-
context "
|
80
|
-
let(:input)
|
81
|
-
|
82
|
-
|
83
|
-
|
112
|
+
context "that is a hash" do
|
113
|
+
let(:input) do
|
114
|
+
{ "a" => { "a" => "a" } }
|
115
|
+
end
|
116
|
+
let(:output) do
|
117
|
+
'a.a="a"'
|
84
118
|
end
|
85
|
-
end
|
86
119
|
|
87
|
-
|
88
|
-
|
120
|
+
include_examples "for output"
|
121
|
+
end
|
89
122
|
|
90
|
-
|
91
|
-
|
123
|
+
context "that is a empty array" do
|
124
|
+
let(:input) do
|
125
|
+
{ "a" => [] }
|
126
|
+
end
|
127
|
+
let(:output) do
|
128
|
+
''
|
92
129
|
end
|
93
130
|
|
94
|
-
|
95
|
-
|
131
|
+
include_examples "for output"
|
132
|
+
end
|
96
133
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
134
|
+
context "that is a array" do
|
135
|
+
let(:input) do
|
136
|
+
{ "a" => ["b", "c"] }
|
137
|
+
end
|
138
|
+
let(:output) do
|
139
|
+
'a.*="b" a.*="c"'
|
102
140
|
end
|
141
|
+
|
142
|
+
include_examples "for output"
|
103
143
|
end
|
104
144
|
|
105
|
-
context "
|
106
|
-
|
107
|
-
|
145
|
+
context "that is a scrawl" do
|
146
|
+
let(:input) do
|
147
|
+
{ "a" => Scrawl.new({ "a" => "a" }) }
|
148
|
+
end
|
149
|
+
let(:output) do
|
150
|
+
'a.a="a"'
|
108
151
|
end
|
109
|
-
end
|
110
152
|
|
111
|
-
|
112
|
-
|
153
|
+
include_examples "for output"
|
154
|
+
end
|
155
|
+
end
|
113
156
|
|
114
|
-
|
115
|
-
|
157
|
+
context "with multiple input" do
|
158
|
+
context "that are strings" do
|
159
|
+
let(:input) do
|
160
|
+
{ "a" => "a", "b" => "b" }
|
161
|
+
end
|
162
|
+
let(:output) do
|
163
|
+
'a="a" b="b"'
|
116
164
|
end
|
117
|
-
end
|
118
165
|
|
119
|
-
|
120
|
-
|
166
|
+
include_examples "for output"
|
167
|
+
end
|
121
168
|
|
122
|
-
|
123
|
-
|
169
|
+
context "that are nils" do
|
170
|
+
let(:input) do
|
171
|
+
{ "a" => nil, "b" => nil }
|
172
|
+
end
|
173
|
+
let(:output) do
|
174
|
+
''
|
124
175
|
end
|
176
|
+
|
177
|
+
include_examples "for output"
|
125
178
|
end
|
126
179
|
|
127
|
-
context "
|
128
|
-
let(:input)
|
180
|
+
context "that are numerics" do
|
181
|
+
let(:input) do
|
182
|
+
{ "a" => 1, "b" => 2 }
|
183
|
+
end
|
184
|
+
let(:output) do
|
185
|
+
'a=1 b=2'
|
186
|
+
end
|
187
|
+
|
188
|
+
include_examples "for output"
|
189
|
+
end
|
129
190
|
|
130
|
-
|
131
|
-
|
191
|
+
context "that are lambdas" do
|
192
|
+
let(:input) do
|
193
|
+
{ "a" => -> { "a" + "a" }, "b" => -> { "b" + "b" } }
|
132
194
|
end
|
195
|
+
let(:output) do
|
196
|
+
'a="aa" b="bb"'
|
197
|
+
end
|
198
|
+
|
199
|
+
include_examples "for output"
|
133
200
|
end
|
134
|
-
end
|
135
201
|
|
136
|
-
|
137
|
-
|
138
|
-
|
202
|
+
context "that are empty hashs" do
|
203
|
+
let(:input) do
|
204
|
+
{ "a" => {}, "b" => {} }
|
205
|
+
end
|
206
|
+
let(:output) do
|
207
|
+
''
|
208
|
+
end
|
139
209
|
|
140
|
-
|
141
|
-
expect(inspect).to eq(stream)
|
210
|
+
include_examples "for output"
|
142
211
|
end
|
143
|
-
end
|
144
212
|
|
145
|
-
|
146
|
-
|
147
|
-
|
213
|
+
context "that are hashs" do
|
214
|
+
let(:input) do
|
215
|
+
{ "a" => { "a" => "a" }, "b" => { "b" => "b" } }
|
216
|
+
end
|
217
|
+
let(:output) do
|
218
|
+
'a.a="a" b.b="b"'
|
219
|
+
end
|
148
220
|
|
149
|
-
|
150
|
-
expect(inspect).to eq(stream)
|
221
|
+
include_examples "for output"
|
151
222
|
end
|
152
|
-
end
|
153
223
|
|
154
|
-
|
155
|
-
|
156
|
-
|
224
|
+
context "that are empty arrays" do
|
225
|
+
let(:input) do
|
226
|
+
{ "a" => [], "b" => [] }
|
227
|
+
end
|
228
|
+
let(:output) do
|
229
|
+
''
|
230
|
+
end
|
157
231
|
|
158
|
-
|
159
|
-
expect(inspect).to eq(stream)
|
232
|
+
include_examples "for output"
|
160
233
|
end
|
161
|
-
end
|
162
|
-
end
|
163
234
|
|
164
|
-
|
165
|
-
|
235
|
+
context "that are arrays" do
|
236
|
+
let(:input) do
|
237
|
+
{ "a" => ["b", "c"], "b" => ["e", "f"] }
|
238
|
+
end
|
239
|
+
let(:output) do
|
240
|
+
'a.*="b" a.*="c" b.*="e" b.*="f"'
|
241
|
+
end
|
166
242
|
|
167
|
-
|
168
|
-
|
169
|
-
end
|
170
|
-
end
|
243
|
+
include_examples "for output"
|
244
|
+
end
|
171
245
|
|
172
|
-
|
173
|
-
|
246
|
+
context "that are scrawls" do
|
247
|
+
let(:input) do
|
248
|
+
{ "a" => Scrawl.new({ "a" => "a" }), "b" => Scrawl.new({ "b" => "b" }) }
|
249
|
+
end
|
250
|
+
let(:output) do
|
251
|
+
'a.a="a" b.b="b"'
|
252
|
+
end
|
174
253
|
|
175
|
-
|
176
|
-
|
254
|
+
include_examples "for output"
|
255
|
+
end
|
177
256
|
end
|
178
257
|
end
|
179
258
|
|
180
|
-
describe "#
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
259
|
+
describe "#to_s" do
|
260
|
+
include_context "for single input"
|
261
|
+
it "uses inspect with no namespace" do
|
262
|
+
expect(scrawl).to receive(:inspect).with(nil)
|
263
|
+
scrawl.to_s
|
185
264
|
end
|
186
265
|
end
|
187
266
|
|
188
|
-
describe "#=="
|
189
267
|
end
|
metadata
CHANGED
@@ -1,127 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scrawl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kurtis Rainbolt-Greene
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.10
|
19
|
+
version: '1.10'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.10
|
26
|
+
version: '1.10'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 3.3
|
33
|
+
version: '3.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 3.3
|
40
|
+
version: '3.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: 10.4
|
47
|
+
version: '10.4'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: 10.4
|
54
|
+
version: '10.4'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.10
|
61
|
+
version: '0.10'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.10
|
68
|
+
version: '0.10'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: pry-doc
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.8
|
75
|
+
version: '0.8'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.8
|
82
|
+
version: '0.8'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: codeclimate-test-reporter
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0.4
|
89
|
+
version: '0.4'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0.4
|
96
|
+
version: '0.4'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: benchmark-ips
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 2.3
|
103
|
+
version: '2.3'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 2.3
|
110
|
+
version: '2.3'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: ruby-prof
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: 0.15
|
117
|
+
version: '0.15'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: 0.15
|
124
|
+
version: '0.15'
|
125
125
|
description: Turn hashes into simple log-ready output
|
126
126
|
email:
|
127
127
|
- me@kurtisrainboltgreene.name
|