forked from jimweirich/gilded_rose_kata
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgilded_rose.rb
79 lines (66 loc) · 1.42 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
def update_quality(items)
items.each do |item|
case item.name
when 'NORMAL ITEM'
update_normal_item(item)
when 'Backstage passes to a TAFKAL80ETC concert'
update_backstage_pass(item)
when 'Aged Brie'
update_aged_brie(item)
when 'Conjured Mana Cake'
update_conjured_item(item)
else # Sulfuras
#No-Op
end
end
end
def update_normal_item(item)
item.sell_in -= 1
if expired?(item)
decrement_quality(item, 2)
else
decrement_quality(item)
end
end
def update_backstage_pass(item)
item.sell_in -= 1
if expired?(item)
item.quality = 0
elsif item.sell_in < 5
increment_quality(item, 3)
elsif item.sell_in < 10
increment_quality(item, 2)
else
increment_quality(item)
end
end
def update_aged_brie(item)
item.sell_in -= 1
if expired?(item)
increment_quality(item, 2)
else
increment_quality(item)
end
end
def update_conjured_item(item)
item.sell_in -= 1
if expired?(item)
decrement_quality(item, 4)
else
decrement_quality(item, 2)
end
end
def decrement_quality(item, amount = 1)
item.quality -= amount if item.quality > 0
end
def increment_quality(item, amount = 1)
item.quality += amount
item.quality = 50 if item.quality > 50
end
def expired?(item)
item.sell_in < 0
end
#----------------------------
# DO NOT CHANGE THINGS BELOW
#----------------------------
Item = Struct.new(:name, :sell_in, :quality)