Class: PseudoDate
- Defined in:
- lib/pseudo_date/version.rb,
lib/pseudo_date/pseudo_date.rb
Constant Summary collapse
- VERSION =
"0.1.6"
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
- #blank? ⇒ Boolean (also: #empty?)
-
#initialize(input) ⇒ PseudoDate
constructor
A new instance of PseudoDate.
- #precision ⇒ Object
- #present? ⇒ Boolean
-
#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 =
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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 < 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
119 120 121 |
# File 'lib/pseudo_date/pseudo_date.rb', line 119 def <=>(other) self == other ? 0 : (self < other ? -1 : 1) end |
#==(other) ⇒ Object
115 116 117 |
# 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) ⇒ Object
111 112 113 |
# File 'lib/pseudo_date/pseudo_date.rb', line 111 def >(other) other < self end |
#blank? ⇒ Boolean Also known as: empty?
48 49 50 |
# File 'lib/pseudo_date/pseudo_date.rb', line 48 def blank? (@year.to_s == '0000' && @month.to_s == '00' && @day.to_s == '00') 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 |
#present? ⇒ Boolean
53 54 55 |
# File 'lib/pseudo_date/pseudo_date.rb', line 53 def present? !self.blank? end |
#to_date ⇒ Object
Export Functions =
60 61 62 |
# 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 ⇒ Object
77 78 79 |
# File 'lib/pseudo_date/pseudo_date.rb', line 77 def to_hash @date_hash end |
#to_s ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# 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? ⇒ Boolean
Boolean Questions =
44 45 46 |
# File 'lib/pseudo_date/pseudo_date.rb', line 44 def valid? !(@date_hash.nil? || @date_hash.empty?) && self.present? end |