Class: Reservation::Schedule::Weekly
- Inherits:
-
Object
- Object
- Reservation::Schedule::Weekly
- Defined in:
- lib/reservation/weekly.rb
Overview
Weekly defines a set of intervals that recur weekly
This class maintains an array of weekdays with an interval for each
This class will never match multi-day events, as it assumes each event starts and ends within the same day
Instance Attribute Summary collapse
-
#wdays ⇒ Object
wdays is an array of Daily instances.
Instance Method Summary collapse
-
#filter(events) ⇒ Object
return a new list including only those events in the given list that match this weekly schedule.
-
#generate(from, upto) ⇒ Object
generate a set of Events according to this Weekly schedule, starting on #from at the earliest, ending on #upto at the latest.
-
#initialize(patterns) ⇒ Weekly
constructor
pattern is an array of the form.
-
#matches?(event) ⇒ Boolean
true if there exists a Daily corresponding to this event’s weekday, with an Interval corresponding to this event’s start and finish times; false otherwise.
- #parse_nth_of_month(txt) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(patterns) ⇒ Weekly
pattern is an array of the form
[ { "nth_of_month" => "all", "day" => "wed", "start" => "0930", "finish" => "10:30"},
{ "nth_of_month" => "1", "day" => "wed", "start" => "18", "finish" => "20" },
{ "nth_of_month" => "-1", "day" => "tue", "start" => "7", "finish" => "8h30" } ]
see HourMinute#parse for how “start” and “finish” values are read
- “nth_of_month” => “all”, “day” => “mon”
-
all mondays in the month are considered
- “nth_of_month” => “1”, “day” => “fri”
-
only the first friday of the month is considered
- “nth_of_month” => “-1”, “day” => “tue”
-
only the last tuesday of the month is considered
- “nth_of_month” => “-2”, “day” => “sat”
-
only the second-last saturday of the month is considered
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/reservation/weekly.rb', line 28 def initialize patterns self.wdays = [] patterns.each { |pattern| day = pattern["day"] dayno = (day =~ /\d+/) ? day.to_i : DAY_MAP[day] nth_of_month = parse_nth_of_month pattern["nth_of_month"] if pattern["intervals"] intervals = Interval.parse(pattern["intervals"]) self.wdays.concat intervals.map { |i| Daily.new(dayno, nth_of_month, i) } else start = HourMinute.parse pattern["start"] finish = HourMinute.parse pattern["finish"] self.wdays << Daily.new(dayno, nth_of_month, Interval.new(start, finish)) end } end |
Instance Attribute Details
#wdays ⇒ Object
wdays is an array of Daily instances
13 14 15 |
# File 'lib/reservation/weekly.rb', line 13 def wdays @wdays end |
Instance Method Details
#filter(events) ⇒ Object
return a new list including only those events in the given list that match this weekly schedule
77 78 79 |
# File 'lib/reservation/weekly.rb', line 77 def filter events events.select { |e| matches? e } end |
#generate(from, upto) ⇒ Object
generate a set of Events according to this Weekly schedule, starting on #from at the earliest, ending on #upto at the latest.
Note: #upto is inclusive, this will generate events on #upto, if the schedule allows
65 66 67 68 69 70 71 |
# File 'lib/reservation/weekly.rb', line 65 def generate from, upto events = [] from.upto(upto).map { |date| wdays.each { |day| day.generate date, events } } events end |
#matches?(event) ⇒ Boolean
true if there exists a Daily corresponding to this event’s weekday, with an Interval corresponding to this event’s start and finish times; false otherwise
52 53 54 55 56 |
# File 'lib/reservation/weekly.rb', line 52 def matches? event return false if event.start.day != event.finish.day self.wdays.each { |wday| return true if wday.matches?(event) } false end |
#parse_nth_of_month(txt) ⇒ Object
85 86 87 |
# File 'lib/reservation/weekly.rb', line 85 def parse_nth_of_month txt (txt.to_s == '' || txt.to_s == 'all') ? :all : txt.to_i end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/reservation/weekly.rb', line 81 def to_s wdays.map(&:to_s).join "\n" end |