Merge pull request #15 from Applicat/handle_dates_without_day
Calculate last day of month correctly. refs #14
This commit is contained in:
commit
518ff64265
|
@ -46,14 +46,18 @@ module MusicBrainz
|
|||
elsif type == String
|
||||
val.to_s
|
||||
elsif type == Date
|
||||
if val.nil? or val == ""
|
||||
val = "2030-12-31"
|
||||
val = if val.nil? or val == ""
|
||||
[2030, 12, 31]
|
||||
elsif val.split("-").length == 1
|
||||
val << "-12-31"
|
||||
[val.split("-").first.to_i, 12, 31]
|
||||
elsif val.split("-").length == 2
|
||||
val << "-31"
|
||||
val = val.split("-").map(&:to_i)
|
||||
[val.first, val.last, -1]
|
||||
else
|
||||
val.split("-").map(&:to_i)
|
||||
end
|
||||
Date.new(*val.split(?-).map(&:to_i))
|
||||
|
||||
Date.new(*val)
|
||||
else
|
||||
val
|
||||
end
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
|
||||
require "spec_helper"
|
||||
|
||||
describe MusicBrainz::BaseModel do
|
||||
describe '#validate_type' do
|
||||
describe 'Date' do
|
||||
context 'nil value' do
|
||||
it 'returns 2030-12-31' do
|
||||
response = '<release-group><first-release-date></first-release-date></release-group>'
|
||||
xml = Nokogiri::XML.parse(response)
|
||||
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
|
||||
release_group.first_release_date.should == Date.new(2030, 12, 31)
|
||||
end
|
||||
end
|
||||
|
||||
context 'year only' do
|
||||
it 'returns 1995-12-31' do
|
||||
response = '<release-group><first-release-date>1995</first-release-date></release-group>'
|
||||
xml = Nokogiri::XML.parse(response)
|
||||
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
|
||||
release_group.first_release_date.should == Date.new(1995, 12, 31)
|
||||
end
|
||||
end
|
||||
|
||||
context 'year and month only' do
|
||||
it 'returns 1995-04-30' do
|
||||
response = '<release-group><first-release-date>1995-04</first-release-date></release-group>'
|
||||
xml = Nokogiri::XML.parse(response)
|
||||
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
|
||||
release_group.first_release_date.should == Date.new(1995, 4, 30)
|
||||
end
|
||||
end
|
||||
|
||||
context 'year, month and day' do
|
||||
it 'returns 1995-04-30' do
|
||||
response = '<release-group><first-release-date>1995-04-30</first-release-date></release-group>'
|
||||
xml = Nokogiri::XML.parse(response)
|
||||
release_group = MusicBrainz::ReleaseGroup.new MusicBrainz::Bindings::ReleaseGroup.parse(xml)
|
||||
release_group.first_release_date.should == Date.new(1995, 4, 30)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue