Class: DR::DateRange

Inherits:
Object
  • Object
show all
Extended by:
DateOutput, DateRangeParser
Defined in:
lib/dr/parse/date_parse.rb

Constant Summary

Constants included from DateOutput

DR::DateOutput::Months_end, DR::DateOutput::Months_names

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DateRangeParser

parse

Methods included from DateOutput

output_date, split_date, to_time

Constructor Details

#initialize(d) ⇒ DateRange

Returns a new instance of DateRange.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dr/parse/date_parse.rb', line 108

def initialize(d)
  @d=d
  @t=d.map do |range|
    case range.length
    when 1
      [DateRange.to_time(range[0], complete_date: :first),
      DateRange.to_time(range[0], complete_date: :last)]
    when 2
      [DateRange.to_time(range[0], complete_date: :first),
      DateRange.to_time(range[1], complete_date: :last)]
    else
      range.map {|i| DateRange.to_time(i)}
    end
  end
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



107
108
109
# File 'lib/dr/parse/date_parse.rb', line 107

def d
  @d
end

#tObject

Returns the value of attribute t.



107
108
109
# File 'lib/dr/parse/date_parse.rb', line 107

def t
  @t
end

Instance Method Details

#<=>(d2, sort_date_by: :last, **_opts) ⇒ Object

sort_date_by :first or :last



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dr/parse/date_parse.rb', line 125

def <=>(d2, sort_date_by: :last,**_opts)
  d1=@t; d2=d2.t
  sel=lambda do |d|
    case sort_date_by
    when :last
      return d.map {|i| i.last}
    when :first
      return d.map {|i| i.first}
    end
  end
  best=lambda do |d|
    case sort_date_by
    when :last
      return d.max
    when :first
      return d.min
    end
  end
  b1=best.call(sel.call(d1))
  b2=best.call(sel.call(d2))
  return b1 <=> b2
end

#to_s(join: ", ", range_join: " – ", **opts) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/dr/parse/date_parse.rb', line 148

def to_s(join: ", ", range_join: " – ", **opts)
  r=@d.map do |range|
    range.map do |d|
      DateRange.output_date(d,**opts)
    end.join(range_join)
  end.join(join)
  r.empty? ? nil : r
end