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.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ndr_support/daterange.rb', line 19 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.
6 7 8 |
# File 'lib/ndr_support/daterange.rb', line 6 def date1 @date1 end |
#date2 ⇒ Object
Returns the value of attribute date2.
6 7 8 |
# File 'lib/ndr_support/daterange.rb', line 6 def date2 @date2 end |
#source ⇒ Object
Returns the value of attribute source.
6 7 8 |
# File 'lib/ndr_support/daterange.rb', line 6 def source @source end |
Class Method Details
.extract(dates_string) ⇒ Object
10 11 12 |
# File 'lib/ndr_support/daterange.rb', line 10 def self.extract(dates_string) dates_string.to_s.split(',').map { |str| new(str) } end |
.merge(dates_string) ⇒ Object
14 15 16 17 |
# File 'lib/ndr_support/daterange.rb', line 14 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
97 98 99 |
# File 'lib/ndr_support/daterange.rb', line 97 def <=>(other) self.date1 <=> other.date1 end |
#==(other) ⇒ Object
101 102 103 |
# File 'lib/ndr_support/daterange.rb', line 101 def ==(other) self.date1 == other.date1 && self.date2 == other.date2 end |
#empty? ⇒ Boolean
109 110 111 112 |
# File 'lib/ndr_support/daterange.rb', line 109 def empty? # An unspecified date will be empty. A valid or invalid date will not. @date1.nil? && @source.blank? end |
#exact? ⇒ Boolean
114 115 116 |
# File 'lib/ndr_support/daterange.rb', line 114 def exact? @date1 == @date2 end |
#intersects?(other) ⇒ Boolean
105 106 107 |
# File 'lib/ndr_support/daterange.rb', line 105 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
64 65 66 |
# File 'lib/ndr_support/daterange.rb', line 64 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)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ndr_support/daterange.rb', line 47 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/ndr_support/daterange.rb', line 69 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 |