Class: DateRangeCovers::DateRange

Inherits:
Object
  • Object
show all
Defined in:
lib/date_range_covers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dayObject (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

#covsObject (readonly)

Returns the value of attribute covs.



9
10
11
# File 'lib/date_range_covers.rb', line 9

def covs
  @covs
end

#end_dateObject (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_dateObject (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]

Returns:

  • Hash



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(cover_options = [:all])
  @covs = {}

  if cover_options.include?(:all)
    months_covered
    weeks_covered
    dates_covered(:include => :both)
  else
    months_covered if cover_options.include?(:months)
    weeks_covered if cover_options.include?(:weeks)
    dates_covered(:include => :both)
  end

  return covs
end

#dates_covered(options = {}) ⇒ dates

dates_covered

Examples:

dates = DateRangeCovers::DateRange.dates_covered(options)

Returns:

  • (dates)


194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/date_range_covers.rb', line 194

def dates_covered(options={})
  s_date = start_date.dup
  e_date = end_date.dup
 
  if is_single_date_range?
    dates = handle_single_date_range(s_date, e_date, options)
  else
    dates = handle_date_range(s_date, e_date, options)
  end

  covs[:days] = dates.compact.sort
end

#is_date_range?Boolean

Is the date range created an actual date range

Examples:

boolean = DateRangeCovers::DateRange.is_date_range?

Returns:

  • (Boolean)

    boolean true: when start date and end date are different false: when start date and end date are the same or either date is nil



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

Examples:

boolean = DateRangeCovers::DateRange.is_single_date_range?

Returns:

  • (Boolean)

    boolean true: when start date and end date are the same false: when start date and end date are different or either date is nil



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

Examples:

months = DateRangeCovers::DateRange.months_covered(options)

Returns:

  • (starting date of months)


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(options={})
  s_date = start_date.dup
  e_date = end_date.dup
  
  months = []

  unless options.empty?
    case options[: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

Examples:

weeks = DateRangeCovers::DateRange.weeks_covered(options)

Returns:

  • (starting date of weeks)


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(options={})
  s_date = start_date.dup
  e_date = end_date.dup

  weeks = []

  unless options.empty?
    case options[: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