xml-write-stream 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +4 -0
- data/lib/xml-write-stream/base.rb +0 -5
- data/lib/xml-write-stream/stateful_writer.rb +41 -6
- data/lib/xml-write-stream/version.rb +1 -1
- data/lib/xml-write-stream/yielding_writer.rb +4 -0
- data/spec/stateful_spec.rb +29 -8
- metadata +13 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b8810869755e31ba4eb0c0efd716787b8eb25de
|
4
|
+
data.tar.gz: 6ac7aae4aee3fcb9eddbd0e7db7084e12a7f3034
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55ac91d6d9a2597e8025c2f29cfe120206056ccabc6e894db31fbdcc820ac1da49d34c8fb970ab74da72d156f4812432bae25c44dfe0508334dfb9c32ea0a783
|
7
|
+
data.tar.gz: 1f5279c539a02ad48925764de74e97876089e2948881ba4a39d52c9d60856acf8eac5b86f09ccf23ba1ff0020d2c5cf12baa90caff20d7fb747986f5ae0674de
|
data/History.txt
CHANGED
@@ -32,13 +32,10 @@ class XmlWriteStream
|
|
32
32
|
|
33
33
|
def write_text(text, options = {})
|
34
34
|
escape = options.fetch(:escape, true)
|
35
|
-
stream.write(indent_spaces)
|
36
35
|
|
37
36
|
stream.write(
|
38
37
|
escape ? escape_text(text) : text
|
39
38
|
)
|
40
|
-
|
41
|
-
write_newline
|
42
39
|
end
|
43
40
|
|
44
41
|
def write_header(attributes = {})
|
@@ -70,7 +67,6 @@ class XmlWriteStream
|
|
70
67
|
end
|
71
68
|
|
72
69
|
def write_open_tag(tag_name, attributes)
|
73
|
-
stream.write(indent_spaces)
|
74
70
|
stream.write("<#{tag_name}")
|
75
71
|
|
76
72
|
if attributes.size > 0
|
@@ -82,7 +78,6 @@ class XmlWriteStream
|
|
82
78
|
end
|
83
79
|
|
84
80
|
def write_close_tag(tag_name)
|
85
|
-
stream.write(indent_spaces)
|
86
81
|
stream.write("</#{tag_name}>")
|
87
82
|
end
|
88
83
|
|
@@ -1,6 +1,17 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
3
|
class XmlWriteStream
|
4
|
+
class StackItem
|
5
|
+
attr_reader :tag_name, :multiline
|
6
|
+
|
7
|
+
def initialize(tag_name, multiline = true)
|
8
|
+
@tag_name = tag_name
|
9
|
+
@multiline = multiline
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :multiline?, :multiline
|
13
|
+
end
|
14
|
+
|
4
15
|
class StatefulWriter < Base
|
5
16
|
attr_reader :stream, :stack, :closed, :indent, :index
|
6
17
|
alias :closed? :closed
|
@@ -14,15 +25,31 @@ class XmlWriteStream
|
|
14
25
|
end
|
15
26
|
|
16
27
|
def open_tag(tag_name, attributes = {})
|
28
|
+
open_tag_helper(tag_name, true, attributes)
|
29
|
+
end
|
30
|
+
|
31
|
+
def open_single_line_tag(tag_name, attributes = {})
|
32
|
+
open_tag_helper(tag_name, false, attributes)
|
33
|
+
end
|
34
|
+
|
35
|
+
def open_tag_helper(tag_name, multiline, attributes = {})
|
17
36
|
check_eos
|
18
|
-
|
37
|
+
|
38
|
+
if index == 0
|
39
|
+
stack.push(StackItem.new(tag_name, multiline))
|
40
|
+
end
|
19
41
|
|
20
42
|
check_tag_name(tag_name)
|
21
43
|
check_attributes(attributes)
|
44
|
+
stream.write(indent_spaces) if index > 0 && current.multiline?
|
22
45
|
write_open_tag(tag_name, attributes)
|
23
|
-
write_newline
|
46
|
+
write_newline if multiline
|
47
|
+
|
48
|
+
if index > 0
|
49
|
+
stack.push(StackItem.new(tag_name, multiline))
|
50
|
+
end
|
24
51
|
|
25
|
-
|
52
|
+
@index += 1
|
26
53
|
end
|
27
54
|
|
28
55
|
def write_text(text, options = {})
|
@@ -32,7 +59,10 @@ class XmlWriteStream
|
|
32
59
|
raise NoTopLevelTagError
|
33
60
|
end
|
34
61
|
|
62
|
+
stream.write(indent_spaces) if current.multiline?
|
35
63
|
super
|
64
|
+
|
65
|
+
write_newline if current.multiline?
|
36
66
|
end
|
37
67
|
|
38
68
|
def write_header(attributes = {})
|
@@ -46,9 +76,10 @@ class XmlWriteStream
|
|
46
76
|
|
47
77
|
def close_tag(options = {})
|
48
78
|
if in_tag?
|
49
|
-
|
50
|
-
|
51
|
-
|
79
|
+
stack_item = stack.pop
|
80
|
+
stream.write(indent_spaces) if stack_item.multiline?
|
81
|
+
write_close_tag(stack_item.tag_name)
|
82
|
+
write_newline if current && current.multiline?
|
52
83
|
end
|
53
84
|
end
|
54
85
|
|
@@ -72,6 +103,10 @@ class XmlWriteStream
|
|
72
103
|
(stack.size == 0 && index > 0) || closed?
|
73
104
|
end
|
74
105
|
|
106
|
+
def current
|
107
|
+
stack.last
|
108
|
+
end
|
109
|
+
|
75
110
|
protected
|
76
111
|
|
77
112
|
def indent_spaces
|
@@ -14,12 +14,14 @@ class XmlWriteStream
|
|
14
14
|
check_closed
|
15
15
|
check_tag_name(tag_name)
|
16
16
|
check_attributes(attributes)
|
17
|
+
stream.write(indent_spaces)
|
17
18
|
write_open_tag(tag_name, attributes)
|
18
19
|
write_newline
|
19
20
|
|
20
21
|
@level += 1
|
21
22
|
yield self if block_given?
|
22
23
|
@level -= 1
|
24
|
+
stream.write(indent_spaces)
|
23
25
|
write_close_tag(tag_name)
|
24
26
|
write_newline
|
25
27
|
end
|
@@ -31,7 +33,9 @@ class XmlWriteStream
|
|
31
33
|
raise NoTopLevelTagError
|
32
34
|
end
|
33
35
|
|
36
|
+
stream.write(indent_spaces)
|
34
37
|
super
|
38
|
+
write_newline
|
35
39
|
end
|
36
40
|
|
37
41
|
def write_header(attributes = {})
|
data/spec/stateful_spec.rb
CHANGED
@@ -45,13 +45,34 @@ describe XmlWriteStream::YieldingWriter do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
+
describe '#open_single_line_tag' do
|
49
|
+
it 'writes a tag and its contents on a single line' do
|
50
|
+
stream_writer.open_single_line_tag('maytag')
|
51
|
+
stream_writer.write_text('foobar')
|
52
|
+
stream_writer.close_tag
|
53
|
+
expect(stream.string).to eq("<maytag>foobar</maytag>")
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'works with nested tags' do
|
57
|
+
stream_writer.open_single_line_tag('maytag')
|
58
|
+
stream_writer.write_text('foobar')
|
59
|
+
stream_writer.open_single_line_tag('gutentag')
|
60
|
+
stream_writer.write_text('foobaz')
|
61
|
+
stream_writer.close_tag
|
62
|
+
stream_writer.close_tag
|
63
|
+
expect(stream.string).to eq(
|
64
|
+
"<maytag>foobar<gutentag>foobaz</gutentag></maytag>"
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
48
69
|
describe '#open_tag' do
|
49
70
|
it 'writes an opening tag' do
|
50
71
|
stream_writer.open_tag('maytag')
|
51
72
|
stream_writer.close
|
52
73
|
|
53
74
|
expect(stream.string).to eq(
|
54
|
-
utf8("<maytag>\n</maytag
|
75
|
+
utf8("<maytag>\n</maytag>")
|
55
76
|
)
|
56
77
|
end
|
57
78
|
|
@@ -60,7 +81,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
60
81
|
stream_writer.close
|
61
82
|
|
62
83
|
expect(stream.string).to eq(
|
63
|
-
utf8("<maytag type=\"washing_machine\">\n</maytag
|
84
|
+
utf8("<maytag type=\"washing_machine\">\n</maytag>")
|
64
85
|
)
|
65
86
|
end
|
66
87
|
|
@@ -82,7 +103,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
82
103
|
stream_writer.close
|
83
104
|
|
84
105
|
expect(stream.string).to eq(
|
85
|
-
utf8("<foo9>\n <bar:baz>\n </bar:baz>\n</foo9
|
106
|
+
utf8("<foo9>\n <bar:baz>\n </bar:baz>\n</foo9>")
|
86
107
|
)
|
87
108
|
end
|
88
109
|
|
@@ -101,7 +122,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
101
122
|
stream_writer.close_tag
|
102
123
|
|
103
124
|
expect(stream.string).to eq(
|
104
|
-
utf8("<maytag>\n</maytag
|
125
|
+
utf8("<maytag>\n</maytag>")
|
105
126
|
)
|
106
127
|
end
|
107
128
|
end
|
@@ -113,7 +134,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
113
134
|
stream_writer.close
|
114
135
|
|
115
136
|
expect(stream.string).to eq(
|
116
|
-
utf8("<places>\n Alaska & Hawai'i\n</places
|
137
|
+
utf8("<places>\n Alaska & Hawai'i\n</places>")
|
117
138
|
)
|
118
139
|
end
|
119
140
|
|
@@ -123,7 +144,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
123
144
|
stream_writer.close
|
124
145
|
|
125
146
|
expect(stream.string).to eq(
|
126
|
-
utf8("<places>\n Alaska & Hawai'i\n</places
|
147
|
+
utf8("<places>\n Alaska & Hawai'i\n</places>")
|
127
148
|
)
|
128
149
|
end
|
129
150
|
|
@@ -150,7 +171,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
150
171
|
stream_writer.flush
|
151
172
|
|
152
173
|
expect(stream.string).to eq(
|
153
|
-
utf8("<foo>\n <bar>\n <baz>\n </baz>\n </bar>\n</foo
|
174
|
+
utf8("<foo>\n <bar>\n <baz>\n </baz>\n </bar>\n</foo>")
|
154
175
|
)
|
155
176
|
|
156
177
|
expect(stream).to_not be_closed
|
@@ -166,7 +187,7 @@ describe XmlWriteStream::YieldingWriter do
|
|
166
187
|
stream_writer.close
|
167
188
|
|
168
189
|
expect(stream.string).to eq(
|
169
|
-
utf8("<foo>\n <bar>\n <baz>\n </baz>\n </bar>\n</foo
|
190
|
+
utf8("<foo>\n <bar>\n <baz>\n </baz>\n </bar>\n</foo>")
|
170
191
|
)
|
171
192
|
|
172
193
|
expect(stream).to be_closed
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-write-stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An easy, streaming way to generate XML.
|
14
14
|
email:
|
@@ -17,6 +17,10 @@ executables: []
|
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- Gemfile
|
21
|
+
- History.txt
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
20
24
|
- lib/xml-write-stream.rb
|
21
25
|
- lib/xml-write-stream/base.rb
|
22
26
|
- lib/xml-write-stream/stateful_writer.rb
|
@@ -26,32 +30,28 @@ files:
|
|
26
30
|
- spec/stateful_spec.rb
|
27
31
|
- spec/xml-write-stream_spec.rb
|
28
32
|
- spec/yielding_spec.rb
|
29
|
-
- Gemfile
|
30
|
-
- History.txt
|
31
|
-
- README.md
|
32
|
-
- Rakefile
|
33
33
|
- xml-write-stream.gemspec
|
34
34
|
homepage: http://github.com/camertron
|
35
35
|
licenses: []
|
36
36
|
metadata: {}
|
37
|
-
post_install_message:
|
37
|
+
post_install_message:
|
38
38
|
rdoc_options: []
|
39
39
|
require_paths:
|
40
40
|
- lib
|
41
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
|
-
- -
|
48
|
+
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
|
-
rubyforge_project:
|
53
|
-
rubygems_version: 2.
|
54
|
-
signing_key:
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 2.2.3
|
54
|
+
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: An easy, streaming way to generate XML.
|
57
57
|
test_files: []
|