Class: PseudoDate
Instance Attribute Summary collapse
-
#date_hash ⇒ Object
Returns the value of attribute date_hash.
-
#day ⇒ Object
Returns the value of attribute day.
-
#month ⇒ Object
Returns the value of attribute month.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
-
#<(other) ⇒ Object
= Comparison Operators = ========================.
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
-
#initialize(input) ⇒ PseudoDate
constructor
A new instance of PseudoDate.
- #precision ⇒ Object
-
#to_date ⇒ Object
= Export Functions = ====================.
- #to_hash ⇒ Object
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
= Boolean Questions = =====================.
Constructor Details
#initialize(input) ⇒ PseudoDate
Returns a new instance of PseudoDate.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Attribute Details
#date_hash ⇒ Object
Returns the value of attribute date_hash.
2 3 4 |
# File 'lib/pseudo_date/pseudo_date.rb', line 2 def date_hash @date_hash end |
#day ⇒ Object
Returns the value of attribute day.
2 3 4 |
# File 'lib/pseudo_date/pseudo_date.rb', line 2 def day @day end |
#month ⇒ Object
Returns the value of attribute month.
2 3 4 |
# File 'lib/pseudo_date/pseudo_date.rb', line 2 def month @month end |
#year ⇒ Object
Returns the value of attribute year.
2 3 4 |
# File 'lib/pseudo_date/pseudo_date.rb', line 2 def year @year end |
Instance Method Details
#<(other) ⇒ Object
Comparison Operators =
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/pseudo_date/pseudo_date.rb', line 75 def <(other) case self.comparison_mode(other) when 'exact' self.to_date < other.to_date when 'year_month' self.year == other.year ? (self.month < other.month) : (self.year < other.year) when 'year' self.year < other.year when 'mixed' if self.precision == 'invalid' true elsif other.precision == 'invalid' false elsif self.year == other.year if self.month == other.month self.day.to_i < other.day.to_i else self.month < other.month end else self.year < other.year end else false end end |
#<=>(other) ⇒ Object
110 111 112 |
# File 'lib/pseudo_date/pseudo_date.rb', line 110 def <=>(other) self == other ? 0 : (self < other ? -1 : 1) end |
#==(other) ⇒ Object
106 107 108 |
# File 'lib/pseudo_date/pseudo_date.rb', line 106 def ==(other) (self.year == other.year) && (self.month == other.month) && (self.day == other.day) end |
#>(other) ⇒ Object
102 103 104 |
# File 'lib/pseudo_date/pseudo_date.rb', line 102 def >(other) other < self end |
#precision ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
#to_date ⇒ Object
Export Functions =
51 52 53 |
# File 'lib/pseudo_date/pseudo_date.rb', line 51 def to_date self.valid? ? Date.parse("#{@year}-#{@month}-#{@day}") : nil rescue nil end |
#to_hash ⇒ Object
68 69 70 |
# File 'lib/pseudo_date/pseudo_date.rb', line 68 def to_hash @date_hash end |
#to_s ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/pseudo_date/pseudo_date.rb', line 55 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? ⇒ Boolean
Boolean Questions =
44 45 46 |
# File 'lib/pseudo_date/pseudo_date.rb', line 44 def valid? !(@date_hash.nil? || @date_hash.empty?) end |