-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path7_10.rb
86 lines (66 loc) · 1.28 KB
/
7_10.rb
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
class Schedule
def scheduled?(schedulable, starting, ending)
# ...
puts "This #{schedulable.class} is " +
"available #{starting} - #{ending}"
false
end
def add(target, starting, ending)
# ...
end
def remove(target, starting, ending)
# ...
end
end
module Schedulable
attr_writer :schedule
def schedule
@schedule ||= Schedule.new
end
def schedulable?(starting, ending)
!scheduled?(starting - lead_days, ending)
end
def scheduled?(starting, ending)
schedule.scheduled?(self, starting, ending)
end
# includers may override
def lead_days
0
end
end
class Bicycle
include Schedulable
def lead_days
1
end
# ...
end
require 'date'
starting = Date.parse("2019/09/04")
ending = Date.parse("2019/09/10")
b = Bicycle.new
puts b.schedulable?(starting, ending)
# => This Bicycle is available 2019-09-03 - 2019-09-10
# => true
class Vehicle
include Schedulable
def lead_days
3
end
# ...
end
class Mechanic
include Schedulable
def lead_days
4
end
# ...
end
v = Vehicle.new
puts v.schedulable?(starting, ending)
# => This Vehicle is available 2019-09-01 - 2019-09-10
# => true
m = Mechanic.new
puts m.schedulable?(starting, ending)
# => This Mechanic is available 2019-08-31 - 2019-09-10
# => true