Class: PartialDate

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchworks/partial_date.rb

Overview

Date representation that permits nil values We need this because of the nature of date values for works Oftentimes a year is known, but a month, or day is not.

Defined Under Namespace

Classes: PartialDateArgError, PartialDateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ PartialDate

We accept three formats, Date, Time (which we drop down to a Date), and an array of up three integers in the format: [year, month, day] TODO: Validate array input



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fetchworks/partial_date.rb', line 35

def initialize(arg)
  if arg.is_a? Time
    arg = arg.to_date
  end

  if arg.is_a? Date
    arg = [arg.year, arg.month, arg.day]
  end

  unless arg.is_a? Array
    raise PartialDateArgError, "Did not receive a Date, Time, or Array"
  end

  unless arg.length.between?(1, 3)
    raise PartialDateArgError, "Received array of incorrect length."
  end

  @year = arg[0]
  @month = arg[1]
  @day = arg[2]
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



11
12
13
# File 'lib/fetchworks/partial_date.rb', line 11

def day
  @day
end

#monthObject

Returns the value of attribute month.



11
12
13
# File 'lib/fetchworks/partial_date.rb', line 11

def month
  @month
end

#yearObject

Returns the value of attribute year.



11
12
13
# File 'lib/fetchworks/partial_date.rb', line 11

def year
  @year
end

Instance Method Details

#<=>(other) ⇒ Object



21
22
23
# File 'lib/fetchworks/partial_date.rb', line 21

def <=>(other)
  to_date <=> other.to_date
end

#==(other) ⇒ Object



25
26
27
28
29
30
# File 'lib/fetchworks/partial_date.rb', line 25

def ==(other)
  self.class == other.class &&
    @year == other.year &&
    @month == other.month &&
    @day == other.day
end

#to_dateObject



13
14
15
# File 'lib/fetchworks/partial_date.rb', line 13

def to_date
  Date.new(year, (month || 1), (day || 1))
end

#to_timeObject



17
18
19
# File 'lib/fetchworks/partial_date.rb', line 17

def to_time
  date.to_time
end