Class: Timely::DateRange
- Inherits:
-
Range
- Object
- Range
- Timely::DateRange
show all
- Defined in:
- lib/timely/date_range.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Range
#days_from, #to_date_range
Constructor Details
#initialize(*args) ⇒ DateRange
Returns a new instance of DateRange.
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/timely/date_range.rb', line 5
def initialize(*args)
if args.first.is_a?(Range)
date_range = args.first
DateRange.validate_range(date_range.first, date_range.last)
super(date_range.first.to_date, date_range.last.to_date)
elsif args.size == 1 && args.first.is_a?(Date)
DateRange.validate_range(args.first, args.last)
super(args.first.to_date, args.first.to_date)
else
DateRange.validate_range(args.first, args.last)
super(args.first.to_date, args.last.to_date)
end
end
|
Class Method Details
.from_params(start_date, duration = nil) ⇒ Object
28
29
30
31
32
33
|
# File 'lib/timely/date_range.rb', line 28
def self.from_params(start_date, duration = nil)
start_date = start_date.to_date
duration = [1, duration.to_i].max
new(start_date..(start_date + duration - 1))
end
|
.to_s(first = nil, last = nil, month_fmt = '%b %Y', date_fmt = default_date_format, time_fmt = nil) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/timely/date_range.rb', line 53
def self.to_s(first = nil, last = nil, month_fmt = '%b %Y', date_fmt = default_date_format, time_fmt = nil)
time_fmt ||= date_fmt + ' %H:%M'
is_date = first.is_a?(Date) || last.is_a?(Date)
fmt = is_date ? date_fmt : time_fmt
if first && last
if first == last
first.strftime(fmt)
elsif first == first.at_beginning_of_month && last == last.at_end_of_month
if first.year == last.year && first.month == last.month
first.strftime(month_fmt)
else
"#{first.strftime(month_fmt)} to #{last.strftime(month_fmt)}"
end
else
"#{first.strftime(fmt)} to #{last.strftime(fmt)}#{' (inclusive)' if is_date}"
end
elsif first
"on or after #{first.strftime(fmt)}"
elsif last
"on or before #{last.strftime(fmt)}"
else
'no date range'
end
end
|
.validate_range(first, last) ⇒ Object
21
22
23
24
25
26
|
# File 'lib/timely/date_range.rb', line 21
def self.validate_range(first, last)
raise ArgumentError, 'Date range missing start date' if first.nil?
raise ArgumentError, 'Date range missing end date' if last.nil?
raise ArgumentError, 'Start date is not a date' unless first.is_a? Date
raise ArgumentError, 'End date is not a date' unless last.is_a? Date
end
|
Instance Method Details
#intersecting_dates(date_range) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/timely/date_range.rb', line 35
def intersecting_dates(date_range)
r_start = [start_date, date_range.first].max
r_end = [end_date, date_range.last].min
return [] if r_end < r_start
r_start..r_end
end
|
#number_of_nights ⇒ Object
Also known as:
duration
44
45
46
|
# File 'lib/timely/date_range.rb', line 44
def number_of_nights
((last - first) + 1).to_i
end
|
#to_s(fmt = '%b %Y', date_fmt = '%Y-%m-%d') ⇒ Object
49
50
51
|
# File 'lib/timely/date_range.rb', line 49
def to_s(fmt = '%b %Y', date_fmt = '%Y-%m-%d')
Timely::DateRange.to_s(first, last, fmt, date_fmt)
end
|