Class: Daterange
- Inherits:
-
Object
- Object
- Daterange
- Defined in:
- lib/ndr_support/daterange.rb
Overview
Our “vague date” class, which can represent a single date or a date range.
Constant Summary collapse
- OKYEARS =
1880..2020
Instance Attribute Summary collapse
-
#date1 ⇒ Object
Returns the value of attribute date1.
-
#date2 ⇒ Object
Returns the value of attribute date2.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
- #exact? ⇒ Boolean
-
#initialize(x1 = nil, x2 = nil) ⇒ Daterange
constructor
A new instance of Daterange.
- #intersects?(other) ⇒ Boolean
-
#to_iso ⇒ Object
used in Address model to_iso output must be SQL safe for security reasons.
-
#to_s ⇒ Object
If we have a valid date range, return a string representation of it TODO: possibly add support for to_s(format) e.g.
-
#verbose ⇒ Object
A long string representation of the date or range.
Constructor Details
#initialize(x1 = nil, x2 = nil) ⇒ Daterange
Returns a new instance of Daterange.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ndr_support/daterange.rb', line 20 def initialize(x1 = nil, x2 = nil) x1 = x1.to_datetime if x1.is_a?(Date) || x1.is_a?(Time) x2 = x2.to_datetime if x2.is_a?(Date) || x2.is_a?(Time) if x1.is_a?(DateTime) && x2.is_a?(DateTime) @date1 = [x1, x2].min @date2 = [x1, x2].max @source = nil elsif x1.is_a?(Daterange) && x2.nil? # Patient model line 645 @date1 = x1.date1 @date2 = x1.date2 @source = x1.source elsif x1.is_a?(DateTime) && x2.nil? @date1 = x1 @date2 = x1 @source = nil elsif x1.is_a?(String) && x2.nil? self.source = (x1) else @date1 = nil @date2 = nil @source = nil end self.freeze end |
Instance Attribute Details
#date1 ⇒ Object
Returns the value of attribute date1.
7 8 9 |
# File 'lib/ndr_support/daterange.rb', line 7 def date1 @date1 end |
#date2 ⇒ Object
Returns the value of attribute date2.
7 8 9 |
# File 'lib/ndr_support/daterange.rb', line 7 def date2 @date2 end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/ndr_support/daterange.rb', line 7 def source @source end |
Class Method Details
.extract(dates_string) ⇒ Object
11 12 13 |
# File 'lib/ndr_support/daterange.rb', line 11 def self.extract(dates_string) dates_string.to_s.split(',').map { |str| new(str) } end |
.merge(dates_string) ⇒ Object
15 16 17 18 |
# File 'lib/ndr_support/daterange.rb', line 15 def self.merge(dates_string) ranges = extract(dates_string) new(ranges.map(&:date1).compact.min, ranges.map(&:date2).compact.max) end |
Instance Method Details
#<=>(other) ⇒ Object
98 99 100 |
# File 'lib/ndr_support/daterange.rb', line 98 def <=>(other) self.date1 <=> other.date1 end |
#==(other) ⇒ Object
102 103 104 105 106 |
# File 'lib/ndr_support/daterange.rb', line 102 def ==(other) date1 == other.date1 && date2 == other.date2 rescue NoMethodError # Comparing to things that don't work like Dateranges, e.g. nil, integer false end |
#empty? ⇒ Boolean
112 113 114 115 |
# File 'lib/ndr_support/daterange.rb', line 112 def empty? # An unspecified date will be empty. A valid or invalid date will not. @date1.nil? && @source.blank? end |
#exact? ⇒ Boolean
117 118 119 |
# File 'lib/ndr_support/daterange.rb', line 117 def exact? @date1 == @date2 end |
#intersects?(other) ⇒ Boolean
108 109 110 |
# File 'lib/ndr_support/daterange.rb', line 108 def intersects?(other) !(self.empty? || other.empty?) && self.date1 <= other.date2 && self.date2 >= other.date1 end |
#to_iso ⇒ Object
used in Address model to_iso output must be SQL safe for security reasons
65 66 67 |
# File 'lib/ndr_support/daterange.rb', line 65 def to_iso date1.is_a?(DateTime) ? date1.to_iso : '' end |
#to_s ⇒ Object
If we have a valid date range, return a string representation of it TODO: possibly add support for to_s(format) e.g. to_s(:short)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ndr_support/daterange.rb', line 48 def to_s return '' unless @date1 && @date2 if @date1 == @date2 # single date tidy_string_if_midnight(@date1) elsif tidy_string_if_midnight(@date1) == tidy_string_if_midnight(@date2.at_beginning_of_year) && tidy_string_if_midnight(@date2) == tidy_string_if_midnight(@date1.at_end_of_year.at_beginning_of_day) # whole year @date1.strftime('%Y') elsif tidy_string_if_midnight(@date1) == tidy_string_if_midnight(@date2.at_beginning_of_month) && tidy_string_if_midnight(@date2) == tidy_string_if_midnight(@date1.at_end_of_month.at_beginning_of_day) # whole month @date1.strftime('%m.%Y') else # range tidy_string_if_midnight(@date1) + ' to ' + tidy_string_if_midnight(@date2) end end |
#verbose ⇒ Object
A long string representation of the date or range
70 71 72 73 74 75 76 77 78 |
# File 'lib/ndr_support/daterange.rb', line 70 def verbose return 'Bad date(s)' unless @date1 && @date2 if @date1 == @date2 # single date _verbose(@date1) else # range 'The period ' + _verbose(@date1) + ' to ' + _verbose(@date2) + ' inclusive (' + (@date2 - @date1 + 1).to_i.to_s + ' days)' end end |