ukoyg 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +192 -0
- data/Rakefile +12 -0
- data/lib/ukoyg.rb +16 -0
- data/lib/ukoyg/array.rb +89 -0
- data/lib/ukoyg/hash.rb +85 -0
- data/lib/ukoyg/version.rb +3 -0
- data/lib/ukoyg/xml_key.rb +69 -0
- data/lib/ukoyg/xml_value.rb +38 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3ff90c95ea012b4f4d758ecd2a0bd412f6a202f9
|
4
|
+
data.tar.gz: e46bfe2880291b6edeff7ea2dd186989c98a7a99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de9f264a8abd31fd1f66d371bc80307db69d233e5bf81ac08adf8c1d72f53ad2cd4103aa986a805e30445c07263f8f716dbdb06bfd7599a21b540550d8fe4b77
|
7
|
+
data.tar.gz: c3614922b185dbf2d2a1d8beee510ef35f4dd8a0050d5c6df077ac0b0dd72b55c2afaa34ed0e4e5c8450963dc73a36f0bd88595766081909ae83af0f961a4ebe
|
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,192 @@
|
|
1
|
+
# Ukoyg
|
2
|
+
|
3
|
+
Ukoyg translates Ruby Hashes to XML.
|
4
|
+
|
5
|
+
``` ruby
|
6
|
+
Ukoyg.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://secure.travis-ci.org/savonrb/ukoyg.png?branch=master)](http://travis-ci.org/savonrb/ukoyg)
|
11
|
+
[![Gem Version](https://badge.fury.io/rb/ukoyg.png)](http://badge.fury.io/rb/ukoyg)
|
12
|
+
[![Code Climate](https://codeclimate.com/github/savonrb/ukoyg.png)](https://codeclimate.com/github/savonrb/ukoyg)
|
13
|
+
[![Coverage Status](https://coveralls.io/repos/savonrb/ukoyg/badge.png?branch=master)](https://coveralls.io/r/savonrb/ukoyg)
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Ukoyg is available through [Rubygems](http://rubygems.org/gems/ukoyg) and can be installed via:
|
19
|
+
|
20
|
+
``` bash
|
21
|
+
$ gem install ukoyg
|
22
|
+
```
|
23
|
+
|
24
|
+
or add it to your Gemfile like this:
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
gem 'ukoyg', '~> 1.0'
|
28
|
+
```
|
29
|
+
|
30
|
+
|
31
|
+
## Hash keys
|
32
|
+
|
33
|
+
Hash key Symbols are converted to lowerCamelCase Strings.
|
34
|
+
|
35
|
+
``` ruby
|
36
|
+
Ukoyg.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
|
+
Ukoyg.xml({ :camel_case => "key" }, { :key_converter => :camelcase })
|
45
|
+
# => "<CamelCase>key</CamelCase>"
|
46
|
+
```
|
47
|
+
|
48
|
+
Hash key Strings are not converted and may contain namespaces.
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
Ukoyg.xml("XML" => "key")
|
52
|
+
# => "<XML>key</XML>"
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
## Hash values
|
57
|
+
|
58
|
+
* DateTime objects are converted to xs:dateTime Strings
|
59
|
+
* Objects responding to :to_datetime (except Strings) are converted to xs:dateTime Strings
|
60
|
+
* TrueClass and FalseClass objects are converted to "true" and "false" Strings
|
61
|
+
* NilClass objects are converted to xsi:nil tags
|
62
|
+
* These conventions are also applied to the return value of objects responding to :call
|
63
|
+
* All other objects are converted to Strings using :to_s
|
64
|
+
|
65
|
+
|
66
|
+
## Special characters
|
67
|
+
|
68
|
+
Ukoyg escapes special characters unless the Hash key ends with an exclamation mark.
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
Ukoyg.xml(:escaped => "<tag />", :not_escaped! => "<tag />")
|
72
|
+
# => "<escaped><tag /></escaped><notEscaped><tag /></notEscaped>"
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
## Self-closing tags
|
77
|
+
|
78
|
+
Hash Keys ending with a forward slash create self-closing tags.
|
79
|
+
|
80
|
+
``` ruby
|
81
|
+
Ukoyg.xml(:"self_closing/" => "", "selfClosing/" => nil)
|
82
|
+
# => "<selfClosing/><selfClosing/>"
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
## Sort XML tags
|
87
|
+
|
88
|
+
In case you need the XML tags to be in a specific order, you can specify the order
|
89
|
+
through an additional Array stored under the `:order!` key.
|
90
|
+
|
91
|
+
``` ruby
|
92
|
+
Ukoyg.xml(:name => "Eve", :id => 1, :order! => [:id, :name])
|
93
|
+
# => "<id>1</id><name>Eve</name>"
|
94
|
+
```
|
95
|
+
|
96
|
+
|
97
|
+
## XML attributes
|
98
|
+
|
99
|
+
Adding XML attributes is rather ugly, but it can be done by specifying an additional
|
100
|
+
Hash stored under the`:attributes!` key.
|
101
|
+
|
102
|
+
``` ruby
|
103
|
+
Ukoyg.xml(:person => "Eve", :attributes! => { :person => { :id => 1 } })
|
104
|
+
# => "<person id=\"1\">Eve</person>"
|
105
|
+
```
|
106
|
+
|
107
|
+
## Explicit XML Attributes
|
108
|
+
|
109
|
+
In addition to using the `:attributes!` key, you may also specify attributes through keys beginning with an "@" sign.
|
110
|
+
Since you'll need to set the attribute within the hash containing the node's contents, a `:content!` key can be used
|
111
|
+
to explicity set the content of the node. The `:content!` value may be a String, Hash, or Array.
|
112
|
+
|
113
|
+
This is particularly useful for self-closing tags.
|
114
|
+
|
115
|
+
**Using :attributes!**
|
116
|
+
|
117
|
+
``` ruby
|
118
|
+
Ukoyg.xml(
|
119
|
+
"foo/" => "",
|
120
|
+
:attributes! => {
|
121
|
+
"foo/" => {
|
122
|
+
"bar" => "1",
|
123
|
+
"biz" => "2",
|
124
|
+
"baz" => "3"
|
125
|
+
}
|
126
|
+
}
|
127
|
+
)
|
128
|
+
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"
|
129
|
+
```
|
130
|
+
|
131
|
+
**Using "@" keys and ":content!"**
|
132
|
+
|
133
|
+
``` ruby
|
134
|
+
Ukoyg.xml(
|
135
|
+
"foo/" => {
|
136
|
+
:@bar => "1",
|
137
|
+
:@biz => "2",
|
138
|
+
:@baz => "3",
|
139
|
+
:content! => ""
|
140
|
+
})
|
141
|
+
# => "<foo baz=\"3\" bar=\"1\" biz=\"2\"/>"
|
142
|
+
```
|
143
|
+
|
144
|
+
**Example using "@" to get Array of parent tags each with @attributes & :content!**
|
145
|
+
|
146
|
+
``` ruby
|
147
|
+
Ukoyg.xml(
|
148
|
+
"foo" => [
|
149
|
+
{:@name => "bar", :content! => 'ukoyg'}
|
150
|
+
{:@name => "baz", :@some => "attr", :content! => 'rocks!'}
|
151
|
+
])
|
152
|
+
# => "<foo name=\"bar\">ukoyg</foo><foo name=\"baz\" some=\"attr\">rocks!</foo>"
|
153
|
+
```
|
154
|
+
|
155
|
+
Naturally, it would ignore :content! if tag is self-closing:
|
156
|
+
|
157
|
+
``` ruby
|
158
|
+
Ukoyg.xml(
|
159
|
+
"foo/" => [
|
160
|
+
{:@name => "bar", :content! => 'ukoyg'}
|
161
|
+
{:@name => "baz", :@some => "attr", :content! => 'rocks!'}
|
162
|
+
])
|
163
|
+
# => "<foo name=\"bar\"/><foo name=\"baz\" some=\"attr\"/>"
|
164
|
+
```
|
165
|
+
|
166
|
+
This seems a bit more explicit with the attributes rather than having to maintain a hash of attributes.
|
167
|
+
|
168
|
+
For backward compatibility, `:attributes!` will still work. However, "@" keys will override `:attributes!` keys
|
169
|
+
if there is a conflict.
|
170
|
+
|
171
|
+
``` ruby
|
172
|
+
Ukoyg.xml(:person => {:content! => "Adam", :@id! => 0})
|
173
|
+
# => "<person id=\"0\">Adam</person>"
|
174
|
+
```
|
175
|
+
|
176
|
+
**Example with ":content!", :attributes! and "@" keys**
|
177
|
+
|
178
|
+
``` ruby
|
179
|
+
Ukoyg.xml({
|
180
|
+
:subtitle => {
|
181
|
+
:@lang => "en",
|
182
|
+
:content! => "It's Godzilla!"
|
183
|
+
},
|
184
|
+
:attributes! => { :subtitle => { "lang" => "jp" } }
|
185
|
+
}
|
186
|
+
# => "<subtitle lang=\"en\">It's Godzilla!</subtitle>"
|
187
|
+
```
|
188
|
+
|
189
|
+
The example above shows an example of how you can use all three at the same time.
|
190
|
+
|
191
|
+
Notice that we have the attribute "lang" defined twice.
|
192
|
+
The `@lang` value takes precedence over the `:attribute![:subtitle]["lang"]` value.
|
data/Rakefile
ADDED
data/lib/ukoyg.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require "ukoyg/version"
|
2
|
+
require "ukoyg/hash"
|
3
|
+
|
4
|
+
module Ukoyg
|
5
|
+
|
6
|
+
# Converts a given Hash +key+ with +options+ into an XML tag.
|
7
|
+
def self.xml_tag(key, options = {})
|
8
|
+
XMLKey.create(key, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Translates a given +hash+ with +options+ to XML.
|
12
|
+
def self.xml(hash, options = {})
|
13
|
+
Hash.to_xml hash.dup, options
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/lib/ukoyg/array.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require "builder"
|
2
|
+
|
3
|
+
require "ukoyg/hash"
|
4
|
+
require "ukoyg/xml_value"
|
5
|
+
|
6
|
+
module Ukoyg
|
7
|
+
class Array
|
8
|
+
|
9
|
+
NESTED_ELEMENT_NAME = "element"
|
10
|
+
|
11
|
+
# Translates a given +array+ to XML. Accepts the XML +key+ to add the elements to,
|
12
|
+
# whether to +escape_xml+ and an optional Hash of +attributes+.
|
13
|
+
def self.to_xml(array, key, escape_xml = true, attributes = {}, options = {})
|
14
|
+
|
15
|
+
self_closing = options.delete(:self_closing)
|
16
|
+
unwrap = options[:unwrap] || false
|
17
|
+
|
18
|
+
iterate_with_xml array, key, attributes, options do |xml, item, attrs, index|
|
19
|
+
if self_closing
|
20
|
+
xml.tag!(key, attrs)
|
21
|
+
else
|
22
|
+
case item
|
23
|
+
when ::Hash then
|
24
|
+
if unwrap
|
25
|
+
xml << Hash.to_xml(item, options)
|
26
|
+
else
|
27
|
+
xml.tag!(key, attrs) { xml << Hash.to_xml(item, options) }
|
28
|
+
end
|
29
|
+
when ::Array then
|
30
|
+
xml.tag!(key, attrs) { xml << Array.to_xml(item, NESTED_ELEMENT_NAME) }
|
31
|
+
when NilClass then
|
32
|
+
xml.tag!(key, "xsi:nil" => "true")
|
33
|
+
else
|
34
|
+
xml.tag!(key, attrs) { xml << XMLValue.create(item, escape_xml) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Iterates over a given +array+ with a Hash of +attributes+ and yields a builder +xml+
|
43
|
+
# instance, the current +item+, any XML +attributes+ and the current +index+.
|
44
|
+
def self.iterate_with_xml(array, key, attributes, options, &block)
|
45
|
+
|
46
|
+
xml = Builder::XmlMarkup.new
|
47
|
+
unwrap = options[:unwrap] || false
|
48
|
+
|
49
|
+
if (unwrap)
|
50
|
+
xml.tag!(key) { iterate_array(xml, array, attributes, &block) }
|
51
|
+
else
|
52
|
+
iterate_array(xml, array, attributes, &block)
|
53
|
+
end
|
54
|
+
|
55
|
+
xml.target!
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# Iterates over a given +array+ with a Hash of +attributes+ and yields a builder +xml+
|
60
|
+
# instance, the current +item+, any XML +attributes+ and the current +index+.
|
61
|
+
def self.iterate_array(xml, array, attributes, &block)
|
62
|
+
array.each_with_index do |item, index|
|
63
|
+
if item.respond_to?(:keys)
|
64
|
+
attrs = item.reduce({}) do |st, v|
|
65
|
+
k = v[0].to_s
|
66
|
+
st[k[1..-1]] = v[1].to_s if k =~ /^@/
|
67
|
+
st
|
68
|
+
end
|
69
|
+
else
|
70
|
+
attrs = {}
|
71
|
+
end
|
72
|
+
yield xml, item, tag_attributes(attributes, index).merge(attrs), index
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
# Takes a Hash of +attributes+ and the +index+ for which to return attributes
|
78
|
+
# for duplicate tags.
|
79
|
+
def self.tag_attributes(attributes, index)
|
80
|
+
return {} if attributes.empty?
|
81
|
+
|
82
|
+
attributes.inject({}) do |hash, (key, value)|
|
83
|
+
value = value[index] if value.kind_of? ::Array
|
84
|
+
value ? hash.merge(key => value) : hash
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
data/lib/ukoyg/hash.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require "builder"
|
2
|
+
|
3
|
+
require "ukoyg/array"
|
4
|
+
require "ukoyg/xml_key"
|
5
|
+
require "ukoyg/xml_value"
|
6
|
+
|
7
|
+
module Ukoyg
|
8
|
+
class Hash
|
9
|
+
|
10
|
+
# Translates a given +hash+ with +options+ to XML.
|
11
|
+
def self.to_xml(hash, options = {})
|
12
|
+
iterate_with_xml hash do |xml, key, value, attributes|
|
13
|
+
self_closing = key.to_s[-1, 1] == "/"
|
14
|
+
escape_xml = key.to_s[-1, 1] != "!"
|
15
|
+
xml_key = XMLKey.create key, options
|
16
|
+
|
17
|
+
case
|
18
|
+
when :content! === key then xml << XMLValue.create(value, escape_xml)
|
19
|
+
when ::Array === value then xml << Array.to_xml(value, xml_key, escape_xml, attributes, options.merge(:self_closing => self_closing))
|
20
|
+
when ::Hash === value then xml.tag!(xml_key, attributes) { xml << Hash.to_xml(value, options) }
|
21
|
+
when self_closing then xml.tag!(xml_key, attributes)
|
22
|
+
when NilClass === value then xml.tag!(xml_key, "xsi:nil" => "true")
|
23
|
+
else xml.tag!(xml_key, attributes) { xml << XMLValue.create(value, escape_xml) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Iterates over a given +hash+ and yields a builder +xml+ instance, the current
|
31
|
+
# Hash +key+ and any XML +attributes+.
|
32
|
+
#
|
33
|
+
# Keys beginning with "@" are treated as explicit attributes for their container.
|
34
|
+
# You can use both :attributes! and "@" keys to specify attributes.
|
35
|
+
# In the event of a conflict, the "@" key takes precedence.
|
36
|
+
def self.iterate_with_xml(hash)
|
37
|
+
xml = Builder::XmlMarkup.new
|
38
|
+
attributes = hash[:attributes!] || {}
|
39
|
+
hash_without_attributes = hash.reject { |key, value| key == :attributes! }
|
40
|
+
|
41
|
+
order(hash_without_attributes).each do |key|
|
42
|
+
node_attr = attributes[key] || {}
|
43
|
+
# node_attr must be kind of ActiveSupport::HashWithIndifferentAccess
|
44
|
+
node_attr = ::Hash[node_attr.map { |k,v| [k.to_s, v] }]
|
45
|
+
node_value = hash[key].respond_to?(:keys) ? hash[key].clone : hash[key]
|
46
|
+
|
47
|
+
if node_value.respond_to?(:keys)
|
48
|
+
explicit_keys = node_value.keys.select{|k| k.to_s =~ /^@/ }
|
49
|
+
explicit_attr = {}
|
50
|
+
explicit_keys.each{|k| explicit_attr[k.to_s[1..-1]] = node_value[k]}
|
51
|
+
node_attr.merge!(explicit_attr)
|
52
|
+
explicit_keys.each{|k| node_value.delete(k) }
|
53
|
+
|
54
|
+
tmp_node_value = node_value.delete(:content!)
|
55
|
+
node_value = tmp_node_value unless tmp_node_value.nil?
|
56
|
+
node_value = "" if node_value.respond_to?(:empty?) && node_value.empty?
|
57
|
+
end
|
58
|
+
|
59
|
+
yield xml, key, node_value, node_attr
|
60
|
+
end
|
61
|
+
|
62
|
+
xml.target!
|
63
|
+
end
|
64
|
+
|
65
|
+
# Deletes and returns an Array of keys stored under the :order! key of a given +hash+.
|
66
|
+
# Defaults to return the actual keys of the Hash if no :order! key could be found.
|
67
|
+
# Raises an ArgumentError in case the :order! Array does not match the Hash keys.
|
68
|
+
def self.order(hash)
|
69
|
+
order = hash[:order!] || hash.delete('order!')
|
70
|
+
hash_without_order = hash.reject { |key, value| key == :order! }
|
71
|
+
order = hash_without_order.keys unless order.kind_of? ::Array
|
72
|
+
|
73
|
+
# Ignore Explicit Attributes
|
74
|
+
orderable = order.delete_if{|k| k.to_s =~ /^@/ }
|
75
|
+
hashable = hash_without_order.keys.select{|k| !(k.to_s =~ /^@/) }
|
76
|
+
|
77
|
+
missing, spurious = hashable - orderable, orderable - hashable
|
78
|
+
raise ArgumentError, "Missing elements in :order! #{missing.inspect}" unless missing.empty?
|
79
|
+
raise ArgumentError, "Spurious elements in :order! #{spurious.inspect}" unless spurious.empty?
|
80
|
+
|
81
|
+
order
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Ukoyg
|
2
|
+
module XMLKey
|
3
|
+
class << self
|
4
|
+
|
5
|
+
CAMELCASE = lambda { |key| key.gsub(/\/(.?)/) { |m| "::#{m.split('').last.upcase}" }.gsub(/(?:^|_)(.)/) { |m| m.split('').last.upcase } }
|
6
|
+
LOWER_CAMELCASE = lambda { |key| key[0].chr.downcase + CAMELCASE.call(key)[1..-1] }
|
7
|
+
UPCASE = lambda { |key| key.upcase }
|
8
|
+
|
9
|
+
FORMULAS = {
|
10
|
+
:lower_camelcase => lambda { |key| LOWER_CAMELCASE.call(key) },
|
11
|
+
:camelcase => lambda { |key| CAMELCASE.call(key) },
|
12
|
+
:upcase => lambda { |key| UPCASE.call(key) },
|
13
|
+
:none => lambda { |key| key }
|
14
|
+
}
|
15
|
+
|
16
|
+
# Converts a given +object+ with +options+ to an XML key.
|
17
|
+
def create(key, options = {})
|
18
|
+
xml_key = chop_special_characters key.to_s
|
19
|
+
|
20
|
+
if unqualified = unqualify?(xml_key)
|
21
|
+
xml_key = xml_key.split(":").last
|
22
|
+
end
|
23
|
+
|
24
|
+
xml_key = key_converter(options, xml_key).call(xml_key) if Symbol === key
|
25
|
+
|
26
|
+
if !unqualified && qualify?(options) && !xml_key.include?(":")
|
27
|
+
xml_key = "#{options[:namespace]}:#{xml_key}"
|
28
|
+
end
|
29
|
+
|
30
|
+
xml_key
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Returns the formula for converting Symbol keys.
|
36
|
+
def key_converter(options, xml_key)
|
37
|
+
return options[:key_converter] if options[:key_converter].is_a? Proc
|
38
|
+
|
39
|
+
defined_key = options[:key_to_convert]
|
40
|
+
if (defined_key != nil) && (defined_key == xml_key)
|
41
|
+
key_converter = options[:key_converter]
|
42
|
+
elsif defined_key != nil
|
43
|
+
key_converter = :lower_camelcase
|
44
|
+
elsif (options[:except] == xml_key)
|
45
|
+
key_converter = :lower_camelcase
|
46
|
+
else
|
47
|
+
key_converter = options[:key_converter] || :lower_camelcase
|
48
|
+
end
|
49
|
+
FORMULAS[key_converter]
|
50
|
+
end
|
51
|
+
|
52
|
+
# Chops special characters from the end of a given +string+.
|
53
|
+
def chop_special_characters(string)
|
54
|
+
["!", "/"].include?(string[-1, 1]) ? string.chop : string
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns whether to remove the namespace from a given +key+.
|
58
|
+
def unqualify?(key)
|
59
|
+
key[0, 1] == ":"
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns whether to namespace all keys (elementFormDefault).
|
63
|
+
def qualify?(options)
|
64
|
+
options[:element_form_default] == :qualified && options[:namespace]
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "cgi"
|
2
|
+
require "date"
|
3
|
+
|
4
|
+
module Ukoyg
|
5
|
+
module XMLValue
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# xs:date format
|
9
|
+
XS_DATE_FORMAT = "%Y-%m-%d"
|
10
|
+
|
11
|
+
# xs:time format
|
12
|
+
XS_TIME_FORMAT = "%H:%M:%S"
|
13
|
+
|
14
|
+
# xs:dateTime format
|
15
|
+
XS_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S%Z"
|
16
|
+
|
17
|
+
# Converts a given +object+ to an XML value.
|
18
|
+
def create(object, escape_xml = true)
|
19
|
+
if Time === object
|
20
|
+
object.strftime XS_TIME_FORMAT
|
21
|
+
elsif DateTime === object
|
22
|
+
object.strftime XS_DATETIME_FORMAT
|
23
|
+
elsif Date === object
|
24
|
+
object.strftime XS_DATE_FORMAT
|
25
|
+
elsif String === object
|
26
|
+
escape_xml ? CGI.escapeHTML(object) : object
|
27
|
+
elsif object.respond_to?(:to_datetime)
|
28
|
+
create object.to_datetime
|
29
|
+
elsif object.respond_to?(:call)
|
30
|
+
create object.call
|
31
|
+
else
|
32
|
+
object.to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ukoyg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Harrington
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: builder
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Ukoyg translates Ruby Hashes to XML
|
56
|
+
email: me@rubiii.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- MIT-LICENSE
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/ukoyg.rb
|
65
|
+
- lib/ukoyg/array.rb
|
66
|
+
- lib/ukoyg/hash.rb
|
67
|
+
- lib/ukoyg/version.rb
|
68
|
+
- lib/ukoyg/xml_key.rb
|
69
|
+
- lib/ukoyg/xml_value.rb
|
70
|
+
homepage: https://github.com/savonrb/ukoyg
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project: ukoyg
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Translates Ruby Hashes to XML
|
94
|
+
test_files: []
|