1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
module Mhc
module DateFrame
class Dummy
def initialize(start_date, interval = 1, wkst = 1)
end
end
class Base
include DateHelper
def initialize(start_date, interval = 1, wkst = 1)
@start_date, @interval, @wkst = start_date, interval, wkst
rewind
end
def each
loop do
date = self.next
yield date
end
end
def next(cycles = 1)
frame = @frame_start
@frame_start = next_frame_start(cycles)
return frame
end
def peek
@frame_start
end
def rewind
@frame_start = beginning_of_frame(@start_date)
return self
end
# go forward to the frame in which DATE is involved
def forward_to(date)
rewind
frames = frames_between(@frame_start, date)
cycles = (frames + (@interval - 1)) / @interval
self.next(cycles) if cycles > 0
return self
end
private
def next_frame_start(cycles = 1)
raise "should be defined in subclasses"
end
def beginning_of_frame(date)
raise "should be defined in subclasses"
end
def frames_between(date1, date2)
raise "should be defined in subclasses"
end
end
class Yearly < Base
private
def next_frame_start(cycles = 1)
@frame_start >> (@interval * 12 * cycles)
end
def beginning_of_frame(date)
beginning_of_year(date)
end
def frames_between(date1, date2)
years_between(date1, date2)
end
end
class Monthly < Base
private
def next_frame_start(cycles = 1)
@frame_start >> (@interval * cycles)
end
def beginning_of_frame(date)
beginning_of_month(date)
end
def frames_between(date1, date2)
months_between(date1, date2)
end
end
class Weekly < Base
private
def next_frame_start(cycles = 1)
@frame_start + (@interval * 7 * cycles)
end
def beginning_of_frame(date)
beginning_of_week(date, @wkst)
end
def frames_between(date1, date2)
(beginning_of_frame(date2) - beginning_of_frame(date1)) / 7
end
end
class Daily < Base
private
def next_frame_start(cycles = 1)
@frame_start + (@interval * cycles)
end
def beginning_of_frame(date)
date
end
def frames_between(date1, date2)
date2 - date1
end
end
end # module DateFrame
end # module Mhc
|