datacite-mapping 0.2.0 → 0.2.1

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: 8ca854763f76a711279af3353b9c5a9a5414058c
4
- data.tar.gz: 3f99767c5394f6ae49e2ef60253d5064bfe33cd7
3
+ metadata.gz: 31f2287ef40248012202b33a003f19aa2d43b2dd
4
+ data.tar.gz: 1c48f2fd6d1d77d78c4c0dac2aa3e794ed9d32e3
5
5
  SHA512:
6
- metadata.gz: 4161a49d7de22ed8528970d7a90afd4231ddd4e7a311aff1f836cbcd34cdaddde9b13719bd3fba239045dd8f636f4e5a226714354dab57bbf2f10cbe89e3a2d0
7
- data.tar.gz: c4af15defeecf0ed3c9a36c3de15ce980f157b1baafac5d1b43a5d393c0067d3a3c3fdef7b0b9634cf116d191e899e920b9cdd88a719f0f3ca5a08c17f73c1ca
6
+ metadata.gz: d40f600f404342223fa28d062f23086b2d22b19875f2b324a23b1bb6b4dba8526b2637b32aaf6d74337dd26ce6bcf26e152a221510e6bc512901ef36747a3f0e
7
+ data.tar.gz: 674b3c269064d693e6d06b3aad48cb3f4cd2346600240d14e2a08a90955a3a023e0f79e822cb309db2f19b7c52b016d70921e2b0cd252c0f7546031a545ebeaf
data/CHANGES.md CHANGED
@@ -1,4 +1,9 @@
1
- ## 0.2.0
1
+ ## 0.2.1 (28 September 2016)
2
+
3
+ - Fixed issue where `Datacite::Mapping::Date` objects initialized with Ruby
4
+ `Dates` or `Datetimes` couldn't be serialized to XML.
5
+
6
+ ## 0.2.0 (26 September 2016)
2
7
 
3
8
  - Datacite 4.0 support:
4
9
  - reading supports both Datacite 3 and Datacite 4
data/README.md CHANGED
@@ -88,6 +88,8 @@ To create XML from a `Resource` object, use `Resource.write_xml`, `Resource.save
88
88
  | file path | `Resource.save_to_file` |
89
89
  | `REXML::Element` | `Resource.save_xml` |
90
90
 
91
+ Example:
92
+
91
93
  ```ruby
92
94
  resource.write_xml
93
95
  # => "<resource xsi:schemaLocation='http://datacite.org/schema/kernel-4 …
@@ -49,6 +49,7 @@ module Datacite
49
49
  # @return [DateValue, nil] the end of the date range represented by this `<date/> field`,
50
50
  # if it represents a range, and the range is not open on the upper end
51
51
  class Date
52
+ include Comparable
52
53
  include XML::Mapping
53
54
 
54
55
  attr_reader :date_value
@@ -81,7 +82,24 @@ module Datacite
81
82
  else
82
83
  fail ArgumentError, "Unable to parse date value #{val}"
83
84
  end
84
- @value = val
85
+ @value = date_value ? date_value.to_s : "#{range_start}/#{range_end}"
86
+ end
87
+
88
+ def <=>(other)
89
+ return nil unless other.class == self.class
90
+ [:date_value, :range_start, :range_end].each do |v|
91
+ order = send(v) <=> other.send(v)
92
+ return order if order.nonzero?
93
+ end
94
+ 0
95
+ end
96
+
97
+ def hash
98
+ [date_value, range_start, range_end].hash
99
+ end
100
+
101
+ def to_s
102
+ @value
85
103
  end
86
104
 
87
105
  private
@@ -32,6 +32,7 @@ module Datacite
32
32
  attr_reader :nsec
33
33
  attr_reader :date
34
34
  attr_reader :zone
35
+ attr_reader :iso_value
35
36
 
36
37
  # Creates a new {DateValue}.
37
38
  #
@@ -43,7 +44,7 @@ module Datacite
43
44
  @year = to_year(val)
44
45
  @month = to_month(val)
45
46
  @day = to_day(val)
46
- iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
47
+ @iso_value = val.respond_to?(:iso8601) ? val.iso8601 : val.to_s
47
48
  if datetime && iso_value.include?('T')
48
49
  @hour = datetime.hour
49
50
  @minute = datetime.minute
@@ -68,11 +69,14 @@ module Datacite
68
69
  end
69
70
 
70
71
  def to_s
71
- 'DateTime(' + [:year, :month, :day, :hour, :minute, :sec, :nsec, :zone].map do |field|
72
- value = send(field)
73
- value = format('%02d', value) if value && value.is_a?(Integer)
74
- "#{field}: #{value}" if value
75
- end.compact.join(', ') + ')'
72
+ @str_value = begin
73
+ str_value = iso_value
74
+ if nsec && nsec != 0
75
+ frac_str = (nsec / 1e9).to_s.sub('0', '')
76
+ str_value.sub!(/(T[0-9]+:[0-9]+:[0-9]+)/, "\\1#{frac_str}") unless str_value.include?(frac_str)
77
+ end
78
+ str_value
79
+ end
76
80
  end
77
81
 
78
82
  private
@@ -4,7 +4,7 @@ module Datacite
4
4
  NAME = 'datacite-mapping'.freeze
5
5
 
6
6
  # The version of this gem
7
- VERSION = '0.2.0'.freeze
7
+ VERSION = '0.2.1'.freeze
8
8
 
9
9
  # The copyright notice for this gem
10
10
  COPYRIGHT = 'Copyright (c) 2016 The Regents of the University of California'.freeze
@@ -29,6 +29,24 @@ module Datacite
29
29
  it 'fails on range-like non-ranges' do
30
30
  expect { Date.new(value: '5/18/72', type: DateType::VALID) }.to raise_error(ArgumentError)
31
31
  end
32
+
33
+ it 'saves closed ranges to XML' do
34
+ date = Date.new(value: '1997-07-16T19:30+10:00/1997-07-17T15:30-05:00', type: DateType::AVAILABLE)
35
+ expected_xml = '<date dateType="Available">1997-07-16T19:30+10:00/1997-07-17T15:30-05:00</date>'
36
+ expect(date.save_to_xml).to be_xml(expected_xml)
37
+ end
38
+
39
+ it 'saves right open ranges to XML' do
40
+ date = Date.new(value: '1997-07-16T19:30+10:00/', type: DateType::AVAILABLE)
41
+ expected_xml = '<date dateType="Available">1997-07-16T19:30+10:00/</date>'
42
+ expect(date.save_to_xml).to be_xml(expected_xml)
43
+ end
44
+
45
+ it 'saves left open ranges to XML' do
46
+ date = Date.new(value: '/1997-07-17T15:30-05:00', type: DateType::AVAILABLE)
47
+ expected_xml = '<date dateType="Available">/1997-07-17T15:30-05:00</date>'
48
+ expect(date.save_to_xml).to be_xml(expected_xml)
49
+ end
32
50
  end
33
51
 
34
52
  describe 'type=' do
@@ -51,6 +69,24 @@ module Datacite
51
69
  expected_xml = '<date dateType="Available">1914-08-04T11:01:06.0123+01:00</date>'
52
70
  expect(d.save_to_xml).to be_xml(expected_xml)
53
71
  end
72
+
73
+ it 'writes XML with all value types' do
74
+ {
75
+ with_date_time: DateTime.new(1914, 8, 4, 11, 1, 6.0123, '+1'),
76
+ with_date: ::Date.new(1914, 8, 4),
77
+ with_year: 1914,
78
+ with_year_str: '1914',
79
+ with_year_month: '1914-08',
80
+ iso8601: '1914-08-04T11:01+01:00',
81
+ iso8601_secs: '1914-08-04T11:01:06+01:00',
82
+ iso8601_frac: '1914-08-04T11:01:06.0123+01:00'
83
+ }.each do |k, v|
84
+ d = Date.new(value: v, type: DateType::AVAILABLE)
85
+ xml = d.save_to_xml
86
+ parsed = Date.parse_xml(xml)
87
+ expect(parsed).to eq(d), "Expected #{d}, got #{parsed} for #{k}"
88
+ end
89
+ end
54
90
  end
55
91
 
56
92
  describe '#load_from_xml' do
@@ -4,6 +4,9 @@ module Datacite
4
4
  module Mapping
5
5
  describe DateValue do
6
6
 
7
+ attr_reader :values
8
+ attr_reader :dates
9
+
7
10
  before(:all) do
8
11
  @values = {
9
12
  with_date_time: DateTime.new(1914, 8, 4, 11, 1, 6.0123, '+1'),
@@ -15,12 +18,12 @@ module Datacite
15
18
  iso8601_secs: '1914-08-04T11:01:06+01:00',
16
19
  iso8601_frac: '1914-08-04T11:01:06.0123+01:00'
17
20
  }
18
- @dates = @values.map { |format, v| [format, DateValue.new(v)] }.to_h
21
+ @dates = values.map { |format, v| [format, DateValue.new(v)] }.to_h
19
22
  end
20
23
 
21
24
  describe '#initialize' do
22
25
  it 'accepts a DateTime' do
23
- d = @dates[:with_date_time]
26
+ d = dates[:with_date_time]
24
27
  expect(d).to be_a(DateValue)
25
28
  expect(d.year).to eq(1914)
26
29
  expect(d.month).to eq(8)
@@ -33,7 +36,7 @@ module Datacite
33
36
  end
34
37
 
35
38
  it 'accepts a DateValue' do
36
- d = @dates[:with_date]
39
+ d = dates[:with_date]
37
40
  expect(d).to be_a(DateValue)
38
41
  expect(d.year).to eq(1914)
39
42
  expect(d.month).to eq(8)
@@ -42,26 +45,26 @@ module Datacite
42
45
  end
43
46
 
44
47
  it 'accepts a year as an integer' do
45
- d = @dates[:with_year]
48
+ d = dates[:with_year]
46
49
  expect(d).to be_a(DateValue)
47
50
  expect(d.year).to eq(1914)
48
51
  end
49
52
 
50
53
  it 'accepts a year as a string' do
51
- d = @dates[:with_year_str]
54
+ d = dates[:with_year_str]
52
55
  expect(d).to be_a(DateValue)
53
56
  expect(d.year).to eq(1914)
54
57
  end
55
58
 
56
59
  it 'accepts a year-month string' do
57
- d = @dates[:with_year_month]
60
+ d = dates[:with_year_month]
58
61
  expect(d).to be_a(DateValue)
59
62
  expect(d.year).to eq(1914)
60
63
  expect(d.month).to eq(8)
61
64
  end
62
65
 
63
66
  it 'accepts an ISO 8601 DateValue string with hours and minutes' do
64
- d = @dates[:iso8601]
67
+ d = dates[:iso8601]
65
68
  expect(d).to be_a(DateValue)
66
69
  expect(d.year).to eq(1914)
67
70
  expect(d.month).to eq(8)
@@ -72,7 +75,7 @@ module Datacite
72
75
  end
73
76
 
74
77
  it 'accepts an ISO 8601 DateValue string with hours, minutes, and seconds' do
75
- d = @dates[:iso8601_secs]
78
+ d = dates[:iso8601_secs]
76
79
  expect(d).to be_a(DateValue)
77
80
  expect(d.year).to eq(1914)
78
81
  expect(d.month).to eq(8)
@@ -84,7 +87,7 @@ module Datacite
84
87
  end
85
88
 
86
89
  it 'accepts an ISO 8601 DateValue string with hours, minutes, seconds, and fractional seconds' do
87
- d = @dates[:iso8601_frac]
90
+ d = dates[:iso8601_frac]
88
91
  expect(d).to be_a(DateValue)
89
92
  expect(d.year).to eq(1914)
90
93
  expect(d.month).to eq(8)
@@ -129,12 +132,21 @@ module Datacite
129
132
  end
130
133
 
131
134
  describe '#to_s' do
132
- it 'includes all relevant fields' do
133
- val = '1914-08-04T11:01:06.0123+01:00'
134
- str = DateValue.new(val).to_s
135
- val.split(/[-T:+.]/).each do |v|
136
- v = 12_300_000.to_s if v == '0123' # special case nanos
137
- expect(str).to include(v)
135
+ it 'returns an ISO-8601 string for all formats' do
136
+ expected = {
137
+ with_date_time: '1914-08-04T11:01:06.0123+01:00',
138
+ with_date: '1914-08-04',
139
+ with_year: '1914',
140
+ with_year_str: '1914',
141
+ with_year_month: '1914-08',
142
+ iso8601: '1914-08-04T11:01+01:00',
143
+ iso8601_secs: '1914-08-04T11:01:06+01:00',
144
+ iso8601_frac: '1914-08-04T11:01:06.0123+01:00'
145
+ }
146
+ dates.each do |format, v|
147
+ actual_str = v.to_s
148
+ expected_str = expected[format]
149
+ expect(actual_str).to eq(expected_str), "expected '#{expected_str}' for #{format}, got '#{actual_str}'"
138
150
  end
139
151
  end
140
152
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacite-mapping
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.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: 2016-09-26 00:00:00.000000000 Z
11
+ date: 2016-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typesafe_enum