class MetalArchives::NilDate

Date with nullable year, month and day

WARNING: No validation on actual date is performed

Attributes

day[RW]
month[RW]
year[RW]

Public Class Methods

new(year = nil, month = nil, day = nil) click to toggle source
# File lib/metal_archives/utils/nil_date.rb, line 15
def initialize(year = nil, month = nil, day = nil)
  @year = year
  @month = month
  @day = day
end
parse(value) click to toggle source

Parse YYYY[-MM]

# File lib/metal_archives/utils/nil_date.rb, line 45
def self.parse(value)
  split = value.split('-')

  year = Integer(split[0], 10) if split.any? && split[0] && !split[0].empty?
  year = nil if year == 0

  month = Integer(split[1], 10) if split.length > 1 && split[1] && !split[1].empty?
  month = nil if month == 0

  day = Integer(split[2], 10) if split.length > 2 && split[2] && !split[2].empty?
  day = nil if day == 0

  return MetalArchives::NilDate.new year, month, day
rescue => e
  raise MetalArchives::Errors::ArgumentError, "Invalid date: #{value}: #{e}"
end

Public Instance Methods

<=>(other) click to toggle source

Comparison operator

# File lib/metal_archives/utils/nil_date.rb, line 73
def <=>(other)
  return nil if other.nil?

  # Return nil if one of the two years is nil
  return nil if (@year.nil? && !other.year.nil?) || !@year.nil? && other.year.nil?

  # Return nil if one of the two months is nil
  return nil if (@month.nil? && !other.month.nil?) || !@month.nil? && other.month.nil?

  # Return nil if one of the two months is nil
  return nil if (@day.nil? && !other.day.nil?) || !@day.nil? && other.day.nil?

  comp_year = @year <=> other.year
  if comp_year == 0
    comp_month = @month <=> other.month
    if comp_month == 0
      return @day <=> other.day
    else
      return comp_month
    end
  else
    comp_year
  end
end
date() click to toggle source

Return a Date object

# File lib/metal_archives/utils/nil_date.rb, line 36
def date
  raise MetalArchives::Errors::ArgumentError, "Invalid conversion to Date: #{self.to_s}" unless year?

  ::Date.new @year, month || 1, day || 1
end
day?() click to toggle source
# File lib/metal_archives/utils/nil_date.rb, line 29
def day?
  !@day.nil?
end
month?() click to toggle source
# File lib/metal_archives/utils/nil_date.rb, line 25
def month?
  !@month.nil?
end
to_s() click to toggle source
# File lib/metal_archives/utils/nil_date.rb, line 62
def to_s
  year = (@year || 0).to_s.rjust 2, '0'
  month = (@month || 0).to_s.rjust 2, '0'
  day = (@day || 0).to_s.rjust 2, '0'

  "#{year}-#{month}-#{day}"
end
year?() click to toggle source
# File lib/metal_archives/utils/nil_date.rb, line 21
def year?
  !@year.nil?
end