xml-mapping_extensions 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 50ceab85aed6813c2835984fc59ece8806dcbd31
4
- data.tar.gz: aa7f145312c0a7c7fb97ba40f5620c1ad58b267c
3
+ metadata.gz: 159f916e7e1a374d68857ce63626f37b763312e3
4
+ data.tar.gz: 50ffcf3d3606eec904e7ce9faaecccf90905ac39
5
5
  SHA512:
6
- metadata.gz: c9c052d3e34068555752d3a52a811d9710a4927611df2b7b3102eb29a406fc612f390c038e21510dcc470d1f9b4461749db57968a2585f024a82b9534f49fead
7
- data.tar.gz: 43c32627ebebed41055496cc0807a536549a7acc5553afc8848cde3baf1f0b800e6e1480ea956c2c9e649212091da7d37bb6168cf14e0aa2a7a3d33782e0f343
6
+ metadata.gz: fdd87b9bb6d862300aac1b9bf993b9023f2704c6f788347868eb9eb89cd2a5911f92c321c4bba1af4a8520d3957f80b05678d7e75302b2b9e756bf61600d355a
7
+ data.tar.gz: fbc3c1cc7ba3627c21c6fe6cf2711031620f4638554a1d53d97e46c43547cb86426be36ef46eb4fb7bfbc5f78a1a341937a6342867710cc0a35bd93ad74f58c3
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.4.0 (16 May 2016)
2
+
3
+ - Renamed `NamespacedElement` to `Namespaced` and made it an explicit include rather than auto-injected
4
+ metaprogramming craziness
5
+
1
6
  ## 0.3.7 (13 May 2016)
2
7
 
3
8
  - Made `Namespace#schema_location` optional.
data/README.md CHANGED
@@ -16,8 +16,8 @@ Additional mapping nodes and other utility code for working with
16
16
  - [Example](#example)
17
17
  - [Provided implementations](#provided-implementations)
18
18
  - [Example](#example-1)
19
- - [Reading XML:](#reading-xml)
20
- - [Writing XML:](#writing-xml)
19
+ - [Reading](#reading)
20
+ - [Writing](#writing)
21
21
  - [Namespaces](#namespaces)
22
22
 
23
23
 
@@ -123,7 +123,7 @@ class MyElem
123
123
  end
124
124
  ```
125
125
 
126
- #### Reading XML:
126
+ #### Reading
127
127
 
128
128
  ```ruby
129
129
  xml_str = '<my_elem>
@@ -156,7 +156,7 @@ Outputs
156
156
  #<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"]}>
157
157
  ```
158
158
 
159
- #### Writing XML:
159
+ #### Writing
160
160
 
161
161
  ```ruby
162
162
  elem = MyElem.new
@@ -189,7 +189,24 @@ The `Namespace` class encapsulates an XML namespace:
189
189
  namespace = Namespace.new(uri: 'http://example.org/px', schema_location: 'http://example.org/px.xsd')
190
190
  ```
191
191
 
192
- Setting a namespace on a mapped object will cause that namespace to be written out when the object is saved
192
+ The `Namespaced` module extends `XML::Mapping` to add a `namespace` attribute and write the namespace
193
+ out when saving to XML.
194
+
195
+ ```ruby
196
+ class MyElem
197
+ include XML::MappingExtensions::Namespaced # instead of XML::Mapping
198
+
199
+ root_element_name 'my_elem'
200
+
201
+ date_node :plain_date, 'plain_date'
202
+ date_node :zulu_date, 'zulu_date', zulu: true
203
+ time_node :time, 'time'
204
+ uri_node :uri, 'uri'
205
+ mime_type_node :mime_type, 'mime_type'
206
+ end
207
+ ```
208
+
209
+ Setting a namespace on a namespaced object will cause that namespace to be written out when the object is saved
193
210
  to XML:
194
211
 
195
212
  ```ruby
@@ -20,8 +20,8 @@ module XML
20
20
  # @param prefix [String, nil] the namespace prefix
21
21
  # @param schema_location [String, nil] the schema location(s)
22
22
  def initialize(uri:, prefix: nil, schema_location: nil)
23
- @uri = uri.to_s
24
- @prefix = prefix
23
+ @uri = uri.to_s
24
+ @prefix = prefix
25
25
  @schema_location = schema_location
26
26
  end
27
27
 
@@ -63,6 +63,7 @@ module XML
63
63
  def state
64
64
  [uri, prefix, schema_location]
65
65
  end
66
+
66
67
  protected :state
67
68
 
68
69
  def set_prefix_recursive(elem) # rubocop:disable Style/AccessorMethodName
@@ -71,35 +72,8 @@ module XML
71
72
  elem.name = "#{prefix}:#{elem.name}"
72
73
  elem.each_element { |e| set_prefix_recursive(e) }
73
74
  end
74
- private :set_prefix_recursive
75
- end
76
- end
77
-
78
- # Patches `XML::Mapping` to add a namespace attribute and write the namespace
79
- # out when saving to XML.
80
- module NamespacedElement
81
-
82
- # @return [Namespace, nil] the namespace, if any
83
- attr_accessor :namespace
84
75
 
85
- # Overrides `XML::Mapping#pre_save` to set the XML namespace and schema location
86
- # on the generated element.
87
- def pre_save(options = { mapping: :_default })
88
- xml = super(options)
89
- namespace.set_default_namespace(xml) if namespace
90
- xml
91
- end
92
-
93
- # Overrides `XML::Mapping#save_to_xml` to set the XML namespace prefix on
94
- # the generated element, and all its descendants that have that namespace.
95
- def save_to_xml(options = { mapping: :_default })
96
- xml = super(options)
97
- namespace.set_prefix(xml) if namespace
98
- xml
76
+ private :set_prefix_recursive
99
77
  end
100
78
  end
101
-
102
- module Mapping
103
- prepend NamespacedElement
104
- end
105
79
  end
@@ -0,0 +1,47 @@
1
+ require 'uri'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+ # Extends `XML::Mapping` to add a `namespace` attribute and write the namespace
6
+ # out when saving to XML.
7
+ module Namespaced
8
+
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ base.include(XML::Mapping)
12
+ base.include(InstanceMethods)
13
+ end
14
+
15
+ module ClassMethods
16
+ def namespace(ns = nil)
17
+ @namespace = ns if ns
18
+ @namespace
19
+ end
20
+ end
21
+
22
+ def namespace
23
+ self.class.namespace
24
+ end
25
+
26
+ # Hack to make sure these don't get defined till after `XML::Mapping`'s include
27
+ # hooks have a chance to define their super methods
28
+ module InstanceMethods
29
+ # Overrides `XML::Mapping#pre_save` to set the XML namespace and schema location
30
+ # on the generated element.
31
+ def pre_save(options = {mapping: :_default})
32
+ xml = super(options)
33
+ namespace.set_default_namespace(xml) if namespace
34
+ xml
35
+ end
36
+
37
+ # Overrides `XML::Mapping#save_to_xml` to set the XML namespace prefix on
38
+ # the generated element, and all its descendants that have that namespace.
39
+ def save_to_xml(options = {mapping: :_default})
40
+ xml = super(options)
41
+ namespace.set_prefix(xml) if namespace
42
+ xml
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.3.7'
4
+ VERSION = '0.4.0'
5
5
  end
6
6
  end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ require 'xml/mapping_extensions/namespace'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+
8
+ UNPREFIXED = Namespace.new(uri: 'http://example.org/nse')
9
+ PREFIXED = Namespace.new(uri: 'http://example.org/nse', prefix: 'px')
10
+
11
+ # TODO: both versions with schema location
12
+
13
+ class Unprefixed
14
+ include Namespaced
15
+ namespace UNPREFIXED
16
+ root_element_name 'unprefixed'
17
+ text_node :attr, '@attr'
18
+ text_node :text, 'text()'
19
+ end
20
+
21
+ class Prefixed
22
+ include Namespaced
23
+ namespace PREFIXED
24
+ root_element_name 'prefixed'
25
+ text_node :attr, '@attr'
26
+ text_node :text, 'text()'
27
+ end
28
+
29
+ describe Namespaced do
30
+ describe '#save_to_xml' do
31
+ it 'writes XML with a default namespace' do
32
+ obj = Unprefixed.new
33
+ obj.attr = 'attr value'
34
+ obj.text = 'text value'
35
+ expected = '<unprefixed xmlns="http://example.org/nse" attr="attr value">text value</unprefixed>'
36
+ expect(obj.write_xml).to be_xml(expected)
37
+ end
38
+ it 'writes XML with a prefixed namespace' do
39
+ obj = Prefixed.new
40
+ obj.attr = 'attr value'
41
+ obj.text = 'text value'
42
+ expected = '<px:prefixed xmlns:px="http://example.org/nse" attr="attr value">text value</unprefixed>'
43
+ expect(obj.write_xml).to be_xml(expected)
44
+ end
45
+ end
46
+
47
+ describe '#parse_xml' do
48
+ it 'parses XML with a default namespace'
49
+ it 'parses XML with a prefixed namespace'
50
+ end
51
+ end
52
+ end
53
+ 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.3.7
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Moles
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -191,6 +191,7 @@ files:
191
191
  - lib/xml/mapping_extensions/date_node.rb
192
192
  - lib/xml/mapping_extensions/mime_type_node.rb
193
193
  - lib/xml/mapping_extensions/namespace.rb
194
+ - lib/xml/mapping_extensions/namespaced.rb
194
195
  - lib/xml/mapping_extensions/node_base.rb
195
196
  - lib/xml/mapping_extensions/time_node.rb
196
197
  - lib/xml/mapping_extensions/typesafe_enum_node.rb
@@ -204,6 +205,7 @@ files:
204
205
  - spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb
205
206
  - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
206
207
  - spec/unit/xml/mapping_extensions/namespace_spec.rb
208
+ - spec/unit/xml/mapping_extensions/namespaced_spec.rb
207
209
  - spec/unit/xml/mapping_extensions/node_base_spec.rb
208
210
  - spec/unit/xml/mapping_extensions/time_node_spec.rb
209
211
  - spec/unit/xml/mapping_extensions/typesafe_enum_node_spec.rb
@@ -242,6 +244,7 @@ test_files:
242
244
  - spec/unit/xml/mapping_extensions/mapping_extensions_spec.rb
243
245
  - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
244
246
  - spec/unit/xml/mapping_extensions/namespace_spec.rb
247
+ - spec/unit/xml/mapping_extensions/namespaced_spec.rb
245
248
  - spec/unit/xml/mapping_extensions/node_base_spec.rb
246
249
  - spec/unit/xml/mapping_extensions/time_node_spec.rb
247
250
  - spec/unit/xml/mapping_extensions/typesafe_enum_node_spec.rb