Class: DateRangeCovers::DateRange
- Inherits:
-
Object
- Object
- DateRangeCovers::DateRange
- Defined in:
- lib/date_range_covers.rb
Instance Attribute Summary collapse
-
#beginning_of_week_day ⇒ Object
readonly
Returns the value of attribute beginning_of_week_day.
-
#covs ⇒ Object
readonly
Returns the value of attribute covs.
-
#end_date ⇒ Object
readonly
Returns the value of attribute end_date.
-
#start_date ⇒ Object
readonly
Returns the value of attribute start_date.
Instance Method Summary collapse
-
#covers(cover_options = [:all]) ⇒ Object
- :days, :weeks
-
> same as [:weeks] [:days, :months] => same as [:months] [:weeks, :months] => same as [:all].
-
#dates_covered(options = {}) ⇒ dates
dates_covered.
-
#initialize(start_date, end_date, bow_day = :monday) ⇒ DateRange
constructor
A new instance of DateRange.
-
#is_date_range? ⇒ Boolean
Is the date range created an actual date range.
-
#is_single_date_range? ⇒ Boolean
Is the date range created, a single date range.
-
#months_covered(options = {}) ⇒ starting date of months
months_covered.
-
#weeks_covered(options = {}) ⇒ starting date of weeks
weeks_covered.
Constructor Details
#initialize(start_date, end_date, bow_day = :monday) ⇒ DateRange
Returns a new instance of DateRange.
11 12 13 14 15 16 |
# File 'lib/date_range_covers.rb', line 11 def initialize(start_date, end_date, bow_day = :monday) @start_date = start_date @end_date = end_date @beginning_of_week_day = bow_day @covs = {} end |
Instance Attribute Details
#beginning_of_week_day ⇒ Object (readonly)
Returns the value of attribute beginning_of_week_day.
8 9 10 |
# File 'lib/date_range_covers.rb', line 8 def beginning_of_week_day @beginning_of_week_day end |
#covs ⇒ Object (readonly)
Returns the value of attribute covs.
9 10 11 |
# File 'lib/date_range_covers.rb', line 9 def covs @covs end |
#end_date ⇒ Object (readonly)
Returns the value of attribute end_date.
7 8 9 |
# File 'lib/date_range_covers.rb', line 7 def end_date @end_date end |
#start_date ⇒ Object (readonly)
Returns the value of attribute start_date.
7 8 9 |
# File 'lib/date_range_covers.rb', line 7 def start_date @start_date end |
Instance Method Details
#covers(cover_options = [:all]) ⇒ Object
- :days, :weeks
-
> same as [:weeks]
- :days, :months
-
> same as [:months]
- :weeks, :months
-
> same as [:all]
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/date_range_covers.rb', line 256 def covers( = [:all]) @covs = {} if .include?(:all) months_covered weeks_covered dates_covered(:include => :both) else months_covered if .include?(:months) weeks_covered if .include?(:weeks) dates_covered(:include => :both) end return covs end |
#dates_covered(options = {}) ⇒ dates
dates_covered
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/date_range_covers.rb', line 194 def dates_covered(={}) s_date = start_date.dup e_date = end_date.dup if is_single_date_range? dates = handle_single_date_range(s_date, e_date, ) else dates = handle_date_range(s_date, e_date, ) end covs[:days] = dates.compact.sort end |
#is_date_range? ⇒ Boolean
Is the date range created an actual date range
42 43 44 |
# File 'lib/date_range_covers.rb', line 42 def is_date_range? (start_date and end_date and !is_single_date_range?) ? true : false end |
#is_single_date_range? ⇒ Boolean
Is the date range created, a single date range
28 29 30 |
# File 'lib/date_range_covers.rb', line 28 def is_single_date_range? (start_date and end_date and start_date == end_date) ? true : false end |
#months_covered(options = {}) ⇒ starting date of months
months_covered
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/date_range_covers.rb', line 60 def months_covered(={}) s_date = start_date.dup e_date = end_date.dup months = [] unless .empty? case [:include] when :both months << s_date.beginning_of_month months << e_date.beginning_of_month when :start_month months << s_date.beginning_of_month when :end_month months << e_date.beginning_of_month end end #We need to iterate through every month and #see if the month is wholly contained in the #date range bottom_of_month = s_date.beginning_of_month top_of_month = s_date.end_of_month while bottom_of_month < e_date do if (s_date..e_date).include?(bottom_of_month..top_of_month) months << bottom_of_month end bottom_of_month = bottom_of_month.next_month top_of_month = bottom_of_month.end_of_month end covs[:months] = months.compact.uniq.sort end |
#weeks_covered(options = {}) ⇒ starting date of weeks
weeks_covered
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/date_range_covers.rb', line 133 def weeks_covered(={}) s_date = start_date.dup e_date = end_date.dup weeks = [] unless .empty? case [:include] when :both s_date = s_date.beginning_of_week(beginning_of_week_day) e_date = e_date.end_of_week(beginning_of_week_day) start_of_e_week = e_date.beginning_of_week(beginning_of_week_day) weeks << s_date unless is_covered?(s_date) weeks << start_of_e_week unless is_covered?(start_of_e_week) when :start_week s_date = s_date.beginning_of_week(beginning_of_week_day) weeks << s_date unless is_covered?(s_date) when :end_week e_date = e_date.end_of_week(beginning_of_week_day) start_of_e_week = e_date.beginning_of_week(beginning_of_week_day) weeks << start_of_e_week unless is_covered?(start_of_e_week) end end #We need to iterate through every week and #see if the week is wholly contained in the #date range bottom_of_week = s_date.beginning_of_week(beginning_of_week_day) top_of_week = s_date.end_of_week(beginning_of_week_day) while bottom_of_week < e_date do if (s_date..e_date).include?(bottom_of_week..top_of_week)\ and !date_range_flows_into_selected_months?(bottom_of_week, top_of_week) # if part of a week already belongs to a covered month, we should not add that week to the list weeks << bottom_of_week unless is_covered?(bottom_of_week) end bottom_of_week = bottom_of_week.next_week(beginning_of_week_day) top_of_week = bottom_of_week.end_of_week(beginning_of_week_day) end covs[:weeks] = weeks.compact.uniq.sort end |