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/nil_date.rb, line 16 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/nil_date.rb, line 46 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 MetalArchives::NilDate.new year, month, day rescue StandardError => e raise MetalArchives::Errors::ArgumentError, "Invalid date: #{value}: #{e}" end
Public Instance Methods
<=>(other)
click to toggle source
Comparison operator
# File lib/metal_archives/nil_date.rb, line 66 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 @day <=> other.day else comp_month end else comp_year end end
date()
click to toggle source
Return a Date
object
# File lib/metal_archives/nil_date.rb, line 37 def date raise MetalArchives::Errors::ArgumentError, "Invalid conversion to Date: #{self}" unless year? ::Date.new @year, month || 1, day || 1 end
day?()
click to toggle source
# File lib/metal_archives/nil_date.rb, line 30 def day? !@day.nil? end
month?()
click to toggle source
# File lib/metal_archives/nil_date.rb, line 26 def month? !@month.nil? end
year?()
click to toggle source
# File lib/metal_archives/nil_date.rb, line 22 def year? !@year.nil? end