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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5150ab0601006ad44967a247d05199fd95a260fa
4
- data.tar.gz: f512124a33caaf8ca2ea52920ce63879d43d1c8e
3
+ metadata.gz: ae8a65b60178d130ae20ab1ed7b69b7da808877b
4
+ data.tar.gz: 5bdaf04f74ef81c54d75f61e453a65b09059284f
5
5
  SHA512:
6
- metadata.gz: 6b0ab08d6209fa31bb09a24168ee03abfc81a4eb852100a57c8e63a7e5f4ec5099f30390e30e5a4414c4a8f3677c5b8fadd3247a29c52d61f684804509d3a357
7
- data.tar.gz: 198b820deec8fa0383daefa6d13f8055676860a032c7027961732f90645d707a70c45189929ebd8bb10b9fa26e8af8f7fca0c95028e6f2ec9ba1d860523d98cd
6
+ metadata.gz: 0a56250ac350ab119588f4a2b86560742ee9df190d0d42601d401f38621c9f9bf3bd5a3fbffdc05199e0d0d3e7e3b0a90ef8c43da6e37f8c001414daeb2e9a19
7
+ data.tar.gz: 439293700442a20ca04e47227e352702eb23ae3ba91edd0788f75e260b1cdf7deb459b8e0daa9adf28ab7d13ba1a077f1fcbbb2d33f96f65e226ec69e544fb85
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ gemspec
4
4
 
5
5
  group :development, :test do
6
6
  gem 'pry', '~> 0.9.0'
7
- gem 'pry-nav'
7
+ gem 'pry-byebug'
8
8
  gem 'rake'
9
9
  end
10
10
 
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
- style = if value == ''
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
- if !quote || !value
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
- quoted = value == ''
129
- value = value ? value : ''
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, quoted, style
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
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  class YamlWriteStream
4
- VERSION = "1.0.4"
4
+ VERSION = '2.0.0'
5
5
  end
@@ -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, true, Psych::Nodes::Sequence::ANY
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, true, Psych::Nodes::Sequence::ANY
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
- @first = false
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
- style = if value == ''
68
+ def write_string_scalar(value, quote = false)
69
+ style = if quote
58
70
  Psych::Nodes::Scalar::DOUBLE_QUOTED
59
71
  else
60
- if !quote || !value
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
- quoted = value == ''
68
- value = value ? value : ''
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, quoted, style
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
@@ -3,7 +3,7 @@
3
3
  require 'rspec'
4
4
  require 'yaml-write-stream'
5
5
  require 'shared_examples'
6
- require 'pry-nav'
6
+ require 'pry-byebug'
7
7
 
8
8
  RSpec.configure do |config|
9
9
  config.mock_with :rr
@@ -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: ! \"ghi\"\n"))
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
@@ -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: 1.0.4
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-02-22 00:00:00.000000000 Z
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: