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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fae72114f1a7396cc0e541f34ab73c3e005af43
4
- data.tar.gz: edf0916f009bd56ec5a8225aaf086d8de5326b93
3
+ metadata.gz: 9a512d0f4fbdc135ac1abd41c6ecd4178c06acf2
4
+ data.tar.gz: f7bee93b9ea82ca2bd2411ac5077132a239601ae
5
5
  SHA512:
6
- metadata.gz: ae0b61376227d10149e3f7fef6289344748c06293202ed03028672b24248ce925bfeee6186f1fb0b5747b77888bd2b29c2ee9aaca8956789bdb350768349ce11
7
- data.tar.gz: 03e82b8abc0c119748b305f448f9bb2ecda76419afa2696d28b0d638325c028773396c1b5b411d015078fe87bc003bc0d96f95d06a9a56f307b08a446862ed6b
6
+ metadata.gz: 1513eabf269f6d398661d52bf65c48697bab3fc518fd8d649e8fc92a9f6d2e7c8d41f5aa46cd8aec6f684c5cd018597b4b730b56f0e8c4ce361f3a27107cc87f
7
+ data.tar.gz: c0882a43a7725eb851b235e868861d3766e850c6cfb7e50b2ffe1752454960bfb13d1e67fa9d3ef776eb63c87a3a2815007e57e98f79a011dc1bff0f5d4c1950
@@ -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
- @tree.map do |key, value|
18
+ tree.map do |key, value|
20
19
  case
21
- when value.kind_of?(Hash) && value.none?
22
- label(namespace, key) + KEY_VALUE_DELIMITER + element(nil)
23
- when value.respond_to?(:to_hash)
24
- Scrawl.new(value).inspect(key)
25
- else
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
@@ -1,3 +1,3 @@
1
1
  class Scrawl
2
- VERSION = "2.1.0"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -1,189 +1,267 @@
1
1
  require "spec_helper"
2
2
 
3
3
  RSpec.describe Scrawl do
4
- let(:input) { { a: 1 } }
5
- let(:trees) { [input] }
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
- describe ".new" do
10
- context "with single" do
11
- shared_examples "setup single" do
12
- it "sets internal tree to that hash" do
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
- context "hash object" do
18
- include_examples "setup single"
19
- end
12
+ let(:output) do
13
+ 'a="a"'
14
+ end
15
+ end
20
16
 
21
- context "scrawl object" do
22
- let(:tree) { Scrawl.new(*trees) }
17
+ shared_context "for multiple input" do
18
+ let(:input) do
19
+ { "a" => "a", "b" => "b" }
20
+ end
23
21
 
24
- include_examples "setup single"
25
- end
22
+ let(:output) do
23
+ 'a="a" b="b"'
26
24
  end
25
+ end
27
26
 
28
- context "with multiple" do
29
- let(:merged_input) { { a: 1, b: 2 } }
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
- shared_examples "setup all" do
32
- it "merges all arguments into a single tree" do
33
- expect(internal_tree).to eq(merged_input)
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
- context "hashes objects" do
38
- let(:trees) { [{ a: 1 }, { b: 2 }] }
52
+ it "combines the namespace keys" do
53
+ expect(function).to eq(output)
54
+ end
39
55
  end
40
56
 
41
- context "scrawl objects" do
42
- let(:trees) { [Scrawl.new(a: 1), Scrawl.new(b: 2)] }
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
- describe "#merge" do
48
- let(:merge) { scrawl.merge(compared) }
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
- shared_examples "merges" do
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
- context "with hash" do
57
- let(:compared) { { b: 2 } }
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
- include_examples "merges"
60
- end
87
+ include_examples "for output"
88
+ end
61
89
 
62
- context "with scrawl object" do
63
- let(:compared) { Scrawl.new(b: 2) }
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
- include_examples "merges"
66
- end
67
- end
98
+ include_examples "for output"
99
+ end
68
100
 
69
- describe "#inspect" do
70
- let(:stream) { "a=1" }
71
- let(:namespace) { nil }
72
- let(:inspect) { scrawl.inspect(namespace) }
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
- context "with a single pair" do
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 "and text value" do
80
- let(:input) { { a: "example" } }
81
-
82
- it "includes the escaped text" do
83
- expect(inspect).to eq("a=\"example\"")
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
- context "and a proc value" do
88
- let(:input) { { a: -> { "example#{1 + 1}" } } }
120
+ include_examples "for output"
121
+ end
89
122
 
90
- it "evaluate the proc" do
91
- expect(inspect).to eq("a=\"example2\"")
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
- context "with a random portion" do
95
- let(:input) { { a: -> { "example#{rand(1..100000)}" } } }
131
+ include_examples "for output"
132
+ end
96
133
 
97
- it "never stores the value" do
98
- first = scrawl.inspect
99
- second = scrawl.inspect
100
- expect(first).to_not eq(second)
101
- end
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 "and a number value" do
106
- it "returns just the number" do
107
- expect(inspect).to eq("a=1")
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
- context "and a symbol value" do
112
- let(:input) { { a: :example } }
153
+ include_examples "for output"
154
+ end
155
+ end
113
156
 
114
- it "returns the escaped string equivelent" do
115
- expect(inspect).to eq("a=\"example\"")
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
- context "and a regexp value" do
120
- let(:input) { { a: /foo(.+)/ } }
166
+ include_examples "for output"
167
+ end
121
168
 
122
- it "returns the escaped regexp" do
123
- expect(inspect).to eq("a=/foo(.+)/")
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 "and a hash" do
128
- let(:input) { { a: Hash.new } }
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
- it "returns as if nil" do
131
- expect(inspect).to eq("a=nil")
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
- context "with multiple pairs" do
137
- let(:input) { { a: 1, b: 2 } }
138
- let(:stream) { "a=1 b=2" }
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
- it "delimits key value pairs with space" do
141
- expect(inspect).to eq(stream)
210
+ include_examples "for output"
142
211
  end
143
- end
144
212
 
145
- context "with a given namespace" do
146
- let(:namespace) { "b" }
147
- let(:stream) { "b.a=1" }
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
- it "prefixes the namespace to the key" do
150
- expect(inspect).to eq(stream)
221
+ include_examples "for output"
151
222
  end
152
- end
153
223
 
154
- context "with nested hash tree" do
155
- let(:input) { { a: { b: 1, c: 2 } } }
156
- let(:stream) { "a.b=1 a.c=2" }
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
- it "namespaces the nested pairs" do
159
- expect(inspect).to eq(stream)
232
+ include_examples "for output"
160
233
  end
161
- end
162
- end
163
234
 
164
- describe "#tree" do
165
- let(:tree) { scrawl.tree }
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
- it "returns the internal tree" do
168
- expect(tree).to eq(internal_tree)
169
- end
170
- end
243
+ include_examples "for output"
244
+ end
171
245
 
172
- describe "#to_h" do
173
- let(:to_h) { scrawl.to_h }
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
- it "returns the internal tree" do
176
- expect(to_h).to eq(internal_tree)
254
+ include_examples "for output"
255
+ end
177
256
  end
178
257
  end
179
258
 
180
- describe "#to_hash" do
181
- let(:to_hash) { scrawl.to_hash }
182
-
183
- it "returns the internal tree" do
184
- expect(to_hash).to eq(internal_tree)
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: 2.1.0
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-08-31 00:00:00.000000000 Z
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.5
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.5
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.0
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.0
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.2
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.2
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.1
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.1
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.0
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.0
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.7
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.7
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.0
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.0
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.8
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.8
124
+ version: '0.15'
125
125
  description: Turn hashes into simple log-ready output
126
126
  email:
127
127
  - me@kurtisrainboltgreene.name