Class: CulturalDates::CulturalDate
- Inherits:
-
Object
- Object
- CulturalDates::CulturalDate
- Includes:
- Comparable
- Defined in:
- lib/cultural_dates/cultural_date.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #earliest ⇒ Object
-
#initialize(val = "") ⇒ CulturalDate
constructor
A new instance of CulturalDate.
- #inspect ⇒ Object
- #known? ⇒ Boolean
- #latest ⇒ Object
- #to_edtf ⇒ Object
- #to_s ⇒ Object
- #unknown? ⇒ Boolean
- #values ⇒ Object
Constructor Details
#initialize(val = "") ⇒ CulturalDate
Returns a new instance of CulturalDate.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cultural_dates/cultural_date.rb', line 40 def initialize(val="") if val begin parse_result = DateParser.new.parse(val) transformed_result = DateTransform.new.apply(parse_result) if transformed_result @value = Date.edtf(transformed_result) end rescue Parslet::ParseFailed => e # puts e @value = nil end else @value = nil end end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/cultural_dates/cultural_date.rb', line 6 def value @value end |
Class Method Details
.edtf(edtf_date) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/cultural_dates/cultural_date.rb', line 13 def edtf(edtf_date) val = CulturalDate.new date = Date.edtf(edtf_date) || EDTF::Unknown.new val.instance_variable_set(:@value, date) val end |
.parse(val) ⇒ Object
9 10 11 |
# File 'lib/cultural_dates/cultural_date.rb', line 9 def parse(val) return CulturalDate.new(val) end |
Instance Method Details
#<=>(other) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cultural_dates/cultural_date.rb', line 25 def <=>(other) case other when CulturalDates::CulturalDate @value <=> other.value when ::Date @value <=> other else @value <=> other end end |
#earliest ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cultural_dates/cultural_date.rb', line 66 def earliest return nil if @value.nil? return @value if @value.instance_of? EDTF::Unknown new_d = EDTF.parse(@value.to_s) if new_d.year < 0 if @value.unspecified.year[2] new_d = new_d.advance(:years =>-99) end end new_d end |
#inspect ⇒ Object
36 37 38 |
# File 'lib/cultural_dates/cultural_date.rb', line 36 def inspect @value.inspect end |
#known? ⇒ Boolean
57 58 59 60 |
# File 'lib/cultural_dates/cultural_date.rb', line 57 def known? return false if @value.instance_of?(EDTF::Unknown) || @value.nil? true end |
#latest ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cultural_dates/cultural_date.rb', line 78 def latest return nil if @value.nil? return @value if @value.instance_of? EDTF::Unknown new_d = @value.clone if new_d.unspecified.year[2] new_d = new_d.advance(:years =>99) if new_d.year >=0 new_d.year_precision! elsif new_d.unspecified.year[3] new_d = new_d.advance(:years =>9) if new_d.year >=0 new_d.year_precision! elsif new_d.unspecified? :day new_d.month_precision! if new_d.unspecified? :month new_d.year_precision! end end new_d = new_d.succ new_d.day_precision! new_d - 1 end |
#to_edtf ⇒ Object
100 101 102 103 |
# File 'lib/cultural_dates/cultural_date.rb', line 100 def to_edtf return nil if @value.nil? return @value.edtf end |
#to_s ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cultural_dates/cultural_date.rb', line 105 def to_s date = @value return nil unless date.is_a? Date str = "" if !date.unspecified? :day str = date.strftime("%B %-d, ") if date.year >=0 year_str = date.year.to_s year_str += " CE" if date.year < 1000 else year_str = "#{-date.year} BCE" end str += year_str elsif !date.unspecified? :month str = date.strftime("%B ") if date.year >=1 year_str = date.year.to_s year_str += " CE" if date.year < 1000 elsif year == 0 year_str = "1 BCE" else year_str = "#{-year} BCE" end str += year_str elsif !date.unspecified? :year if date.year >=1 str = date.year.to_s str += " CE" if date.year < 1000 else str = "#{-date.year+1} BCE" end elsif !date.unspecified.year[2] str = "the #{date.year}s" else bce = false year = (date.year/100+1) if year <= 0 year = -(year-2) bce = true end str = "the #{year.ordinalize} century" str += " CE" if year >= 1 && year < 10 && !bce str += " BCE" if bce str end str += "?" unless date.certain? str end |
#unknown? ⇒ Boolean
62 63 64 |
# File 'lib/cultural_dates/cultural_date.rb', line 62 def unknown? return !self.known? end |
#values ⇒ Object
21 22 23 |
# File 'lib/cultural_dates/cultural_date.rb', line 21 def values @value.values end |