-
Notifications
You must be signed in to change notification settings - Fork 43
/
8_05.rb
103 lines (79 loc) · 1.65 KB
/
8_05.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
class Bicycle
attr_reader :size, :parts
def initialize(size:, parts:)
@size = size
@parts = parts
end
def spares
parts.spares
end
end
class Parts
attr_reader :chain, :tire_size
def initialize(**opts)
@chain = opts[:chain] || default_chain
@tire_size = opts[:tire_size] || default_tire_size
post_initialize(opts)
end
def spares
{ chain: chain,
tire_size: tire_size}.merge(local_spares)
end
def default_tire_size
raise NotImplementedError
end
# subclasses may override
def post_initialize(opts)
nil
end
def local_spares
{}
end
def default_chain
"11-speed"
end
end
class RoadBikeParts < Parts
attr_reader :tape_color
def post_initialize(**opts)
@tape_color = opts[:tape_color]
end
def local_spares
{tape_color: tape_color}
end
def default_tire_size
"23"
end
end
class MountainBikeParts < Parts
attr_reader :front_shock, :rear_shock
def post_initialize(**opts)
@front_shock = opts[:front_shock]
@rear_shock = opts[:rear_shock]
end
def local_spares
{front_shock: front_shock}
end
def default_tire_size
"2.1"
end
end
road_bike =
Bicycle.new(
size: "L",
parts: RoadBikeParts.new(tape_color: "red"))
puts road_bike.size
# => L
puts road_bike.spares
# => {:chain=>"11-speed", :tire_size=>"23", :tape_color=>"red"}
mountain_bike =
Bicycle.new(
size: "L",
parts: MountainBikeParts.new(
front_shock: 'Manitou',
rear_shock: "Fox")
)
puts mountain_bike.size
# => L
puts mountain_bike.spares
# => {:chain=>"11-speed", :tire_size=>"2.1", :front_shock=>"Manitou"}