/
gilded_rose.rb
107 lines (95 loc) · 2.32 KB
/
gilded_rose.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
def update_quality(items)
QualityUpdater.new.update(items)
end
class QualityUpdater
def update(items)
items.each do |item|
update_one(item)
end
end
private
class NoopQualityUpdater
def update_quality(item)
end
def update_sell_in(item)
end
end
class BrieQualityUpdater
def update_quality(item)
if item.sell_in <= 0
bump(item, 2)
else
bump(item, 1)
end
end
def update_sell_in(item)
item.sell_in -= 1
end
def bump(item, amount)
item.quality += amount
item.quality = 50 if item.quality >= 50
end
end
class BackstagePassQualityUpdater
def update_quality(item)
if item.sell_in > 10
bump(item, 1)
elsif item.sell_in > 5
bump(item, 2)
elsif item.sell_in > 0
bump(item, 3)
else
item.quality = 0
end
end
def update_sell_in(item)
item.sell_in -= 1
end
def bump(item, amount)
item.quality += amount
item.quality = 50 if item.quality >= 50
end
end
class StandardQualityUpdater
def update_quality(item)
if item.sell_in <= 0
bump(item, -2)
else
bump(item, -1)
end
end
def update_sell_in(item)
item.sell_in -= 1
end
def bump(item, amount)
item.quality += amount
item.quality = 50 if item.quality > 50
item.quality = 0 if item.quality < 0
end
end
def update_one(item)
if item.name == 'Sulfuras, Hand of Ragnaros'
updater = NoopQualityUpdater.new
elsif item.name == 'Aged Brie'
updater = BrieQualityUpdater.new
elsif item.name == 'Backstage passes to a TAFKAL80ETC concert'
updater = BackstagePassQualityUpdater.new
else
updater = StandardQualityUpdater.new
end
updater.update_quality(item)
updater.update_sell_in(item)
end
end
# DO NOT CHANGE THINGS BELOW -----------------------------------------
Item = Struct.new(:name, :sell_in, :quality)
# We use the setup in the spec rather than the following for testing.
#
# Items = [
# Item.new("+5 Dexterity Vest", 10, 20),
# Item.new("Aged Brie", 2, 0),
# Item.new("Elixir of the Mongoose", 5, 7),
# Item.new("Sulfuras, Hand of Ragnaros", 0, 80),
# Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20),
# Item.new("Conjured Mana Cake", 3, 6),
# ]