Class: MusicBrainz::Model::IncompleteDate

Inherits:
Range
  • Object
show all
Defined in:
lib/rbrainz/model/incomplete_date.rb

Overview

Represents an incomplete date. An incomplete date is a date which can be defined without day or without month and day. It can be written as YYYY, YYYY-MM or YYYY-MM-DD.

An IncompleteDate is a Range of Date objects. The incomplete date 1969-01 for example results in a range beginning on 1969-01-01 and ending on 1969-01-31, including the end.

RBrainz extends the Ruby Range class with additional comparison methods. See CoreExtensions::Range::Equality for a description of those methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CoreExtensions::Range::Equality

#<=, #>=, #after?, #before?, #between?, #contains?, #during?, #finished_by?, #finishes?, #meets_beginning_of?, #meets_end_of?, #overlaps_beginning_of?, #overlaps_end_of?, #started_by?, #starts?

Constructor Details

#initialize(date) ⇒ IncompleteDate

Create a new IncompleteDate. The parameter date must be a String in the form YYYY, YYYY-MM or YYYY-MM-DD.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rbrainz/model/incomplete_date.rb', line 29

def initialize(date)
  date = date.to_s if date.respond_to? :to_s
  if date =~ /^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$/
    @year = $1.to_i
    @month = $2 ? $2.to_i : nil
    @day = $3 ? $3.to_i : nil
    if @month
      if @day
        start_d = Date.civil( @year, @month, @day)
        end_d = start_d
      else
        start_d = Date.civil( @year, @month)
        end_d = Date.civil( @year, @month, -1)
      end
    else
      start_d = Date.civil( @year)
      end_d = Date.civil( @year, -1, -1)
    end
    super( start_d, end_d)
  else
    raise ArgumentError, "Invalid incomplete date #{date}"
  end
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



25
26
27
# File 'lib/rbrainz/model/incomplete_date.rb', line 25

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month.



25
26
27
# File 'lib/rbrainz/model/incomplete_date.rb', line 25

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



25
26
27
# File 'lib/rbrainz/model/incomplete_date.rb', line 25

def year
  @year
end

Instance Method Details

#eql?(b) ⇒ Boolean Also known as: ==

Compare two IncompleteDate objects for equality.

You can compare an IncompleteDate with another IncompleteDate, with a Range of Date objects or directly with a Date. The following examples all return true:

IncompleteDate.new('1969-01-05').eql?( IncompleteDate.new('1969-01-05') )
IncompleteDate.new('1969-01-05').eql?( Date.civil(1969, 1, 5) )
IncompleteDate.new('1969-01').eql?( Date.civil(1969, 1)..Date.civil(1969, 1, 31) )
IncompleteDate.new('1969-01').eql?( Date.civil(1969, 1)...Date.civil(1969, 2, 1) )

Please note that comparing an IncompleteDate with something else than a IncompleteDate is normally not symmetric.

Returns:

  • (Boolean)


78
79
80
81
82
83
84
# File 'lib/rbrainz/model/incomplete_date.rb', line 78

def eql?(b)
  if b.kind_of? ::Range
    self.begin == b.begin && self.open_end == b.open_end
  else
    self.begin == b && self.open_end == b.succ
  end
end

#include?(b) ⇒ Boolean

Returns true if b is completely included in this IncompleteDate. Unlike CoreExtensions::Range::Equality#contains? include? allows equality for begin and end.

Returns:

  • (Boolean)


90
91
92
# File 'lib/rbrainz/model/incomplete_date.rb', line 90

def include?(b)
  self.started_by?(b) || self.contains?(b) || self.eql?(b) || self.finished_by?(b)
end

#to_sObject

Returns the incomplete date in its textual form YYYY, YYYY-MM or YYYY-MM-DD.

TODO: Allow formatting options similiar to Date.to_s



57
58
59
60
61
62
63
# File 'lib/rbrainz/model/incomplete_date.rb', line 57

def to_s
  date = @year.to_s
  [@month, @day].each {|value|
    date += '-%02d' % value if value
  }
  return date
end