forked from emilybache/GildedRose-Refactoring-Kata
-
Notifications
You must be signed in to change notification settings - Fork 16
/
gilded_rose_refactored_2.rb
151 lines (117 loc) · 2.6 KB
/
gilded_rose_refactored_2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
class GildedRose
def initialize(items)
@items = items
end
def update_quality()
@items.each do |item|
ItemMapper.update(item)
end
end
end
class Item
attr_accessor :name, :sell_in, :quality
def initialize(name, sell_in, quality)
@name = name
@sell_in = sell_in
@quality = quality
end
def to_s()
"#{@name}, #{@sell_in}, #{@quality}"
end
end
class BaseItemHandler
attr_reader :item
def initialize(item)
@item = item
end
def update
update_quality
update_sell_in
end
private
def update_quality
end
def update_sell_in
end
end
class DefaultItemHandler < BaseItemHandler
private
def update_quality
if item.sell_in > 0
quality = item.quality - 1
else
quality = item.quality - 2
end
item.quality = quality if quality >= 0
end
def update_sell_in
item.sell_in -= 1
end
end
class ConjuredManaCakeHandler < BaseItemHandler
private
def update_quality
quality = item.quality - 2
item.quality = quality if quality >= 0
end
def update_sell_in
item.sell_in -= 1
end
end
class BackstagePassesHandler < BaseItemHandler
private
def update_quality
quality = if item.sell_in > 10
item.quality + 1
elsif item.sell_in > 5
item.quality + 2
elsif item.sell_in > 0
item.quality + 3
else
0
end
item.quality = quality if quality <= 50
item.quality = 50 if quality > 50
end
def update_sell_in
item.sell_in -= 1
end
end
class SulfurasHandler < BaseItemHandler
end
class AgedBrieHandler < BaseItemHandler
private
def update_quality
if item.sell_in > 0
quality = item.quality + 1
else
quality = item.quality + 2
end
item.quality = quality if quality <= 50
end
def update_sell_in
item.sell_in -= 1
end
end
class ItemMapper
attr_reader :item
MAP = {
'Aged Brie' => AgedBrieHandler,
'Sulfuras, Hand of Ragnaros' => SulfurasHandler,
'Backstage passes to a TAFKAL80ETC concert' => BackstagePassesHandler,
'Conjured Mana Cake' => ConjuredManaCakeHandler,
}
def initialize(item)
@item = item
end
def update
handler.new(item).update
end
def self.update(item)
new(item).update
end
private
def handler
@handler ||= MAP[item.name] || DefaultItemHandler
end
end