1
0
Fork 0

Merge pull request #15 from Applicat/handle_dates_without_day

Calculate last day of month correctly. refs #14
This commit is contained in:
Gregory Eremin 2013-01-25 05:39:33 -08:00
commit 518ff64265
2 changed files with 54 additions and 5 deletions

View File

@ -46,14 +46,18 @@ module MusicBrainz
elsif type == String elsif type == String
val.to_s val.to_s
elsif type == Date elsif type == Date
if val.nil? or val == "" val = if val.nil? or val == ""
val = "2030-12-31" [2030, 12, 31]
elsif val.split("-").length == 1 elsif val.split("-").length == 1
val << "-12-31" [val.split("-").first.to_i, 12, 31]
elsif val.split("-").length == 2 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 end
Date.new(*val.split(?-).map(&:to_i))
Date.new(*val)
else else
val val
end end

View File

@ -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