xml-mapping_extensions 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +6 -1
- data/README.md +86 -1
- data/example.rb +55 -0
- data/lib/xml/mapping_extensions/date_node.rb +14 -1
- data/lib/xml/mapping_extensions/version.rb +1 -1
- data/spec/unit/xml/mapping_extensions/date_node_spec.rb +15 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58fe30758a9f3eee417329e21e489a1fcb05cee3
|
4
|
+
data.tar.gz: 5ce03257e351534e12cd07bbe7c0ccfa6e4e5689
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 470de71e5c996ec025d632218bb7f4a626f22362bcf345dd079b8107a6f3053a9868de32efd7b9283c0cf8ef660f3a4010a2b9fb8d01b40c7066b28fcfd114c6
|
7
|
+
data.tar.gz: 0747764d15df2ce7c9441985b6417f071e718c955e69959651295ea7a82302ad127612d71f92a8e0ee2f5c7390613b0dfea55f9bc9c77fb97cbe364ba76ffc66
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
# XML::MappingExtensions
|
1
|
+
# XML::MappingExtensions
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/dmolesUC3/xml-mapping_extensions.png?branch=master)](https://travis-ci.org/dmolesUC3/xml-mapping_extensions)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/dmolesUC3/xml-mapping_extensions.png)](https://codeclimate.com/github/dmolesUC3/xml-mapping_extensions)
|
5
|
+
[![Inline docs](http://inch-ci.org/github/dmolesUC3/xml-mapping_extensions.png)](http://inch-ci.org/github/dmolesUC3/xml-mapping_extensions)
|
3
6
|
|
4
7
|
Additional mapping nodes and other utility classes for working with
|
5
8
|
[XML::Mapping](http://multi-io.github.io/xml-mapping/).
|
@@ -24,3 +27,85 @@ to be registered with the XML mapping engine.
|
|
24
27
|
- `TimeNode`: ISO 8601 strings to `Time` objects
|
25
28
|
- `UriNode`: maps URI strings to `URI` objects
|
26
29
|
- `MimeTypeNode`: maps MIME type strings to `MIME::Type` objects
|
30
|
+
|
31
|
+
### Example
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'xml/mapping_extensions'
|
35
|
+
require 'rexml/document'
|
36
|
+
|
37
|
+
class MyElem
|
38
|
+
include ::XML::Mapping
|
39
|
+
|
40
|
+
root_element_name 'my_elem'
|
41
|
+
|
42
|
+
date_node :plain_date, 'plain_date'
|
43
|
+
date_node :zulu_date, 'zulu_date', zulu: true
|
44
|
+
time_node :time, 'time'
|
45
|
+
uri_node :uri, 'uri'
|
46
|
+
mime_type_node :mime_type, 'mime_type'
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
#### Reading XML:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
xml_str = "<my_elem>
|
54
|
+
<plain_date>1999-12-31</plain_date>
|
55
|
+
<zulu_date>2000-01-01Z</zulu_date>
|
56
|
+
<time>2000-01-01T02:34:56Z</time>
|
57
|
+
<uri>http://example.org</uri>
|
58
|
+
<mime_type>text/plain</mime_type>
|
59
|
+
</my_elem>"
|
60
|
+
|
61
|
+
xml_doc = REXML::Document.new(xml_str)
|
62
|
+
xml_elem = xml_doc.root
|
63
|
+
|
64
|
+
elem = MyElem.load_from_xml(xml_elem)
|
65
|
+
|
66
|
+
puts elem.plain_date.inspect
|
67
|
+
puts elem.zulu_date.inspect
|
68
|
+
puts elem.time.inspect
|
69
|
+
puts elem.uri.inspect
|
70
|
+
puts elem.mime_type.inspect
|
71
|
+
```
|
72
|
+
|
73
|
+
Outputs
|
74
|
+
|
75
|
+
```
|
76
|
+
#<Date: 1999-12-31 ((2451544j,0s,0n),+0s,2299161j)>
|
77
|
+
#<Date: 2000-01-01 ((2451545j,0s,0n),+0s,2299161j)>
|
78
|
+
2000-01-01 02:34:56 UTC
|
79
|
+
#<URI::HTTP http://example.org>
|
80
|
+
#<MIME::Type:0x007f864bdc4f78 @friendly={"en"=>"Text File"}, @system=nil, @obsolete=false, @registered=true, @use_instead=nil, @signature=false, @content_type="text/plain", @raw_media_type="text", @raw_sub_type="plain", @simplified="text/plain", @i18n_key="text.plain", @media_type="text", @sub_type="plain", @docs=[], @encoding="quoted-printable", @extensions=["txt", "asc", "c", "cc", "h", "hh", "cpp", "hpp", "dat", "hlp", "conf", "def", "doc", "in", "list", "log", "markdown", "md", "rst", "text", "textile"], @references=["IANA", "RFC2046", "RFC3676", "RFC5147"], @xrefs={"rfc"=>["rfc2046", "rfc3676", "rfc5147"]}>
|
81
|
+
```
|
82
|
+
|
83
|
+
#### Writing XML:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
elem = MyElem.new
|
87
|
+
elem.plain_date = Date.new(1999, 12, 31)
|
88
|
+
elem.zulu_date = Date.new(2000, 1, 1)
|
89
|
+
elem.time = Time.utc(2000, 1, 1, 2, 34, 56)
|
90
|
+
elem.uri = URI('http://example.org')
|
91
|
+
elem.mime_type = MIME::Types['text/plain'].first
|
92
|
+
|
93
|
+
xml = elem.save_to_xml
|
94
|
+
|
95
|
+
formatter = REXML::Formatters::Pretty.new
|
96
|
+
formatter.compact = true
|
97
|
+
|
98
|
+
puts(formatter.write(xml, ""))
|
99
|
+
```
|
100
|
+
|
101
|
+
Outputs:
|
102
|
+
|
103
|
+
```xml
|
104
|
+
<my_elem>
|
105
|
+
<plain_date>1999-12-31</plain_date>
|
106
|
+
<zulu_date>2000-01-01Z</zulu_date>
|
107
|
+
<time>2000-01-01T02:34:56Z</time>
|
108
|
+
<uri>http://example.org</uri>
|
109
|
+
<mime_type>text/plain</mime_type>
|
110
|
+
</my_elem>
|
111
|
+
```
|
data/example.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'xml/mapping_extensions'
|
4
|
+
require 'rexml/document'
|
5
|
+
|
6
|
+
class MyElem
|
7
|
+
include ::XML::Mapping
|
8
|
+
|
9
|
+
root_element_name 'my_elem'
|
10
|
+
|
11
|
+
date_node :plain_date, 'plain_date'
|
12
|
+
date_node :zulu_date, 'zulu_date', zulu: true
|
13
|
+
time_node :time, 'time'
|
14
|
+
uri_node :uri, 'uri'
|
15
|
+
mime_type_node :mime_type, 'mime_type'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Reading XML
|
19
|
+
|
20
|
+
xml_str = "<my_elem>
|
21
|
+
<plain_date>1999-12-31</plain_date>
|
22
|
+
<zulu_date>2000-01-01Z</zulu_date>
|
23
|
+
<time>2000-01-01T02:34:56Z</time>
|
24
|
+
<uri>http://example.org</uri>
|
25
|
+
<mime_type>text/plain</mime_type>
|
26
|
+
</my_elem>"
|
27
|
+
|
28
|
+
xml_doc = REXML::Document.new(xml_str)
|
29
|
+
xml_elem = xml_doc.root
|
30
|
+
|
31
|
+
elem = MyElem.load_from_xml(xml_elem)
|
32
|
+
|
33
|
+
puts elem.plain_date.inspect
|
34
|
+
puts elem.zulu_date.inspect
|
35
|
+
puts elem.time.inspect
|
36
|
+
puts elem.uri.inspect
|
37
|
+
puts elem.mime_type.inspect
|
38
|
+
|
39
|
+
# Writing XML
|
40
|
+
|
41
|
+
elem = MyElem.new
|
42
|
+
elem.plain_date = Date.new(1999, 12, 31)
|
43
|
+
elem.zulu_date = Date.new(2000, 1, 1)
|
44
|
+
elem.time = Time.utc(2000, 1, 1, 2, 34, 56)
|
45
|
+
elem.uri = URI('http://example.org')
|
46
|
+
elem.mime_type = MIME::Types['text/plain'].first
|
47
|
+
|
48
|
+
xml = elem.save_to_xml
|
49
|
+
|
50
|
+
formatter = REXML::Formatters::Pretty.new
|
51
|
+
formatter.compact = true
|
52
|
+
|
53
|
+
puts(formatter.write(xml, ""))
|
54
|
+
|
55
|
+
|
@@ -8,6 +8,18 @@ module XML
|
|
8
8
|
# Known limitation: loses time zone info
|
9
9
|
class DateNode < NodeBase
|
10
10
|
|
11
|
+
def initialize(*args)
|
12
|
+
super
|
13
|
+
end
|
14
|
+
|
15
|
+
# Whether date should be output with UTC "Zulu" time
|
16
|
+
# designator ("Z")
|
17
|
+
# @return [Boolean, nil] True if date should be output
|
18
|
+
# with "Z" time designator, false or nil otherwise
|
19
|
+
def zulu
|
20
|
+
@options[:zulu]
|
21
|
+
end
|
22
|
+
|
11
23
|
# param xml_text [String] an XML Schema date
|
12
24
|
# @return [Date] the value as a `Date`
|
13
25
|
def to_value(xml_text)
|
@@ -18,7 +30,8 @@ module XML
|
|
18
30
|
# @return [String] the value as an XML Schema date string, without
|
19
31
|
# time zone information
|
20
32
|
def to_xml_text(value)
|
21
|
-
value.xmlschema
|
33
|
+
text = value.xmlschema
|
34
|
+
zulu ? "#{text}Z" : text
|
22
35
|
end
|
23
36
|
end
|
24
37
|
::XML::Mapping.add_node_class DateNode
|
@@ -5,9 +5,10 @@ module XML
|
|
5
5
|
class DateNodeSpecElem
|
6
6
|
include ::XML::Mapping
|
7
7
|
date_node :date, '@date', default_value: nil
|
8
|
+
date_node :zulu_date, '@zulu_date', default_value: nil, zulu: true
|
8
9
|
|
9
10
|
def self.from_str(date_str)
|
10
|
-
xml_string = "<elem date='#{date_str}'/>"
|
11
|
+
xml_string = "<elem date='#{date_str}' zulu_date='#{date_str}'/>"
|
11
12
|
doc = REXML::Document.new(xml_string)
|
12
13
|
load_from_xml(doc.root)
|
13
14
|
end
|
@@ -25,6 +26,13 @@ module XML
|
|
25
26
|
xml.attributes['date']
|
26
27
|
end
|
27
28
|
|
29
|
+
def to_zulu_text(date)
|
30
|
+
elem = DateNodeSpecElem.new
|
31
|
+
elem.zulu_date = date
|
32
|
+
xml = elem.save_to_xml
|
33
|
+
xml.attributes['zulu_date']
|
34
|
+
end
|
35
|
+
|
28
36
|
it 'parses a date' do
|
29
37
|
actual = to_date('2002-09-24')
|
30
38
|
expected = Date.new(2002, 9, 24)
|
@@ -49,6 +57,12 @@ module XML
|
|
49
57
|
expect(actual).to eq(expected)
|
50
58
|
end
|
51
59
|
|
60
|
+
it 'outputs a UTC "zulu" date (time zone designator "Z")' do
|
61
|
+
expected = '2002-09-24Z'
|
62
|
+
actual = to_zulu_text(Date.new(2002, 9, 24))
|
63
|
+
expect(actual).to eq(expected)
|
64
|
+
end
|
65
|
+
|
52
66
|
end
|
53
67
|
end
|
54
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xml-mapping_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Moles
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mime-types
|
@@ -166,6 +166,7 @@ files:
|
|
166
166
|
- LICENSE.md
|
167
167
|
- README.md
|
168
168
|
- Rakefile
|
169
|
+
- example.rb
|
169
170
|
- lib/xml/mapping_extensions.rb
|
170
171
|
- lib/xml/mapping_extensions/date_node.rb
|
171
172
|
- lib/xml/mapping_extensions/enum_node_base.rb
|
@@ -203,7 +204,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
203
204
|
version: '0'
|
204
205
|
requirements: []
|
205
206
|
rubyforge_project:
|
206
|
-
rubygems_version: 2.4.
|
207
|
+
rubygems_version: 2.4.8
|
207
208
|
signing_key:
|
208
209
|
specification_version: 4
|
209
210
|
summary: Some extensions for for XML::Mapping
|