xml-mapping_extensions 0.2.1 → 0.3.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: 4ad2038c6915f4e3195269c95f734366b100701f
4
- data.tar.gz: e9385d3e05c59809e21425a2a2fada61975a35bf
3
+ metadata.gz: 6cbddeb5cebad76e112d491aa604c17a2662e3cd
4
+ data.tar.gz: 8b54eb808ba7ffb26ddd7bc43b00d8886c6fc9f9
5
5
  SHA512:
6
- metadata.gz: 6f3af4513a661edf05a8ad119745fe5556e59e7ca4bf0de81def9962fe8e00446f8005af34f82c1273dc8d724b87c49f1350881048c9e29d103eb481ecaeaaee
7
- data.tar.gz: 4af18d3e1fb46313683ea91e9ba60fc9e62ee36237783fda16cc691fa1c37b3148336deb6be2ab44e966bb03d8e68690862113ba4156c6de0edd07baa15f8a8e
6
+ metadata.gz: 5da4f0c98e7a224c6363a777ff8558a6fe5229ba7fb6af6f1a56309e2d844919cf01f60adedd6f21c55015b09989b7b5b76df81398a1d274a209a4f3e3ceb471
7
+ data.tar.gz: 91abdca7327b7075defe0d27c9a272fe59f442ed06cf5aa33689befe317f75e56908dfd2cdb0b32ea867c4917230e55b8a84aae73daf1df690382bcefb5cd93a
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.3.0 (19 November 2015)
2
+
3
+ - Replaced `EnumNodeBase` with (simpler) `TypesafeEnumNode` for interoperability
4
+ with [typesafe_enum](https://github.com/dmolesUC3/typesafe_enum) rather than
5
+ [ruby-enum](https://github.com/dblock/ruby-enum/)
6
+
1
7
  ## 0.2.1 (16 November 2015)
2
8
 
3
9
  - Modified `EnumNodeBase` to deal with either enum object instances or enum value
data/README.md CHANGED
@@ -16,7 +16,6 @@ classes, or use one of the provided implementations.
16
16
 
17
17
  - `NodeBase`: Base class for simple single-attribute nodes that
18
18
  convert XML strings to object values.
19
- - `EnumNodeBase`: maps XML strings to `Ruby::Enum` values
20
19
 
21
20
  Note that you must call `::XML::Mapping.add_node_class` for your new node class
22
21
  to be registered with the XML mapping engine.
@@ -27,6 +26,7 @@ to be registered with the XML mapping engine.
27
26
  - `TimeNode`: ISO 8601 strings to `Time` objects
28
27
  - `UriNode`: maps URI strings to `URI` objects
29
28
  - `MimeTypeNode`: maps MIME type strings to `MIME::Type` objects
29
+ - `TypesafeEnumNode`: maps XML strings to [typesafe_enum](https://github.com/dmolesUC3/typesafe_enum) values
30
30
 
31
31
  ### Example
32
32
 
@@ -50,13 +50,13 @@ end
50
50
  #### Reading XML:
51
51
 
52
52
  ```ruby
53
- xml_str = "<my_elem>
53
+ xml_str = '<my_elem>
54
54
  <plain_date>1999-12-31</plain_date>
55
55
  <zulu_date>2000-01-01Z</zulu_date>
56
56
  <time>2000-01-01T02:34:56Z</time>
57
57
  <uri>http://example.org</uri>
58
58
  <mime_type>text/plain</mime_type>
59
- </my_elem>"
59
+ </my_elem>'
60
60
 
61
61
  xml_doc = REXML::Document.new(xml_str)
62
62
  xml_elem = xml_doc.root
@@ -0,0 +1,31 @@
1
+ require_relative 'node_base'
2
+
3
+ require 'typesafe_enum'
4
+
5
+ module XML
6
+ module MappingExtensions
7
+
8
+ # Base class for single-attribute nodes with values that extend `TypesafeEnum::Base`
9
+ #
10
+ # Usage:
11
+ # # for node class MyEnum
12
+ # typesafe_enum_node :my_enum, '@my_enum', default_value: nil, class: MyEnum
13
+ class TypesafeEnumNode < NodeBase
14
+ def initialize(*args)
15
+ super
16
+ @enum_class = @options[:class]
17
+ end
18
+
19
+ def to_value(xml_text)
20
+ enum_instance = @enum_class.find_by_value(xml_text)
21
+ enum_instance = @enum_class.find_by_value_str(xml_text) unless enum_instance
22
+ enum_instance
23
+ end
24
+
25
+ def to_xml_text(enum_instance)
26
+ enum_instance.value.to_s if enum_instance
27
+ end
28
+ end
29
+ ::XML::Mapping.add_node_class TypesafeEnumNode
30
+ end
31
+ end
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
6
6
  end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ module XML
4
+ module MappingExtensions
5
+
6
+ class MyStringEnum < TypesafeEnum::Base
7
+ [:FOO, :BAR, :BAZ_QUX].each { |k| new k }
8
+ end
9
+
10
+ class MySymbolEnum < TypesafeEnum::Base
11
+ new :FOO, :foo
12
+ new :BAR, :bar
13
+ new :BAZ_QUX, :baz_qux
14
+ end
15
+
16
+ class TypesafeEnumNodeSpecElem
17
+ include ::XML::Mapping
18
+ typesafe_enum_node :my_string_enum, '@my_string_enum', default_value: nil, class: MyStringEnum
19
+ typesafe_enum_node :my_symbol_enum, '@my_symbol_enum', default_value: nil, class: MySymbolEnum
20
+
21
+ root_element_name 'elem'
22
+ end
23
+
24
+ describe TypesafeEnumNode do
25
+
26
+ def to_my_string_enum(enum_str)
27
+ xml_string = enum_str ? "<elem my_string_enum='#{enum_str}'/>" : '<elem/>'
28
+ doc = REXML::Document.new(xml_string)
29
+ TypesafeEnumNodeSpecElem.load_from_xml(doc.root).my_string_enum
30
+ end
31
+
32
+ def to_my_symbol_enum(enum_str)
33
+ xml_string = enum_str ? "<elem my_symbol_enum='#{enum_str}'/>" : '<elem/>'
34
+ doc = REXML::Document.new(xml_string)
35
+ TypesafeEnumNodeSpecElem.load_from_xml(doc.root).my_symbol_enum
36
+ end
37
+
38
+ def string_enum_to_text(enum)
39
+ elem = TypesafeEnumNodeSpecElem.new
40
+ elem.my_string_enum = enum
41
+ xml = elem.save_to_xml
42
+ xml.attributes['my_string_enum']
43
+ end
44
+
45
+ def symbol_enum_to_text(enum)
46
+ elem = TypesafeEnumNodeSpecElem.new
47
+ elem.my_symbol_enum = enum
48
+ xml = elem.save_to_xml
49
+ xml.attributes['my_symbol_enum']
50
+ end
51
+
52
+ it 'works with a string enum' do
53
+ MyStringEnum.each do |enum|
54
+ expect(to_my_string_enum(enum.value)).to eq(enum)
55
+ end
56
+
57
+ MyStringEnum.each do |enum|
58
+ expect(string_enum_to_text(enum)).to eq(enum.value)
59
+ end
60
+ end
61
+
62
+ it 'works with a symbol enum' do
63
+ MySymbolEnum.each do |enum|
64
+ expect(to_my_symbol_enum(enum.value)).to eq(enum)
65
+ end
66
+
67
+ MySymbolEnum.each do |enum|
68
+ expect(symbol_enum_to_text(enum)).to eq(enum.value.to_s)
69
+ end
70
+ end
71
+
72
+ it 'parses a missing value as nil' do
73
+ expect(to_my_string_enum(nil)).to be_nil
74
+ end
75
+
76
+ it 'doesn\'t set an attribute for a nil value' do
77
+ expect(string_enum_to_text(nil)).to be_nil
78
+ end
79
+
80
+ it 'accepts enum constants' do
81
+ elem = TypesafeEnumNodeSpecElem.new
82
+ elem.my_string_enum = MyStringEnum::BAZ_QUX
83
+ xml_string = '<elem my_string_enum="baz_qux"/>'
84
+ expect(elem.save_to_xml).to be_xml(xml_string)
85
+ end
86
+
87
+ it 'round-trips to XML' do
88
+ xml_string = '<elem my_string_enum="baz_qux"/>'
89
+ doc = REXML::Document.new(xml_string)
90
+ elem = TypesafeEnumNodeSpecElem.load_from_xml(doc.root)
91
+ expect(elem.save_to_xml).to be_xml(xml_string)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ['lib']
24
24
 
25
25
  spec.add_dependency 'mime-types', '~> 2.5'
26
- spec.add_dependency 'ruby-enum', '~> 0.4'
26
+ spec.add_dependency 'typesafe_enum', '~> 0.1', '>= 0.1.1'
27
27
  spec.add_dependency 'xml-mapping', '~> 0.10'
28
28
 
29
29
  spec.add_development_dependency 'equivalent-xml', '~> 0.6.0'
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.2.1
4
+ version: 0.3.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: 2015-11-17 00:00:00.000000000 Z
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -25,19 +25,25 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.5'
27
27
  - !ruby/object:Gem::Dependency
28
- name: ruby-enum
28
+ name: typesafe_enum
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.4'
33
+ version: '0.1'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 0.1.1
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
41
  - - "~>"
39
42
  - !ruby/object:Gem::Version
40
- version: '0.4'
43
+ version: '0.1'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.1
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: xml-mapping
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -183,20 +189,20 @@ files:
183
189
  - example.rb
184
190
  - lib/xml/mapping_extensions.rb
185
191
  - lib/xml/mapping_extensions/date_node.rb
186
- - lib/xml/mapping_extensions/enum_node_base.rb
187
192
  - lib/xml/mapping_extensions/mime_type_node.rb
188
193
  - lib/xml/mapping_extensions/node_base.rb
189
194
  - lib/xml/mapping_extensions/time_node.rb
195
+ - lib/xml/mapping_extensions/typesafe_enum_node.rb
190
196
  - lib/xml/mapping_extensions/uri_node.rb
191
197
  - lib/xml/mapping_extensions/version.rb
192
198
  - spec/.rubocop.yml
193
199
  - spec/rspec_custom_matchers.rb
194
200
  - spec/spec_helper.rb
195
201
  - spec/unit/xml/mapping_extensions/date_node_spec.rb
196
- - spec/unit/xml/mapping_extensions/enum_node_base_spec.rb
197
202
  - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
198
203
  - spec/unit/xml/mapping_extensions/node_base_spec.rb
199
204
  - spec/unit/xml/mapping_extensions/time_node_spec.rb
205
+ - spec/unit/xml/mapping_extensions/typesafe_enum_node_spec.rb
200
206
  - spec/unit/xml/mapping_extensions/uri_node_spec.rb
201
207
  - xml-mapping_extensions.gemspec
202
208
  homepage: http://github.com/dmolesUC3/xml-mapping_extensions
@@ -228,9 +234,9 @@ test_files:
228
234
  - spec/rspec_custom_matchers.rb
229
235
  - spec/spec_helper.rb
230
236
  - spec/unit/xml/mapping_extensions/date_node_spec.rb
231
- - spec/unit/xml/mapping_extensions/enum_node_base_spec.rb
232
237
  - spec/unit/xml/mapping_extensions/mime_type_node_spec.rb
233
238
  - spec/unit/xml/mapping_extensions/node_base_spec.rb
234
239
  - spec/unit/xml/mapping_extensions/time_node_spec.rb
240
+ - spec/unit/xml/mapping_extensions/typesafe_enum_node_spec.rb
235
241
  - spec/unit/xml/mapping_extensions/uri_node_spec.rb
236
242
  has_rdoc:
@@ -1,30 +0,0 @@
1
- require_relative 'node_base'
2
-
3
- require 'ruby-enum'
4
-
5
- module XML
6
- module MappingExtensions
7
-
8
- # Base class for single-attribute nodes with `Ruby::Enum` values
9
- #
10
- # Usage:
11
- # - extend this class
12
- # - add an `ENUM_CLASS` constant whose value is the `Ruby::Enum` class
13
- # to be mapped
14
- # - call `::XML::Mapping.add_node_class` to add your new node class
15
- class EnumNodeBase < NodeBase
16
-
17
- def to_value(xml_text)
18
- enum_class = self.class::ENUM_CLASS
19
- enum_class.map do |_, enum|
20
- return enum if enum.value == xml_text
21
- end
22
- end
23
-
24
- def to_xml_text(value)
25
- value.respond_to?(:value) ? value.value : value.to_s
26
- end
27
-
28
- end
29
- end
30
- end
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module XML
4
- module MappingExtensions
5
-
6
- class MyEnum
7
- include Ruby::Enum
8
-
9
- define :FOO, 'foo'
10
- define :BAR, 'bar'
11
- define :BAZ_QUX, 'baz_qux'
12
- end
13
-
14
- class MyEnumNode < EnumNodeBase
15
- ENUM_CLASS = MyEnum
16
- end
17
- ::XML::Mapping.add_node_class(MyEnumNode)
18
-
19
- class EnumNodeBaseSpecElem
20
- include ::XML::Mapping
21
- my_enum_node :my_enum, '@my_enum', default_value: nil
22
-
23
- root_element_name 'elem'
24
-
25
- def self.from_str(enum_str)
26
- xml_string = enum_str ? "<elem my_enum='#{enum_str}'/>" : '<elem/>'
27
- doc = REXML::Document.new(xml_string)
28
- load_from_xml(doc.root)
29
- end
30
- end
31
- describe EnumNodeBase do
32
-
33
- def to_my_enum(str)
34
- EnumNodeBaseSpecElem.from_str(str).my_enum
35
- end
36
-
37
- def to_text(enum)
38
- elem = EnumNodeBaseSpecElem.new
39
- elem.my_enum = enum
40
- xml = elem.save_to_xml
41
- xml.attributes['my_enum']
42
- end
43
-
44
- it 'parses an enum value' do
45
- MyEnum.map do |_, enum|
46
- expect(to_my_enum(enum.value)).to eq(enum)
47
- end
48
- end
49
-
50
- it 'transforms an enum to a string' do
51
- MyEnum.map do |_, enum|
52
- expect(to_text(enum)).to eq(enum.value)
53
- end
54
- end
55
-
56
- it 'parses a missing value as nil' do
57
- expect(to_my_enum(nil)).to be_nil
58
- end
59
-
60
- it 'doesn\'t set an attribute for a nil value' do
61
- expect(to_text(nil)).to be_nil
62
- end
63
-
64
- it 'accepts enum constants' do
65
- elem = EnumNodeBaseSpecElem.new
66
- elem.my_enum = MyEnum::BAZ_QUX
67
- xml_string = '<elem my_enum="baz_qux"/>'
68
- expect(elem.save_to_xml).to be_xml(xml_string)
69
- end
70
-
71
- it 'round-trips to XML' do
72
- xml_string = '<elem my_enum="baz_qux"/>'
73
- doc = REXML::Document.new(xml_string)
74
- elem = EnumNodeBaseSpecElem.load_from_xml(doc.root)
75
- expect(elem.save_to_xml).to be_xml(xml_string)
76
- end
77
- end
78
- end
79
- end