class PseudoDate

Constants

VERSION

Attributes

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

Public Class Methods

new(input) click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 4
def initialize(input)
  @date_hash = if input.is_a?(Hash)
    Hash[input.to_hash.map { |k, v| [k.to_s.downcase.to_sym, v] }]
  else
    input = '00000000' if input.to_s.strip == '19000000'
    @date_hash = input.to_s.strip.to_date_hash
  end
  if @date_hash
    @year = @date_hash[:year].to_s.match('1900') ? '0000' : @date_hash[:year].to_s.strip
    @month = @date_hash[:month].to_s.strip
    @day = @date_hash[:day].to_s.strip
  else
    @year = @month = @day = nil
  end
  if @year && @year.match('~') && @year.length == 3
    @year = @year.to_i
    @year = @year > Date.today.year.to_s.gsub('20','').to_i ? "19#{@year}" : "20#{@year}"
  end
  correct_digits!
  @year.to_s.gsub!('~','')
end

Public Instance Methods

<(other) click to toggle source

Comparison Operators =

# File lib/pseudo_date/pseudo_date.rb, line 84
def <(other)
  case self.comparison_mode(other)
  when 'exact'
    self.to_date < other.to_date
  when 'year_month'
    self.year == other.year ? (self.month.to_i < other.month.to_i) : (self.year.to_i < other.year.to_i)
  when 'year'
    self.year.to_i < other.year.to_i
  when 'mixed'
    if self.precision == 'invalid'
      true
    elsif other.precision == 'invalid'
      false
    elsif self.year.to_i == other.year.to_i
      if self.month.to_i == other.month.to_i
        self.day.to_i < other.day.to_i
      else
        self.month.to_i < other.month.to_i
      end
    else
      self.year.to_i < other.year.to_i
    end
  else
    false
  end
end
<=>(other) click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 119
def <=>(other)
  self == other ? 0 : (self < other ? -1 : 1)
end
==(other) click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 115
def ==(other)
  (self.year == other.year) && (self.month == other.month) && (self.day == other.day)
end
>(other) click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 111
def >(other)
  other < self
end
blank?() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 48
def blank?
  (@year.to_s == '0000' && @month.to_s == '00' && @day.to_s == '00')
end
Also aliased as: empty?
empty?()
Alias for: blank?
precision() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 26
def precision
  correct_digits!
  if @year.nil? || (@year.to_s == '0000' && @month.to_s == '00') || (@year.to_s == "8888")
    "invalid"
  elsif self.to_date
    'exact'
  elsif @month != '00' && @day == '00'
    'year_month'
  elsif @month == '00' && @day == '00'
    'year'
  else
    'invalid'
  end
end
present?() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 53
def present?
  !self.blank?
end
to_date() click to toggle source

Export Functions =

# File lib/pseudo_date/pseudo_date.rb, line 60
def to_date
  self.valid? ? Date.parse("#{@year}-#{@month}-#{@day}") : nil rescue nil
end
to_hash() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 77
def to_hash
  @date_hash
end
to_s() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 64
def to_s
  return "" unless self.valid?
  
  case self.precision
  when 'invalid'; ""
  when 'weak_year'; ""
  when 'exact'; "#{month}/#{day}/#{year}"
  when 'year_month'; "#{month}/#{year}"
  when 'year'; year
  else; ''
  end
end
valid?() click to toggle source

Boolean Questions =

# File lib/pseudo_date/pseudo_date.rb, line 44
def valid?
  !(@date_hash.nil? || @date_hash.empty?) && self.present?
end

Protected Instance Methods

comparison_mode(other) click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 125
def comparison_mode(other)
  if self.precision == other.precision
    self.precision
  else
    'mixed'
  end
end

Private Instance Methods

correct_digits!() click to toggle source
# File lib/pseudo_date/pseudo_date.rb, line 135
def correct_digits!
  @year  = '0000' if @year.to_s.strip.length == 0
  @month = '00' if @month.to_s.strip.length == 0
  @day   = '00' if @day.to_s.strip.length == 0
  
  @day   = "0#{@day}" if @day.to_s.length == 1
  @month = "0#{@month}" if @month.to_s.length == 1
  
  %w(day month year).each do |i|
    @date_hash[i.to_sym] = self.send(i)
  end
end