xml-mapping_extensions 0.4.5 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91ed4292829e9d05dae2299e8d9f79d6e8e668db
4
- data.tar.gz: 7980a10f39051df33fe45df96b0541d9242fcc00
3
+ metadata.gz: c4e7ef0d9b37dd1ecc3569d5aeadb75a9da079b1
4
+ data.tar.gz: 59a93fa0fa398f3ea4d493c7751f93e3ae34c9de
5
5
  SHA512:
6
- metadata.gz: 2a79811d0aac935feb3e80dfa218d08f84f8d20726c34616dde559ec18c3422501ca6686affa8c8434470efc20b2319efb8765dceff3a829b7a6ce01ed157456
7
- data.tar.gz: bbfb24c26fa14bebc99d5a6b84290ca1d3d1e9050828a534fa3616710bf6411f0c98e085ed2ab71e55cdb5971380a5e2f3d1d911b11929c3f606a857f1452e2a
6
+ metadata.gz: de2478465f7e44829e949505834d290274466d43d643bab3f8cef180fe35e7db1fd3c4ad0a1a07a022b818c5e5d84fb15f108c8a01eb1e58157dc27abaeec8a9
7
+ data.tar.gz: 19a67e611dfaf5ae7a8b509598faaa0c84546f0f5a98a3f27058f26cef646c278642b8e39a73a844eaa9a3eaf798698ac95bda6158737816fab92d7a0ba52234
data/CHANGES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.4.6 (15 August 2016)
2
+
3
+ - `TypesafeEnumNode.to_value()` now raises `ArgumentError` if the argument is not a valid value for the node's
4
+ enum type.
5
+ - Added `#write_to_file` to simplify writing a root element to a file.
6
+
1
7
  ## 0.4.5 (14 July 2016)
2
8
 
3
9
  - Make `#fallback_mapping` preserve the order of the fallback mapping instead of appending all fallback
@@ -5,7 +11,7 @@
5
11
 
6
12
  ## 0.4.4 (12 July 2016)
7
13
 
8
- - Add '#fallback_mapping'
14
+ - Add `#fallback_mapping`
9
15
 
10
16
  ## 0.4.3 (11 July 2016)
11
17
 
@@ -21,17 +21,20 @@ module XML
21
21
  end
22
22
 
23
23
  # Converts an enum value or value string to an enum instance
24
- # @param xml_text the enum value or value string
25
- # @return [TypesafeEnum::Base] an instance of the enum class declared in the initializer
24
+ # @param xml_text [String, nil] the enum value or value string
25
+ # @return [TypesafeEnum::Base, nil] an instance of the enum class declared in the initializer,
26
+ # or nil if `xml_text` is nil
26
27
  def to_value(xml_text)
28
+ return nil unless xml_text
27
29
  enum_instance = @enum_class.find_by_value(xml_text)
28
30
  enum_instance = @enum_class.find_by_value_str(xml_text) unless enum_instance
31
+ fail ArgumentError, "No instance of enum class #{@enum_class.name} found for value '#{xml_text}'" unless enum_instance
29
32
  enum_instance
30
33
  end
31
34
 
32
35
  # Converts an enum value or value string to an enum instance
33
- # @param enum_instance [TypesafeEnum::Base] an instance of the enum class declared in the initializer
34
- # @return [String] the enum value as a string
36
+ # @param enum_instance [TypesafeEnum::Base, nil] an instance of the enum class declared in the initializer
37
+ # @return [String, nil] the enum value as a string, or nil if `enum_instance` is nil
35
38
  def to_xml_text(enum_instance)
36
39
  enum_instance.value.to_s if enum_instance
37
40
  end
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.4.5'
4
+ VERSION = '0.4.6'
5
5
  end
6
6
  end
@@ -34,7 +34,7 @@ module XML
34
34
 
35
35
  module Mapping
36
36
 
37
- # Writes this mapped object as an XML string.
37
+ # Writes this mapped object as a pretty-printed XML string.
38
38
  #
39
39
  # @param options [Hash] the options to be passed to
40
40
  # [XML::Mapping#save_to_xml](http://multi-io.github.io/xml-mapping/XML/Mapping.html#method-i-save_to_xml)
@@ -48,6 +48,27 @@ module XML
48
48
  io.string
49
49
  end
50
50
 
51
+ # Writes this mapped object to the specified file, as an XML document (including declaration).
52
+ #
53
+ # @param path [String] The path of the file to write to
54
+ # @param indent [Integer] The indentation level. Defaults to -1 (no indenting). Note
55
+ # that this parameter will be ignored if `pretty` is present.
56
+ # @param pretty [Boolean] Whether to pretty-print the output. Defaults to `false`. Note
57
+ # that this option overrides `indent`.
58
+ # @param options [Hash] the options to be passed to
59
+ # [XML::Mapping#save_to_xml](http://multi-io.github.io/xml-mapping/XML/Mapping.html#method-i-save_to_xml)
60
+ def write_to_file(path, indent: -1, pretty: false, options: { mapping: :_default })
61
+ doc = REXML::Document.new
62
+ doc << REXML::XMLDecl.new
63
+ doc.elements[1] = save_to_xml(options)
64
+ File.open(path, 'w') do |f|
65
+ return doc.write(f, indent) unless pretty
66
+ formatter = REXML::Formatters::Pretty.new
67
+ formatter.compact = true
68
+ formatter.write(doc, f)
69
+ end
70
+ end
71
+
51
72
  # Additional accessors needed for `#fallback_mapping`.
52
73
  class Node
53
74
  attr_reader :attrname
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'tempfile'
3
+ require 'tmpdir'
3
4
 
4
5
  module XML
5
6
  module Mapping
@@ -61,6 +62,37 @@ module XML
61
62
  end
62
63
  end
63
64
 
65
+ describe '#write_to_file' do
66
+
67
+ before(:each) do
68
+ @obj = MXSpecObject.new
69
+ @obj.attribute = 123
70
+ @obj.text = 'element text'
71
+ @obj.children = ['child 1', 'child 2']
72
+ @expected = REXML::Document.new(@obj.write_xml)
73
+ end
74
+
75
+ it 'writes to a file' do
76
+ Dir.mktmpdir do |tmp|
77
+ outfile = "#{tmp}/mxspec.xml"
78
+ @obj.write_to_file(outfile, indent: 4)
79
+ outxml = File.read(outfile)
80
+ puts("'#{outxml}'")
81
+ expect(outxml).to be_xml(@expected)
82
+ end
83
+ end
84
+
85
+ it 'pretty-prints to a file' do
86
+ Dir.mktmpdir do |tmp|
87
+ outfile = "#{tmp}/mxspec.xml"
88
+ @obj.write_to_file(outfile, pretty: true)
89
+ outxml = File.read(outfile)
90
+ puts("'#{outxml}'")
91
+ expect(outxml).to be_xml(@expected)
92
+ end
93
+ end
94
+ end
95
+
64
96
  describe '#parse_xml' do
65
97
 
66
98
  before(:each) do
@@ -69,10 +69,14 @@ module XML
69
69
  end
70
70
  end
71
71
 
72
- it 'parses a missing value as nil' do
72
+ it 'parses a nil value as nil' do
73
73
  expect(to_my_string_enum(nil)).to be_nil
74
74
  end
75
75
 
76
+ it 'raises ArgumentError for an invalid value' do
77
+ expect { to_my_string_enum('elvis') }.to raise_error(ArgumentError)
78
+ end
79
+
76
80
  it 'doesn\'t set an attribute for a nil value' do
77
81
  expect(string_enum_to_text(nil)).to be_nil
78
82
  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.4.5
4
+ version: 0.4.6
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-07-14 00:00:00.000000000 Z
11
+ date: 2016-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types