chef-gyoku 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +62 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +154 -0
- data/Gemfile +6 -0
- data/MIT-LICENSE +20 -0
- data/README.md +313 -0
- data/Rakefile +12 -0
- data/gyoku.gemspec +27 -0
- data/lib/gyoku/array.rb +96 -0
- data/lib/gyoku/hash.rb +105 -0
- data/lib/gyoku/prettifier.rb +29 -0
- data/lib/gyoku/version.rb +3 -0
- data/lib/gyoku/xml_key.rb +67 -0
- data/lib/gyoku/xml_value.rb +41 -0
- data/lib/gyoku.rb +14 -0
- data/spec/gyoku/array_spec.rb +121 -0
- data/spec/gyoku/hash_spec.rb +431 -0
- data/spec/gyoku/prettifier_spec.rb +39 -0
- data/spec/gyoku/xml_key_spec.rb +76 -0
- data/spec/gyoku/xml_value_spec.rb +61 -0
- data/spec/gyoku_spec.rb +82 -0
- data/spec/spec_helper.rb +15 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 961beab424895dc794bc90010c758f6b6ce1423a8b2b1248c64796a7874cc76e
|
4
|
+
data.tar.gz: bef27d17bcc60cb5e2c10761230bcfa459243e2c71240229816fa036c82b40d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0255cddf2cc2448f6f30840ac97f54c846d7e9a8728087318d67bb149d0efaf2208bfd2e87ca0c41050a2d5abfd84b4e01872c81739c55b9e8c88867be41a45f
|
7
|
+
data.tar.gz: fa9bf05243f96bda7ab2564c805c817e2cdab018112773660f9214ec8151084a7f3e1f932f804b8506d8977db0f460396084833143d39fb678b7cfc6216bc91e
|
@@ -0,0 +1,62 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
pull_request:
|
8
|
+
branches:
|
9
|
+
- '*'
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
test:
|
13
|
+
name: Ruby ${{ matrix.ruby }}
|
14
|
+
|
15
|
+
strategy:
|
16
|
+
fail-fast: true
|
17
|
+
matrix:
|
18
|
+
ruby:
|
19
|
+
- '3.2'
|
20
|
+
- '3.1'
|
21
|
+
- '3.0'
|
22
|
+
- 'jruby'
|
23
|
+
|
24
|
+
runs-on: ubuntu-latest
|
25
|
+
|
26
|
+
steps:
|
27
|
+
- name: Checkout
|
28
|
+
uses: actions/checkout@v3
|
29
|
+
|
30
|
+
- name: Install Ruby ${{ matrix.ruby }}
|
31
|
+
uses: ruby/setup-ruby@v1
|
32
|
+
with:
|
33
|
+
ruby-version: ${{ matrix.ruby }}
|
34
|
+
bundler-cache: true # 'bundle install' and cache
|
35
|
+
|
36
|
+
- name: Run tests
|
37
|
+
run: bundle exec rake --trace
|
38
|
+
continue-on-error: false
|
39
|
+
|
40
|
+
coveralls:
|
41
|
+
name: Coveralls
|
42
|
+
runs-on: ubuntu-latest
|
43
|
+
steps:
|
44
|
+
- name: Checkout
|
45
|
+
uses: actions/checkout@v3
|
46
|
+
|
47
|
+
- name: Set up Ruby
|
48
|
+
uses: ruby/setup-ruby@v1
|
49
|
+
with:
|
50
|
+
ruby-version: 3.2
|
51
|
+
bundler-cache: true
|
52
|
+
|
53
|
+
- name: Install dependencies
|
54
|
+
run: bundle install
|
55
|
+
|
56
|
+
- name: Run tests
|
57
|
+
run: bundle exec rake
|
58
|
+
|
59
|
+
- name: Report coverage
|
60
|
+
uses: coverallsapp/github-action@v2
|
61
|
+
with:
|
62
|
+
flag-name: ruby-3.2
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
## 1.4.0 (2022-04-01)
|
4
|
+
|
5
|
+
### Fixed
|
6
|
+
|
7
|
+
- Fix [Issue #56](https://github.com/savonrb/gyoku/issues/56) with PR [#57](https://github.com/savonrb/gyoku/pull/57). Thanks, [@jpmoral]!
|
8
|
+
- Avoid circular reference [#69](https://github.com/savonrb/gyoku/pull/69), thanks [@ccarruitero]!
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
- Unwrap specific keys [#54](https://github.com/savonrb/gyoku/pull/54), by [@rlburkes]. Documented by [@mahemoff]. Thanks to you both!
|
13
|
+
- Add `:pretty_print`, `:indent` and `:compact` options to allow prettified XML output. [#59](https://github.com/savonrb/gyoku/pull/59), by [@Jeiwan]. Thanks!
|
14
|
+
|
15
|
+
### Changed
|
16
|
+
|
17
|
+
- Removed Rubinius support, by [@olleolleolle]
|
18
|
+
- Clean-up, CI setup, and changelog authoring, by [@olleolleolle]
|
19
|
+
|
20
|
+
[@jpmoral]: https://github.com/jpmoral
|
21
|
+
[@ccarruitero]: https://github.com/ccarruitero
|
22
|
+
[@rlburkes]: https://github.com/rlburkes
|
23
|
+
[@mahemoff]: https://github.com/mahemoff
|
24
|
+
[@Jeiwan]: https://github.com/Jeiwan
|
25
|
+
[@olleolleolle]: https://github.com/olleolleolle
|
26
|
+
|
27
|
+
## 1.3.1 (2015-04-05)
|
28
|
+
|
29
|
+
* Feature: [#53](https://github.com/savonrb/gyoku/pull/53) Improved serialization of hashes nested in arrays. Thanks to @riburkes for this!
|
30
|
+
|
31
|
+
## 1.3.0 (2015-03-30)
|
32
|
+
|
33
|
+
* Formally drop support for ruby 1.8.7
|
34
|
+
|
35
|
+
## 1.2.3 (2015-03-10)
|
36
|
+
|
37
|
+
* Feature: [#52](https://github.com/savonrb/gyoku/pull/52) Adds an :unwrap option that allows an array of hashes to be unwrapped into a single array xml node, rather than one per hash.
|
38
|
+
|
39
|
+
## 1.2.2 (2014-09-22)
|
40
|
+
|
41
|
+
* Fixed a bug introduced by making Gyoku threadsafe. Who knew that `$1` and the block variable that `#gsub` provides are not the same?
|
42
|
+
|
43
|
+
## 1.2.1 (2014-09-22)
|
44
|
+
|
45
|
+
* Fix : [#46](https://github.com/savonrb/gyoku/pull/46) Fixed an issue where Gyoku was not threadsafe. Gyoku should now be relatively more threadsafe due to less usage of global variables.
|
46
|
+
|
47
|
+
## 1.2.0 (2014-09-18)
|
48
|
+
|
49
|
+
* Feature: [#44](https://github.com/savonrb/gyoku/pull/44) support for sorting via :order! with a string key
|
50
|
+
|
51
|
+
## 1.1.1 (2014-01-02)
|
52
|
+
|
53
|
+
* Feature: [#38](https://github.com/savonrb/gyoku/pull/38) support for building nested Arrays
|
54
|
+
* Feature: [#36](https://github.com/savonrb/gyoku/pull/36) allow setting any objects content with :content!
|
55
|
+
* Deprecation: Support for ree and ruby 1.8.7 will be going away soon.
|
56
|
+
|
57
|
+
## 1.1.0 (2013-07-26)
|
58
|
+
|
59
|
+
* Feature: [#30](https://github.com/savonrb/gyoku/pull/30) support for building Arrays
|
60
|
+
of parent tags using @attributes.
|
61
|
+
|
62
|
+
* Fix: [#21](https://github.com/savonrb/gyoku/pull/21) stop modifying the original Hash.
|
63
|
+
The original issue is [savonrb/savon#410](https://github.com/savonrb/savon/issues/410).
|
64
|
+
|
65
|
+
## 1.0.0 (2012-12-17)
|
66
|
+
|
67
|
+
* Refactoring: Removed the global configuration. This should really only affect the
|
68
|
+
`Gyoku.convert_symbols_to` shortcut which was removed as well. If you're using Gyoku
|
69
|
+
with Savon 2.0, there's now an option for that. If you're using Gyoku on itself,
|
70
|
+
you can pass it the `:key_converter` option instead.
|
71
|
+
|
72
|
+
## 0.5.0 (2012-12-15)
|
73
|
+
|
74
|
+
Feature: [#19](https://github.com/savonrb/gyoku/pull/19) adds support for explicit XML attributes.
|
75
|
+
|
76
|
+
Feature: [#17](https://github.com/savonrb/gyoku/pull/17) adds an `:upcase` formula.
|
77
|
+
|
78
|
+
## 0.4.6 (2012-06-28)
|
79
|
+
|
80
|
+
* Fix: [#16](https://github.com/rubiii/gyoku/issues/16) Date objects were mapped like DateTime objects.
|
81
|
+
|
82
|
+
Gyoku.xml(date: Date.today) # => "<date>2012-06-28</date>"
|
83
|
+
|
84
|
+
* Fix: Time objects were also mapped like DateTime objects.
|
85
|
+
|
86
|
+
Gyoku.xml(time: sunday) # => "<time>16:22:33</time>"
|
87
|
+
|
88
|
+
## 0.4.5 (2012-05-28)
|
89
|
+
|
90
|
+
* Fix: [issue 8](https://github.com/rubiii/gyoku/issues/8) -
|
91
|
+
Conflict between camelcase methods in Rails.
|
92
|
+
|
93
|
+
* Fix: [pull request 15](https://github.com/rubiii/gyoku/pull/15) -
|
94
|
+
Gyoku generates blank attribute values if there are fewer attribute
|
95
|
+
values in attributes! than elements.
|
96
|
+
|
97
|
+
* Fix: [issue 12](https://github.com/rubiii/gyoku/issues/12) -
|
98
|
+
Don't remove special keys from the original Hash.
|
99
|
+
|
100
|
+
## 0.4.4
|
101
|
+
|
102
|
+
* Fix: [issue 6](https://github.com/rubiii/gyoku/issues/6) -
|
103
|
+
`Gyoku.xml` does not modify the original Hash.
|
104
|
+
|
105
|
+
## 0.4.3
|
106
|
+
|
107
|
+
* Fix: Make sure `require "date"` when necessary.
|
108
|
+
|
109
|
+
## 0.4.2
|
110
|
+
|
111
|
+
* Fix: `Array.to_xml` so that the given :namespace is applied to every element
|
112
|
+
in an Array.
|
113
|
+
|
114
|
+
## 0.4.1
|
115
|
+
|
116
|
+
* Fix: Alternative formulas and namespaces.
|
117
|
+
|
118
|
+
## 0.4.0
|
119
|
+
|
120
|
+
* Feature: Added alternative Symbol conversion formulas. You can choose between
|
121
|
+
:lower_camelcase (the default), :camelcase and :none.
|
122
|
+
|
123
|
+
Gyoku.convert_symbols_to :camelcase
|
124
|
+
|
125
|
+
You can even define your own formula:
|
126
|
+
|
127
|
+
Gyoku.convert_symbols_to { |key| key.upcase }
|
128
|
+
|
129
|
+
## 0.3.1
|
130
|
+
|
131
|
+
* Feature: Gyoku now calls Proc objects and converts their return value.
|
132
|
+
|
133
|
+
## 0.3.0
|
134
|
+
|
135
|
+
* Feature: Now when all Hash keys need to be namespaced (like with
|
136
|
+
elementFormDefault), you can use options to to trigger this behavior.
|
137
|
+
|
138
|
+
Gyoku.xml hash,
|
139
|
+
:element_form_default => :qualified,
|
140
|
+
:namespace => :v2
|
141
|
+
|
142
|
+
## 0.2.0
|
143
|
+
|
144
|
+
* Feature: Added support for self-closing tags. Hash keys ending with a forward
|
145
|
+
slash (regardless of their value) are now converted to self-closing tags.
|
146
|
+
|
147
|
+
## 0.1.1
|
148
|
+
|
149
|
+
* Fix: Allow people to use new versions of builder.
|
150
|
+
|
151
|
+
## 0.1.0
|
152
|
+
|
153
|
+
* Initial version. Gyoku was born as a core extension inside the
|
154
|
+
[Savon](http://rubygems.org/gems/savon) library.
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Daniel Harrington
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,313 @@
|
|
1
|
+
# Gyoku
|
2
|
+
|
3
|
+
Gyoku translates Ruby Hashes to XML.
|
4
|
+
|
5
|
+
``` ruby
|
6
|
+
Gyoku.xml(:find_user => { :id => 123, "v1:Key" => "api" })
|
7
|
+
# => "<findUser><id>123</id><v1:Key>api</v1:Key></findUser>"
|
8
|
+
```
|
9
|
+
|
10
|
+
[![Build status](https://github.com/savonrb/gyoku/actions/workflows/ci.yml/badge.svg)](https://github.com/savonrb/gyoku/actions/workflows/ci.yml)
|
11
|
+
[![Gem Version](https://badge.fury.io/rb/gyoku.svg)](http://badge.fury.io/rb/gyoku)
|
12
|
+
[![Code Climate](https://codeclimate.com/github/savonrb/gyoku.svg)](https://codeclimate.com/github/savonrb/gyoku)
|
13
|
+
[![Coverage Status](https://coveralls.io/repos/savonrb/gyoku/badge.svg?branch=master)](https://coveralls.io/r/savonrb/gyoku)
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Gyoku is available through [Rubygems](http://rubygems.org/gems/gyoku) and can be installed via:
|
19
|
+
|
20
|
+
``` bash
|
21
|
+
$ gem install gyoku
|
22
|
+
```
|
23
|
+
|
24
|
+
or add it to your Gemfile like this:
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
gem 'gyoku', '~> 1.0'
|
28
|
+
```
|
29
|
+
|
30
|
+
|
31
|
+
## Hash keys
|
32
|
+
|
33
|
+
Hash key Symbols are converted to lowerCamelCase Strings.
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
Gyoku.xml(:lower_camel_case => "key")
|
37
|
+
# => "<lowerCamelCase>key</lowerCamelCase>"
|
38
|
+
```
|
39
|
+
|
40
|
+
You can change the default conversion formula to `:camelcase`, `:upcase` or `:none`.
|
41
|
+
Note that options are passed as a second Hash to the `.xml` method.
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
Gyoku.xml({ :camel_case => "key" }, { :key_converter => :camelcase })
|
45
|
+
# => "<CamelCase>key</CamelCase>"
|
46
|
+
```
|
47
|
+
|
48
|
+
Custom key converters. You can use a lambda/Proc to provide customer key converters.
|
49
|
+
This is a great way to leverage active support inflections for domain specific acronyms.
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
# Use camelize lower which will hook into active support if installed.
|
53
|
+
Gyoku.xml({ acronym_abc: "value" }, key_converter: lambda { |key| key.camelize(:lower) })
|
54
|
+
# => "<acronymABC>value</acronymABC>"
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
Hash key Strings are not converted and may contain namespaces.
|
59
|
+
|
60
|
+
``` ruby
|
61
|
+
Gyoku.xml("XML" => "key")
|
62
|
+
# => "<XML>key</XML>"
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
## Hash values
|
67
|
+
|
68
|
+
* DateTime objects are converted to xs:dateTime Strings
|
69
|
+
* Objects responding to :to_datetime (except Strings) are converted to xs:dateTime Strings
|
70
|
+
* TrueClass and FalseClass objects are converted to "true" and "false" Strings
|
71
|
+
* NilClass objects are converted to xsi:nil tags
|
72
|
+
* These conventions are also applied to the return value of objects responding to :call
|
73
|
+
* All other objects are converted to Strings using :to_s
|
74
|
+
|
75
|
+
## Array values
|
76
|
+
|
77
|
+
Array items are by default wrapped with the containiner tag, which may be unexpected.
|
78
|
+
|
79
|
+
``` ruby
|
80
|
+
> Gyoku.xml({languages: [{language: 'ruby'},{language: 'java'}]})
|
81
|
+
# => "<languages><language>ruby</language></languages><languages><language>java</language></languages>"
|
82
|
+
```
|
83
|
+
|
84
|
+
You can set the `unwrap` option to remove this behavior.
|
85
|
+
|
86
|
+
``` ruby
|
87
|
+
> Gyoku.xml({languages: [{language: 'ruby'},{language: 'java'}]}, { unwrap: true})
|
88
|
+
# => "<languages><language>ruby</language><language>java</language></languages>"
|
89
|
+
```
|
90
|
+
|
91
|
+
## Special characters
|
92
|
+
|
93
|
+
Gyoku escapes special characters unless the Hash key ends with an exclamation mark.
|
94
|
+
|
95
|
+
``` ruby
|
96
|
+
Gyoku.xml(:escaped => "<tag />", :not_escaped! => "<tag />")
|
97
|
+
# => "<escaped><tag /></escaped><notEscaped><tag /></notEscaped>"
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
## Self-closing tags
|
102
|
+
|
103
|
+
Hash Keys ending with a forward slash create self-closing tags.
|
104
|
+
|
105
|
+
``` ruby
|
106
|
+
Gyoku.xml(:"self_closing/" => "", "selfClosing/" => nil)
|
107
|
+
# => "<selfClosing/><selfClosing/>"
|
108
|
+
```
|
109
|
+
|
110
|
+
|
111
|
+
## Sort XML tags
|
112
|
+
|
113
|
+
In case you need the XML tags to be in a specific order, you can specify the order
|
114
|
+
through an additional Array stored under the `:order!` key.
|
115
|
+
|
116
|
+
``` ruby
|
117
|
+
Gyoku.xml(:name => "Eve", :id => 1, :order! => [:id, :name])
|
118
|
+
# => "<id>1</id><name>Eve</name>"
|
119
|
+
```
|
120
|
+
|
121
|
+
|
122
|
+
## XML attributes
|
123
|
+
|
124
|
+
Adding XML attributes is rather ugly, but it can be done by specifying an additional
|
125
|
+
Hash stored under the`:attributes!` key.
|
126
|
+
|
127
|
+
``` ruby
|
128
|
+
Gyoku.xml(:person => "Eve", :attributes! => { :person => { :id => 1 } })
|
129
|
+
# => "<person id=\"1\">Eve</person>"
|
130
|
+
```
|
131
|
+
|
132
|
+
## Explicit XML Attributes
|
133
|
+
|
134
|
+
In addition to using the `:attributes!` key, you may also specify attributes through keys beginning with an "@" sign.
|
135
|
+
Since you'll need to set the attribute within the hash containing the node's contents, a `:content!` key can be used
|
136
|
+
to explicity set the content of the node. The `:content!` value may be a String, Hash, or Array.
|
137
|
+
|
138
|
+
This is particularly useful for self-closing tags.
|
139
|
+
|
140
|
+
**Using :attributes!**
|
141
|
+
|
142
|
+
``` ruby
|
143
|
+
Gyoku.xml(
|
144
|
+
"foo/" => "",
|
145
|
+
:attributes! => {
|
146
|
+
"foo/" => {
|
147
|
+
"bar" => "1",
|
148
|
+
"biz" => "2",
|
149
|
+
"baz" => "3"
|
150
|
+
}
|
151
|
+
}
|
152
|
+
)
|
153
|
+
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"
|
154
|
+
```
|
155
|
+
|
156
|
+
**Using "@" keys and ":content!"**
|
157
|
+
|
158
|
+
``` ruby
|
159
|
+
Gyoku.xml(
|
160
|
+
"foo/" => {
|
161
|
+
:@bar => "1",
|
162
|
+
:@biz => "2",
|
163
|
+
:@baz => "3",
|
164
|
+
:content! => ""
|
165
|
+
})
|
166
|
+
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"
|
167
|
+
```
|
168
|
+
|
169
|
+
**Example using "@" to get Array of parent tags each with @attributes & :content!**
|
170
|
+
|
171
|
+
``` ruby
|
172
|
+
Gyoku.xml(
|
173
|
+
"foo" => [
|
174
|
+
{:@name => "bar", :content! => 'gyoku'}
|
175
|
+
{:@name => "baz", :@some => "attr", :content! => 'rocks!'}
|
176
|
+
])
|
177
|
+
# => "<foo name=\"bar\">gyoku</foo><foo name=\"baz\" some=\"attr\">rocks!</foo>"
|
178
|
+
```
|
179
|
+
|
180
|
+
Unwrapping Arrays. You can specify an optional `unwrap` argument to modify the default Array
|
181
|
+
behavior. `unwrap` accepts a boolean flag (false by default) or an Array whitelist of keys to unwrap.
|
182
|
+
``` ruby
|
183
|
+
# Default Array behavior
|
184
|
+
Gyoku.xml({
|
185
|
+
"foo" => [
|
186
|
+
{:is => 'great' },
|
187
|
+
{:is => 'awesome'}
|
188
|
+
]
|
189
|
+
})
|
190
|
+
# => "<foo><is>great</is></foo><foo><is>awesome</is></foo>"
|
191
|
+
|
192
|
+
# Unwrap Array behavior
|
193
|
+
Gyoku.xml({
|
194
|
+
"foo" => [
|
195
|
+
{:is => 'great' },
|
196
|
+
{:is => 'awesome'}
|
197
|
+
]
|
198
|
+
}, unwrap: true)
|
199
|
+
# => "<foo><is>great</is><is>awesome</is></foo>"
|
200
|
+
|
201
|
+
# Unwrap Array, whitelist.
|
202
|
+
# foo is not unwrapped, bar is.
|
203
|
+
Gyoku.xml({
|
204
|
+
"foo" => [
|
205
|
+
{:is => 'great' },
|
206
|
+
{:is => 'awesome'}
|
207
|
+
],
|
208
|
+
"bar" => [
|
209
|
+
{:is => 'rad' },
|
210
|
+
{:is => 'cool'}
|
211
|
+
]
|
212
|
+
}, unwrap: [:bar])
|
213
|
+
# => "<foo><is>great</is></foo><foo><is>awesome</is></foo><bar><is>rad</is><is>cool</is></bar>"
|
214
|
+
```
|
215
|
+
|
216
|
+
Naturally, it would ignore :content! if tag is self-closing:
|
217
|
+
|
218
|
+
``` ruby
|
219
|
+
Gyoku.xml(
|
220
|
+
"foo/" => [
|
221
|
+
{:@name => "bar", :content! => 'gyoku'}
|
222
|
+
{:@name => "baz", :@some => "attr", :content! => 'rocks!'}
|
223
|
+
])
|
224
|
+
# => "<foo name=\"bar\"/><foo name=\"baz\" some=\"attr\"/>"
|
225
|
+
```
|
226
|
+
|
227
|
+
This seems a bit more explicit with the attributes rather than having to maintain a hash of attributes.
|
228
|
+
|
229
|
+
For backward compatibility, `:attributes!` will still work. However, "@" keys will override `:attributes!` keys
|
230
|
+
if there is a conflict.
|
231
|
+
|
232
|
+
``` ruby
|
233
|
+
Gyoku.xml(:person => {:content! => "Adam", :@id! => 0})
|
234
|
+
# => "<person id=\"0\">Adam</person>"
|
235
|
+
```
|
236
|
+
|
237
|
+
**Example with ":content!", :attributes! and "@" keys**
|
238
|
+
|
239
|
+
``` ruby
|
240
|
+
Gyoku.xml({
|
241
|
+
:subtitle => {
|
242
|
+
:@lang => "en",
|
243
|
+
:content! => "It's Godzilla!"
|
244
|
+
},
|
245
|
+
:attributes! => { :subtitle => { "lang" => "jp" } }
|
246
|
+
}
|
247
|
+
# => "<subtitle lang=\"en\">It's Godzilla!</subtitle>"
|
248
|
+
```
|
249
|
+
|
250
|
+
The example above shows an example of how you can use all three at the same time.
|
251
|
+
|
252
|
+
Notice that we have the attribute "lang" defined twice.
|
253
|
+
The `@lang` value takes precedence over the `:attribute![:subtitle]["lang"]` value.
|
254
|
+
|
255
|
+
## Pretty Print
|
256
|
+
|
257
|
+
You can prettify the output XML to make it more readable. Use these options:
|
258
|
+
* `pretty_print` – controls pretty mode (default: `false`)
|
259
|
+
* `indent` – specifies indentation in spaces (default: `2`)
|
260
|
+
* `compact` – controls compact mode (default: `true`)
|
261
|
+
|
262
|
+
**This feature is not available for XML documents generated from arrays with unwrap option set to false as such documents are not valid**
|
263
|
+
|
264
|
+
**Examples**
|
265
|
+
|
266
|
+
``` ruby
|
267
|
+
puts Gyoku.xml({user: { name: 'John', job: { title: 'Programmer' }, :@status => 'active' }}, pretty_print: true)
|
268
|
+
#<user status='active'>
|
269
|
+
# <name>John</name>
|
270
|
+
# <job>
|
271
|
+
# <title>Programmer</title>
|
272
|
+
# </job>
|
273
|
+
#</user>
|
274
|
+
```
|
275
|
+
|
276
|
+
``` ruby
|
277
|
+
puts Gyoku.xml({user: { name: 'John', job: { title: 'Programmer' }, :@status => 'active' }}, pretty_print: true, indent: 4)
|
278
|
+
#<user status='active'>
|
279
|
+
# <name>John</name>
|
280
|
+
# <job>
|
281
|
+
# <title>Programmer</title>
|
282
|
+
# </job>
|
283
|
+
#</user>
|
284
|
+
```
|
285
|
+
|
286
|
+
``` ruby
|
287
|
+
puts Gyoku.xml({user: { name: 'John', job: { title: 'Programmer' }, :@status => 'active' }}, pretty_print: true, compact: false)
|
288
|
+
#<user status='active'>
|
289
|
+
# <name>
|
290
|
+
# John
|
291
|
+
# </name>
|
292
|
+
# <job>
|
293
|
+
# <title>
|
294
|
+
# Programmer
|
295
|
+
# </title>
|
296
|
+
# </job>
|
297
|
+
#</user>
|
298
|
+
```
|
299
|
+
|
300
|
+
**Generate XML from an array with `unwrap` option set to `true`**
|
301
|
+
``` ruby
|
302
|
+
puts Gyoku::Array.to_xml(["john", "jane"], "user", true, {}, pretty_print: true, unwrap: true)
|
303
|
+
#<user>
|
304
|
+
# <user>john</user>
|
305
|
+
# <user>jane</user>
|
306
|
+
#</user>
|
307
|
+
```
|
308
|
+
|
309
|
+
**Generate XML from an array with `unwrap` option unset (`false` by default)**
|
310
|
+
``` ruby
|
311
|
+
puts Gyoku::Array.to_xml(["john", "jane"], "user", true, {}, pretty_print: true)
|
312
|
+
#<user>john</user><user>jane</user>
|
313
|
+
```
|
data/Rakefile
ADDED
data/gyoku.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "gyoku/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "chef-gyoku"
|
6
|
+
s.version = Gyoku::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = "Daniel Harrington"
|
9
|
+
s.email = "me@rubiii.com"
|
10
|
+
s.homepage = "https://github.com/savonrb/#{s.name}"
|
11
|
+
s.summary = "Translates Ruby Hashes to XML"
|
12
|
+
s.description = "Gyoku translates Ruby Hashes to XML"
|
13
|
+
s.required_ruby_version = ">= 3.0"
|
14
|
+
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.add_dependency "builder", ">= 2.1.2"
|
18
|
+
s.add_dependency "rexml", "~> 3.3"
|
19
|
+
|
20
|
+
s.add_development_dependency "rake"
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "standard"
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|