yaml-write-stream 1.0.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/History.txt +8 -0
- data/lib/yaml-write-stream/stateful.rb +24 -9
- data/lib/yaml-write-stream/version.rb +1 -1
- data/lib/yaml-write-stream/yielding.rb +29 -13
- data/spec/spec_helper.rb +1 -1
- data/spec/stateful_spec.rb +11 -1
- data/spec/yielding_spec.rb +25 -0
- 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: ae8a65b60178d130ae20ab1ed7b69b7da808877b
|
4
|
+
data.tar.gz: 5bdaf04f74ef81c54d75f61e453a65b09059284f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a56250ac350ab119588f4a2b86560742ee9df190d0d42601d401f38621c9f9bf3bd5a3fbffdc05199e0d0d3e7e3b0a90ef8c43da6e37f8c001414daeb2e9a19
|
7
|
+
data.tar.gz: 439293700442a20ca04e47227e352702eb23ae3ba91edd0788f75e260b1cdf7deb459b8e0daa9adf28ab7d13ba1a077f1fcbbb2d33f96f65e226ec69e544fb85
|
data/Gemfile
CHANGED
data/History.txt
CHANGED
@@ -20,3 +20,11 @@
|
|
20
20
|
== 1.0.4
|
21
21
|
|
22
22
|
* Mapping values are now wrapped in double quotes by default
|
23
|
+
|
24
|
+
== 2.0.0
|
25
|
+
|
26
|
+
* Scalars are now dumped more consistently. Strings always have quotes, numbers never do.
|
27
|
+
This should address discrepancies that arise when dumping a string of digits. Previously,
|
28
|
+
the string would have been dumped either with "non-specific" tag notation (i.e. "!") or
|
29
|
+
without quotes. Both result in an integer when parsed. Now if you want to dump a number
|
30
|
+
as a string, convert it to a string before serializing.
|
@@ -115,24 +115,39 @@ class YamlWriteStream
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def write_scalar(value, quote = false)
|
118
|
-
|
118
|
+
case value
|
119
|
+
when Numeric
|
120
|
+
write_numeric_scalar(value)
|
121
|
+
when NilClass
|
122
|
+
write_nil_scalar
|
123
|
+
else
|
124
|
+
write_string_scalar(value.to_s, quote)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def write_string_scalar(value, quote = false)
|
129
|
+
style = if quote
|
119
130
|
Psych::Nodes::Scalar::DOUBLE_QUOTED
|
120
131
|
else
|
121
|
-
|
122
|
-
Psych::Nodes::Scalar::ANY
|
123
|
-
else
|
124
|
-
Psych::Nodes::Scalar::DOUBLE_QUOTED
|
125
|
-
end
|
132
|
+
Psych::Nodes::Scalar::PLAIN
|
126
133
|
end
|
127
134
|
|
128
|
-
|
129
|
-
|
135
|
+
# value, anchor, tag, plain, quoted, style
|
136
|
+
emitter.scalar(
|
137
|
+
value, nil, nil, true, true, style
|
138
|
+
)
|
139
|
+
end
|
130
140
|
|
141
|
+
def write_numeric_scalar(value)
|
131
142
|
# value, anchor, tag, plain, quoted, style
|
132
143
|
emitter.scalar(
|
133
|
-
value, nil, nil, true,
|
144
|
+
value.to_s, nil, nil, true, false, Psych::Nodes::Scalar::PLAIN
|
134
145
|
)
|
135
146
|
end
|
147
|
+
|
148
|
+
def write_nil_scalar
|
149
|
+
write_string_scalar('')
|
150
|
+
end
|
136
151
|
end
|
137
152
|
|
138
153
|
class StatefulMappingWriter < StatefulWriter
|
@@ -2,12 +2,14 @@
|
|
2
2
|
|
3
3
|
class YamlWriteStream
|
4
4
|
class YieldingWriter
|
5
|
-
attr_reader :emitter, :stream, :first
|
5
|
+
attr_reader :emitter, :stream, :first, :closed
|
6
|
+
alias_method :closed?, :closed
|
6
7
|
|
7
8
|
def initialize(emitter, stream)
|
8
9
|
@emitter = emitter
|
9
10
|
@stream = stream
|
10
11
|
@first = true
|
12
|
+
@closed = false
|
11
13
|
end
|
12
14
|
|
13
15
|
def flush
|
@@ -22,6 +24,7 @@ class YamlWriteStream
|
|
22
24
|
def close
|
23
25
|
flush
|
24
26
|
stream.close
|
27
|
+
@closed = true
|
25
28
|
nil
|
26
29
|
end
|
27
30
|
|
@@ -30,7 +33,7 @@ class YamlWriteStream
|
|
30
33
|
|
31
34
|
# anchor, tag, implicit, style
|
32
35
|
emitter.start_sequence(
|
33
|
-
nil, nil,
|
36
|
+
nil, nil, false, Psych::Nodes::Sequence::ANY
|
34
37
|
)
|
35
38
|
|
36
39
|
yield YieldingSequenceWriter.new(emitter, stream)
|
@@ -42,7 +45,7 @@ class YamlWriteStream
|
|
42
45
|
|
43
46
|
# anchor, tag, implicit, style
|
44
47
|
emitter.start_mapping(
|
45
|
-
nil, nil,
|
48
|
+
nil, nil, false, Psych::Nodes::Sequence::ANY
|
46
49
|
)
|
47
50
|
|
48
51
|
yield YieldingMappingWriter.new(emitter, stream)
|
@@ -52,26 +55,39 @@ class YamlWriteStream
|
|
52
55
|
protected
|
53
56
|
|
54
57
|
def write_scalar(value, quote = false)
|
55
|
-
|
58
|
+
case value
|
59
|
+
when Numeric
|
60
|
+
write_numeric_scalar(value)
|
61
|
+
when NilClass
|
62
|
+
write_nil_scalar
|
63
|
+
else
|
64
|
+
write_string_scalar(value.to_s, quote)
|
65
|
+
end
|
66
|
+
end
|
56
67
|
|
57
|
-
|
68
|
+
def write_string_scalar(value, quote = false)
|
69
|
+
style = if quote
|
58
70
|
Psych::Nodes::Scalar::DOUBLE_QUOTED
|
59
71
|
else
|
60
|
-
|
61
|
-
Psych::Nodes::Scalar::ANY
|
62
|
-
else
|
63
|
-
Psych::Nodes::Scalar::DOUBLE_QUOTED
|
64
|
-
end
|
72
|
+
Psych::Nodes::Scalar::PLAIN
|
65
73
|
end
|
66
74
|
|
67
|
-
|
68
|
-
|
75
|
+
# value, anchor, tag, plain, quoted, style
|
76
|
+
emitter.scalar(
|
77
|
+
value, nil, nil, true, true, style
|
78
|
+
)
|
79
|
+
end
|
69
80
|
|
81
|
+
def write_numeric_scalar(value)
|
70
82
|
# value, anchor, tag, plain, quoted, style
|
71
83
|
emitter.scalar(
|
72
|
-
value, nil, nil, true,
|
84
|
+
value.to_s, nil, nil, true, false, Psych::Nodes::Scalar::PLAIN
|
73
85
|
)
|
74
86
|
end
|
87
|
+
|
88
|
+
def write_nil_scalar
|
89
|
+
write_string_scalar('')
|
90
|
+
end
|
75
91
|
end
|
76
92
|
|
77
93
|
class YieldingMappingWriter < YieldingWriter
|
data/spec/spec_helper.rb
CHANGED
data/spec/stateful_spec.rb
CHANGED
@@ -31,7 +31,17 @@ describe YamlWriteStream::YieldingWriter do
|
|
31
31
|
stream_writer.write_key_value('def', 'ghi')
|
32
32
|
stream_writer.close
|
33
33
|
|
34
|
-
expect(stream.string).to eq(utf8("- abc\n- def:
|
34
|
+
expect(stream.string).to eq(utf8("- abc\n- def: \"ghi\"\n"))
|
35
|
+
expect(stream_writer).to be_closed
|
36
|
+
expect(stream).to be_closed
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'dumps numbers without quotes and without non-specific (implicit) tag notation' do
|
40
|
+
stream_writer.write_map
|
41
|
+
stream_writer.write_key_value('abc', 7)
|
42
|
+
stream_writer.close
|
43
|
+
|
44
|
+
expect(stream.string).to eq(utf8("abc: 7\n"))
|
35
45
|
expect(stream_writer).to be_closed
|
36
46
|
expect(stream).to be_closed
|
37
47
|
end
|
data/spec/yielding_spec.rb
CHANGED
@@ -29,6 +29,31 @@ describe YamlWriteStream::YieldingWriter do
|
|
29
29
|
expect(stream).to be_closed
|
30
30
|
end
|
31
31
|
|
32
|
+
it 'correctly writes to and closes the stream without non-specific (implicit) tag notation' do
|
33
|
+
stream_writer.write_sequence do |seq_writer|
|
34
|
+
seq_writer.write_element('abc')
|
35
|
+
seq_writer.write_map do |map_writer|
|
36
|
+
map_writer.write_key_value('def', 'ghi')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
stream_writer.close
|
41
|
+
expect(stream.string).to eq(utf8("- abc\n- def: \"ghi\"\n"))
|
42
|
+
expect(stream_writer).to be_closed
|
43
|
+
expect(stream).to be_closed
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'dumps numbers without quotes and without non-specific (implicit) tag notation' do
|
47
|
+
stream_writer.write_map do |map_writer|
|
48
|
+
map_writer.write_key_value('abc', 7)
|
49
|
+
end
|
50
|
+
|
51
|
+
stream_writer.close
|
52
|
+
expect(stream.string).to eq(utf8("abc: 7\n"))
|
53
|
+
expect(stream_writer).to be_closed
|
54
|
+
expect(stream).to be_closed
|
55
|
+
end
|
56
|
+
|
32
57
|
it 'quotes empty strings' do
|
33
58
|
stream_writer.write_map do |map_writer|
|
34
59
|
map_writer.write_key_value('foo', '')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml-write-stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An easy, streaming way to generate YAML.
|
14
14
|
email:
|