ox-builder 0.1.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmYwOGEzMTk1MzY5ZTIxNjMyNGVjMTMxZWZjNDdlZDU0MWM0NjMxZQ==
4
+ ZTA5YzU5MzYxYTZlMDQ5YjE0NWU4ODhlNTc2MDI5OTNmODllZTdjYg==
5
5
  data.tar.gz: !binary |-
6
- ZWU4NGU1ZDczYTY0ZWQ0M2IxYTZiNzU2YmQyNjQ0YzdmZjEwNWMxMw==
6
+ NzYzOGQ4ZjAwZjdmMTVjMjY3ZWZmZTFjYmQxNTQ1MDhhNWFiNzY1Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWIwNGY2NjQ4MTA2ZmM1ZmEwOTIxZmZjNmYyYWU0M2M0OTcyYWRlZGE2OTY0
10
- ZWMxMDE2NjYxNjA5OGYyNjM4MDA2ZTY0NDYwZGUyYjcwZDJkZjk3Zjg3NTY1
11
- NDc0NjcxOGRkODVhNzdhNzIwM2UyZTU0NTdlODAzZjI4NjBlYjE=
9
+ YzZhYzU4YjUyMDllYTZmYmZhMGFmNzE2N2I5ZjA5NDFlNDc4ZmZjM2MyZWVi
10
+ NDMxZTc2ZmYyOTNkY2FiMDUyZTBjOGE1Y2NjYmM1YWZkMTlmYjk5MWEyODM3
11
+ N2E2NWI4ZjE1NDYxOGQyNzNmZjZjOTI4NTgwMjBhZjg0YWY2NTI=
12
12
  data.tar.gz: !binary |-
13
- NTU4MWIyYTRhMDExMjBiYTNhMGUwM2JiODZiZTU5YWRkNzdiMjU0OTM3NTRi
14
- YTQ4ZGFmYzYyNzc1ZjgwNjdmOGYxZTVjYTAxNmVkNjMyMTk5NmVjZmZjY2Jk
15
- ZTQ0Y2Q2NDdiYWY5ODU5MDA4MzFhZTM3NjRiZGIxMTUyYmNlZDA=
13
+ MzFlNjkxN2Q0NzQ1YTM3ZThlOWY1ZTA0ZWIyNWE5NjIzNTEzNDcyZGQ0ZjYx
14
+ OGQ2ZjYyYjJlYjVlZWQ1ZGJlZDk2YWU3MTgyOWQwN2NjNjgwZTZiNjUzMmNj
15
+ MGM2NDUwNzY3NGMyMzNhZDExMjQzZjEyZjI0M2Q5NzJmYjk4OTc=
data/.travis.yml CHANGED
@@ -1,4 +1,11 @@
1
+ sudo: false
1
2
  language: ruby
3
+ cache:
4
+ - bundler
2
5
  rvm:
3
6
  - 1.9.3
7
+ - 2.0
8
+ - 2.1
9
+ - 2.2
10
+ - 2.3.0
4
11
  before_install: gem install bundler -v 1.11.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ ## Version 1.0.0
2
+ * Initial version of Ox::Builder
3
+ * Includes mappings for all Ox node types (element, instruct, doctype, comment)
4
+ * Includes template handler for ActionView and Tilt
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ox-builder.gemspec
4
4
  gemspec
5
-
6
- gem 'ox', '~> 2.3.0'
data/README.md CHANGED
@@ -1,8 +1,202 @@
1
1
  # Ox::Builder
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ox/builder`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Build Status](https://travis-ci.org/dvandersluis/ox-builder.svg?branch=master)](https://travis-ci.org/dvandersluis/ox-builder)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Ox::Builder provides a DSL for quickly building XML documents using the [`ox`](https://github.com/ohler55/ox) gem.
6
+ It is intended to be a practically plug-in replacement for [`builder`](https://rubygems.org/gems/builder) gem for
7
+ constructing XML documents.
8
+
9
+ The DSL verbs are meant to map closely to those provided by `builder` so that an existing builder template can be
10
+ ported to use `Ox::Builder` with minimum effort.
11
+
12
+ In addition to providing a DSL for `ox`, this library provides template handlers for `action_view` and `tilt`,
13
+ so that a template file can be rendered in Rails and/or via Tilt. The template handlers are activated automatically
14
+ when rendering a file named `filename.ox`.
15
+
16
+ ## Usage
17
+
18
+ An XML document can be generated by passing a block to `Ox::Builder.build`:
19
+
20
+ <table>
21
+ <tr><th>Ruby Code:</th><th>Generated XML:</th></tr>
22
+ <tr>
23
+ <td>
24
+ <pre lang="ruby">
25
+ doc = Ox::Builder.build do
26
+ instruct!
27
+
28
+ person id: 123 do
29
+ name { cdata!('John Smith') }
30
+ age 37
31
+ nationality 'Canadian'
32
+ end
33
+ end
34
+ </pre>
35
+ </td>
36
+ <td>
37
+ <pre lang="xml">
38
+ &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
39
+ &lt;person id="123"&gt;
40
+ &lt;name&gt;
41
+ &lt;![CDATA[John Smith]]&gt;
42
+ &lt;/name&gt;
43
+ &lt;age&gt;37&lt;/age&gt;
44
+ &lt;nationality&gt;Canadian&lt;/nationality&gt;
45
+ &lt;/person&gt;
46
+ </pre>
47
+ </td>
48
+ </tr>
49
+ </table>
50
+
51
+ ### Methods
52
+
53
+ The following methods are available within the `Ox::Builder.build` DSL:
54
+
55
+ #### `instruct!(name = :xml, attributes = { version: '1.0', encoding: 'UTF-8' })`
56
+
57
+ Creates an XML processing instruction. The name or attributes can be overridden.
58
+
59
+ | Ruby Code:| Generated XML: |
60
+ |-----------|----------------|
61
+ | <pre lang="ruby">instruct!</pre> | <pre lang="xml">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</pre> |
62
+ | <pre lang="ruby">instruct! version: 2.0</pre>| <pre lang="xml">&lt;?xml version="2.0"&gt;</pre> |
63
+ | <pre lang="ruby">instruct! 'xml-stylesheet', type: 'text/xsl', href: 'style.xsl'</pre> | <pre lang="xml">&lt;?xml-stylesheet type="text/xsl" href="style.xsl"?&gt;</pre> |
64
+
65
+ #### `tag!(name, content = nil, attributes = {})`
66
+
67
+ Creates an arbitrary XML node with a given name and any attributes. If content is passed in, it will be inserted into the tag; otherwise the tag will be empty. A block can be given to create nested tags (and there is no limit to how deep nesting can be).
68
+
69
+ If passing content to a tag, any object can be given but note that `to_s` will be called.
70
+
71
+ Nodes can also be created dynamically by name (assuming the name is a valid ruby method name). You can also append `!` to the name in case there is a conflict with an existing method (see examples below).
72
+
73
+ | Ruby Code:| Generated XML: |
74
+ |-----------|----------------|
75
+ | <pre lang="ruby">tag! :person</pre> | <pre lang="xml">&lt;person/&gt;</pre> |
76
+ | <pre lang="ruby">tag! :person, 'John Smith', id: '123'</pre>|<pre lang="xml">&lt;person id="123"&gt;John Smith&lt;/person&gt;</pre>|
77
+ | <pre lang="ruby">person 'John Smith', id: '123'</pre> | <pre lang="xml">&lt;person id="123"&gt;John Smith&lt;/person&gt;</pre> |
78
+ | <pre lang="ruby">person! 'John Smith', id: '123'</pre> | <pre lang="xml">&lt;person id="123"&gt;John Smith&lt;/person&gt;</pre> |
79
+ | <pre lang="ruby">person do&#13;&#10; first_name, 'John'&#13;&#10; last_name, 'Smith'&#13;&#10;&#13;&#10; address do&#13;&#10; country, 'Canada'&#13;&#10; state, 'Ontario'&#13;&#10; end&#13;&#10;end</pre> | <pre lang="xml">&lt;person&gt;&#13;&#10; &lt;first_name&gt;John&lt;/first_name&gt;&#13;&#10; &lt;last_name&gt;Smith&lt;/last_name&gt;&#13;&#10; &lt;address&gt;&#13;&#10; &lt;country&gt;Canada&lt;/country&gt;&#13;&#10; &lt;state&gt;Ontario&lt;/state&gt;&#13;&#10; &lt;/address&gt;&#13;&#10;&lt;/person&gt;&#13;&#10;</pre> |
80
+
81
+ #### `comment!(text)`
82
+
83
+ Creates an XML comment with the given text.
84
+
85
+ | Ruby Code: | Generated XML: |
86
+ |------------|----------------|
87
+ | <pre lang="ruby">comment!('comment goes here')</pre> | <pre lang="xml">&lt;!-- comment goes here --&gt;</pre> |
88
+
89
+ #### `doctype!(text)`
90
+
91
+ Creates a doctype element with the given text.
92
+
93
+ | Ruby Code: | Generated XML: |
94
+ |------------|----------------|
95
+ | <pre lang="ruby">doctype!('html')</pre> | <pre lang="xml">&lt;!DOCTYPE html &gt;</pre> |
96
+
97
+ #### `cdata!(text)`
98
+
99
+ Create a CDATA node (ie. text wrapped in `<![CDATA[]]>`)
100
+
101
+ | Ruby Code: | Generated XML: |
102
+ |------------|----------------|
103
+ | <pre lang="ruby">cdata! 'John Smith'</pre> | <pre lang="xml">&lt;![CDATA[John Smith]]&gt;</pre> |
104
+ | <pre lang="ruby">tag! :name do&#13;&#10; cdata!('John Smith')&#13;&#10;end</pre> | <pre lang="xml">&lt;name&gt;&#13;&#10; &lt;![CDATA[John Smith]]&gt;&#13;&#10;&lt;name&gt;</pre> |
105
+
106
+ ### Using in a Rails view
107
+
108
+ Any view file with the extension `.ox` or `.xml.ox` will be picked up by `Ox::Builder` and rendered as an XML file, with MIME type `application/xml`. There is no setup needed to get this to work. The view should not contain `Ox::Builder.build`, but rather the contents of the block that you would pass to it.
109
+
110
+ ```ruby
111
+ # test.xml.ox
112
+ instruct!
113
+
114
+ person id: 123 do
115
+ name { cdata!('John Smith') }
116
+ age 37
117
+ nationality 'Canadian'
118
+ end
119
+ ```
120
+
121
+ ## Migrating from builder
122
+
123
+ Using `Ox::Builder` instead of `Builder` is straightforward:
124
+
125
+ 1. Rename the view file from `filename.builder` to `filename.ox`.
126
+ 2. Remove any block parameters from the view code. As well, any code where the block parameter was used as a message receiver, use an implicit receiver instead.
127
+ 3. Remove any instances of the special `xml` receiver.
128
+ 4. `declare!` is not currently supported as it's not implemented in `ox`.
129
+
130
+ Therefore, the following template:
131
+
132
+ ```ruby
133
+ # filename.builder
134
+ xml.people do
135
+ xml.person do |person|
136
+ person.name { xml.cdata! 'John Smith' }
137
+ person.age 37
138
+ end
139
+ end
140
+ ```
141
+
142
+ would be converted to:
143
+
144
+ ```ruby
145
+ # filename.ox
146
+ people do
147
+ person do
148
+ name { cdata! 'John Smith' }
149
+ age 37
150
+ end
151
+ end
152
+ ```
153
+
154
+ ## Benchmarks
155
+
156
+ Benchmarks which compare `Ox::Builder` to `Builder` can be found in `benchmarks/`. There are three benchmarks, for
157
+ various XML output sizes.
158
+
159
+ ### Small file (~170 bytes)
160
+ ```
161
+ Calculating -------------------------------------
162
+ builder 412.000 i/100ms
163
+ ox 640.000 i/100ms
164
+ -------------------------------------------------
165
+ builder 4.230k (± 6.7%) i/s - 42.436k
166
+ ox 10.413k (±18.5%) i/s - 100.480k
167
+
168
+ Comparison:
169
+ ox: 10413.5 i/s
170
+ builder: 4230.5 i/s - 2.46x slower
171
+ ```
172
+
173
+ ### Medium file (107K)
174
+ ```
175
+ Calculating -------------------------------------
176
+ builder 1.000 i/100ms
177
+ ox 2.000 i/100ms
178
+ -------------------------------------------------
179
+ builder 11.153 (± 9.0%) i/s - 223.000
180
+ ox 23.454 (± 8.5%) i/s - 468.000
181
+
182
+ Comparison:
183
+ ox: 23.5 i/s
184
+ builder: 11.2 i/s - 2.10x slower
185
+ ```
186
+
187
+ ### Huge file (11M)
188
+ ```
189
+ Calculating -------------------------------------
190
+ builder 1.000 i/100ms
191
+ ox 1.000 i/100ms
192
+ -------------------------------------------------
193
+ builder 0.117 (± 0.0%) i/s - 8.000 in 68.435529s
194
+ ox 0.221 (± 0.0%) i/s - 14.000 in 63.819146s
195
+
196
+ Comparison:
197
+ ox: 0.2 i/s
198
+ builder: 0.1 i/s - 1.88x slower
199
+ ```
6
200
 
7
201
  ## Installation
8
202
 
@@ -20,10 +214,6 @@ Or install it yourself as:
20
214
 
21
215
  $ gem install ox-builder
22
216
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
217
  ## Development
28
218
 
29
219
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -32,8 +222,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
222
 
33
223
  ## Contributing
34
224
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ox-builder.
36
-
225
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dvandersluis/ox-builder.
37
226
 
38
227
  ## License
39
228