class Jiff::DateRange
Implementation of date range relying in ruby dates (no rails helpers/add-ons)
Constants
- VERSION
Attributes
end_date[R]
start_date[R]
Public Class Methods
new(start_date, end_date)
click to toggle source
# File lib/jiff/date_range.rb, line 12 def initialize(start_date, end_date) @start_date = start_date @end_date = end_date end
Public Instance Methods
by_month()
click to toggle source
# File lib/jiff/date_range.rb, line 17 def by_month return month_ends_in_range if date_is_end_of_month(start_date) dates = [] current_date = start_date while current_date <= end_date dates << current_date current_date = next_date(current_date, dates) end dates end
include?(date)
click to toggle source
# File lib/jiff/date_range.rb, line 33 def include?(date) if date.is_a?(Jiff::DateRange) include?(date.start_date) || include?(date.end_date) else date >= start_date && date <= end_date end end
overlap(other_range)
click to toggle source
NOTE: Thank you @chrismytton for the implementation suggestion
# File lib/jiff/date_range.rb, line 49 def overlap(other_range) return unless overlap?(other_range) dates = [start_date, end_date, other_range.start_date, other_range.end_date].sort[1, 2] Jiff::DateRange.new(*dates) end
overlap?(other_range)
click to toggle source
# File lib/jiff/date_range.rb, line 41 def overlap?(other_range) # TODO: Probably should raise 'unsupported type' return unless other_range.is_a? Jiff::DateRange other_range.include?(start_date) || other_range.include?(end_date) || include?(other_range) end
to_a()
click to toggle source
# File lib/jiff/date_range.rb, line 29 def to_a date_range.to_a end
Private Instance Methods
date_is_end_of_month(date)
click to toggle source
# File lib/jiff/date_range.rb, line 82 def date_is_end_of_month(date) date.day == MonthMapper.days_in_month(date) end
date_range()
click to toggle source
# File lib/jiff/date_range.rb, line 78 def date_range (start_date..end_date) end
days_grouped_by_month()
click to toggle source
# File lib/jiff/date_range.rb, line 58 def days_grouped_by_month to_a.group_by { |date| "#{date.month}-#{date.year}" } end
month_ends_in_range()
click to toggle source
# File lib/jiff/date_range.rb, line 62 def month_ends_in_range dates = days_grouped_by_month.values.map(&:last) dates.pop unless date_is_end_of_month(end_date) dates end
next_date(current_date, dates)
click to toggle source
# File lib/jiff/date_range.rb, line 68 def next_date(current_date, dates) # NOTE: If first date is in february, then there is no problem # in jumping forward. if current_date.month == 2 && dates.length > 1 dates[-2] >> 2 else current_date >> 1 end end