xml-mapping_extensions 0.3.4 → 0.3.5

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: 2a1dd7637b8de4a6a345debc25f742429680b254
4
- data.tar.gz: eb6b2d9a0329dc1c6ebb4eec332afca4563a4590
3
+ metadata.gz: 1f632745c95852e892e50841d1dae290012d7941
4
+ data.tar.gz: 308acdeaa42233881b291b2adce912641e50a993
5
5
  SHA512:
6
- metadata.gz: 863b8c8d288cef6a7786e7fc0b4855d3d35d4de5ee73065f22512a79d0a0d54c81dff407534b25cfc1cb9f3d775a162539672cc9e2ae4741b6af3f6412a85c6e
7
- data.tar.gz: 5ac77afe37714a99b341b9a7f083b68fe4daa1652ee03432007577eeccbdcf9f6559f5024408abca7e97ed2fd754f9172e8d7ae56f6c93991006df0c2e53914e
6
+ metadata.gz: 3280dae9565abd98b0a8800ae66d217097ded31f275dde511b5c652760ad54f27462940b92b7ab539cb1550755af34773879e0fe10b38c866d97e0d290ddb140
7
+ data.tar.gz: 0885775aa138a6dacf348f572f9843a97994c036abeab239a36d75816ef19719af29803e54ff57889078d752f47dce67fbeb590e0c40e8c700ea1e14d858794e
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.5 (28 April 2016)
2
+
3
+ - `DateNode`: Fix issues with `Date.xmlschema` misbehaving in a Rails / ActiveSupport environment
4
+ - `NodeBase`: Improve parsing error reporting, so that we get the node owner and attribute and the offending value
5
+
1
6
  ## 0.3.4 (27 January 2016)
2
7
 
3
8
  - Make gemspec smart enough to handle SSH checkouts
data/Rakefile CHANGED
@@ -12,14 +12,13 @@ namespace :spec do
12
12
  task.pattern = 'unit/**/*_spec.rb'
13
13
  end
14
14
 
15
- desc 'Run all acceptance tests'
16
- RSpec::Core::RakeTask.new(:acceptance) do |task|
17
- ENV['COVERAGE'] = nil
18
- task.rspec_opts = %w(--color --format documentation --order default)
19
- task.pattern = 'acceptance/**/*_spec.rb'
15
+ desc 'Run all unit tests with ActiveSupport required'
16
+ RSpec::Core::RakeTask.new(:active_support_unit) do |task|
17
+ task.rspec_opts = %w(--color --format documentation --order default --require active_support/all)
18
+ task.pattern = 'unit/**/*_spec.rb'
20
19
  end
21
20
 
22
- task all: [:unit, :acceptance]
21
+ task all: [:unit, :active_support_unit]
23
22
  end
24
23
 
25
24
  desc 'Run all tests'
@@ -53,4 +52,4 @@ end
53
52
  # Defaults
54
53
 
55
54
  desc 'Run unit tests, check test coverage, run acceptance tests, check code style'
56
- task default: [:coverage, 'spec:acceptance', :rubocop]
55
+ task default: [:coverage, 'spec:active_support_unit', :rubocop]
@@ -28,8 +28,8 @@ module XML
28
28
  # @return [String] the value as an XML Schema date string, without
29
29
  # time zone information unless {#zulu} is set
30
30
  def to_xml_text(value)
31
- text = value.xmlschema
32
- zulu ? "#{text}Z" : text
31
+ text = value.iso8601 # use iso8601 instead of xmlschema in case of ActiveSupport shenanigans
32
+ (zulu && !text.end_with?('Z')) ? "#{text}Z" : text
33
33
  end
34
34
  end
35
35
  ::XML::Mapping.add_node_class DateNode
@@ -25,6 +25,9 @@ module XML
25
25
  def extract_attr_value(xml)
26
26
  xml_text = default_when_xpath_err { @path.first(xml).text }
27
27
  to_value(xml_text) if xml_text
28
+ rescue => e
29
+ bad_value = xml_text ? "'#{xml_text}'" : 'nil'
30
+ raise e, "#{@owner}.#{@attrname}: Can't parse #{bad_value} as #{self.class}: #{e.message}"
28
31
  end
29
32
 
30
33
  # Implements `::XML::Mapping::SingleAttributeNode#set_attr_value`.
@@ -37,9 +40,9 @@ module XML
37
40
  end
38
41
 
39
42
  # Override this method to convert XML text to an object value
40
- # @param _xml_text [String] The XML string to parse
43
+ # @param xml_text [String] The XML string to parse
41
44
  # @return [Object] The object value
42
- def to_value(_xml_text)
45
+ def to_value(xml_text) # rubocop:disable Lint/UnusedMethodArgument
43
46
  fail NoMethodError, "#{self.class} should override #to_value to convert an XML string to an object value"
44
47
  end
45
48
 
@@ -1,6 +1,6 @@
1
1
  module XML
2
2
  module MappingExtensions
3
3
  # The version of this gem
4
- VERSION = '0.3.4'
4
+ VERSION = '0.3.5'
5
5
  end
6
6
  end
@@ -63,6 +63,67 @@ module XML
63
63
  expect(actual).to eq(expected)
64
64
  end
65
65
 
66
+ describe 'works with Date.today' do
67
+ it 'as plain date' do
68
+ date = Date.today
69
+ expected = format('%04d-%02d-%02d', date.year, date.month, date.day)
70
+ actual = to_text(date)
71
+ expect(actual).to eq(expected)
72
+ end
73
+
74
+ it 'as "zulu" date' do
75
+ date = Date.today
76
+ expected = format('%04d-%02d-%02dZ', date.year, date.month, date.day)
77
+ actual = to_zulu_text(date)
78
+ expect(actual).to eq(expected)
79
+ end
80
+ end
81
+
82
+ if Date.respond_to?(:current)
83
+ describe 'works with Date.current' do
84
+ it 'as plain date' do
85
+ date = Date.current
86
+ expected = format('%04d-%02d-%02d', date.year, date.month, date.day)
87
+ actual = to_text(date)
88
+ expect(actual).to eq(expected)
89
+ end
90
+
91
+ it 'as "zulu" date' do
92
+ date = Date.current
93
+ expected = format('%04d-%02d-%02dZ', date.year, date.month, date.day)
94
+ actual = to_zulu_text(date)
95
+ expect(actual).to eq(expected)
96
+ end
97
+ end
98
+ end
99
+
100
+ if Time.respond_to?(:zone)
101
+ describe 'works with Time.zone' do
102
+ before(:each) do
103
+ @old_zone = Time.zone
104
+ Time.zone = 'UTC'
105
+ end
106
+ after(:each) do
107
+ Time.zone = @old_zone
108
+ end
109
+
110
+ it 'as plain date' do
111
+ date = Time.zone.today
112
+ expected = format('%04d-%02d-%02d', date.year, date.month, date.day)
113
+ actual = to_text(date)
114
+ expect(actual).to eq(expected)
115
+ end
116
+
117
+ it 'as "zulu" date' do
118
+ date = Time.zone.today
119
+ expected = format('%04d-%02d-%02dZ', date.year, date.month, date.day)
120
+ actual = to_zulu_text(date)
121
+ expect(actual).to eq(expected)
122
+ end
123
+
124
+ end
125
+ end
126
+
66
127
  end
67
128
  end
68
129
  end
@@ -3,11 +3,7 @@ require 'spec_helper'
3
3
  module XML
4
4
  module MappingExtensions
5
5
 
6
- class SomeMappingClass
7
- include ::XML::Mapping
8
- end
9
-
10
- class LaTeXRationalNode < NodeBase
6
+ class LatexRationalNode < NodeBase
11
7
  def to_value(xml_text)
12
8
  match_data = /\\frac\{([0-9.]+)\}\{([0-9.]+)\}/.match(xml_text)
13
9
  Rational("#{match_data[1]}/#{match_data[2]}")
@@ -17,37 +13,54 @@ module XML
17
13
  "\\frac{#{value.numerator}}{#{value.denominator}}"
18
14
  end
19
15
  end
16
+ ::XML::Mapping.add_node_class LatexRationalNode
20
17
 
21
- describe NodeBase do
22
- before :each do
23
- @node = NodeBase.new(SomeMappingClass, :attr_name, 'attr_name')
24
- end
18
+ class SomeElem
19
+ include ::XML::Mapping
20
+
21
+ latex_rational_node :rational, '@rational'
22
+ end
25
23
 
24
+ describe NodeBase do
26
25
  describe '#extract_attr_value' do
27
26
  it 'forwards to #to_value' do
28
- node = LaTeXRationalNode.new(SomeMappingClass, :attr_name, 'attr_name')
29
- expect(node.to_value('\frac{1}{2}')).to eq(Rational('1/2'))
27
+ xml = REXML::Document.new('<some_elem rational="\frac{1}{2}"/>').root
28
+ mapping = SomeElem.load_from_xml(xml)
29
+
30
+ expect(mapping.rational).to eq(Rational('1/2'))
31
+ end
32
+
33
+ it 'produces useful error messages' do
34
+ xml = REXML::Document.new('<some_elem rational="elvis"/>').root
35
+ expect { SomeElem.load_from_xml(xml) }.to raise_error do |e|
36
+ expect(e.message).to match(/SomeElem.rational.*'elvis'/)
37
+ end
30
38
  end
31
39
  end
32
40
 
33
41
  describe '#set_attr_value' do
34
42
  it 'forwards to #to_xml_text' do
35
- node = LaTeXRationalNode.new(SomeMappingClass, :attr_name, 'attr_name')
43
+ node = LatexRationalNode.new(SomeElem, :rational, 'rational')
36
44
  expect(node.to_xml_text(Rational('1/2'))).to eq('\frac{1}{2}')
37
45
  end
38
46
  end
39
47
 
40
48
  describe '#xml_text' do
41
49
  it 'should be abstract' do
42
- expect { @node.to_value('some text') }.to raise_error(NoMethodError)
50
+ node = NodeBase.allocate
51
+ expect { node.to_value('some text') }.to raise_error do |e|
52
+ expect(e).to be_a(NoMethodError)
53
+ expect(e.message).to include('should override #to_value')
54
+ end
43
55
  end
44
56
  end
45
57
 
46
58
  describe '#to_xml_text' do
47
59
  it 'should call to_s by default' do
60
+ node = NodeBase.allocate
48
61
  values = ['elvis', 123, Object.new]
49
62
  values.each do |v|
50
- expect(@node.to_xml_text(v)).to eq(v.to_s)
63
+ expect(node.to_xml_text(v)).to eq(v.to_s)
51
64
  end
52
65
  end
53
66
  end
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency 'typesafe_enum', '~> 0.1', '>= 0.1.5'
28
28
  spec.add_dependency 'xml-mapping', '~> 0.10'
29
29
 
30
+ spec.add_development_dependency 'activesupport', '~> 4.2' # for testing
30
31
  spec.add_development_dependency 'equivalent-xml', '~> 0.6.0'
31
32
  spec.add_development_dependency 'rake', '~> 10.4'
32
33
  spec.add_development_dependency 'rspec', '~> 3.2'
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.4
4
+ version: 0.3.5
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-01-27 00:00:00.000000000 Z
11
+ date: 2016-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0.10'
61
+ - !ruby/object:Gem::Dependency
62
+ name: activesupport
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '4.2'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '4.2'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: equivalent-xml
63
77
  requirement: !ruby/object:Gem::Requirement