forked from jimweirich/gilded_rose_kata
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgilded_rose.rb
86 lines (72 loc) · 1.63 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
require 'delegate'
class ItemDecorator < SimpleDelegator
def self.decorate(item)
case item.name
when 'Aged Brie'
AgedBrie.new(item)
when 'Backstage passes to a TAFKAL80ETC concert'
BackstagePass.new(item)
when 'Sulfuras, Hand of Ragnaros'
LegendaryItem.new(item)
when 'Conjured Mana Cake'
ConjuredItem.new(item)
else
ItemDecorator.new(item)
end
end
def update
decrement_quality
decrement_sell_in
decrement_quality if self.sell_in < 0
end
private
def decrement_quality
self.quality -= 1 if self.quality > 0
end
def increment_quality
self.quality += 1 if self.quality < 50
end
def decrement_sell_in
self.sell_in -= 1
end
def zero_out_quality
self.quality = 0
end
end
class AgedBrie < ItemDecorator
def update
increment_quality
decrement_sell_in
increment_quality if self.sell_in < 0
end
end
class BackstagePass < ItemDecorator
def update
increment_quality
increment_quality if self.sell_in < 11
increment_quality if self.sell_in < 6
decrement_sell_in
zero_out_quality if self.sell_in < 0
end
end
class LegendaryItem < ItemDecorator
def update; end # No-Op
end
class ConjuredItem < ItemDecorator
def update
decrement_quality
decrement_quality
decrement_sell_in
decrement_quality if self.sell_in < 0
decrement_quality if self.sell_in < 0
end
end
def update_quality(items)
items.each do |item|
ItemDecorator.decorate(item).update
end
end
#----------------------------
# DO NOT CHANGE THINGS BELOW
#----------------------------
Item = Struct.new(:name, :sell_in, :quality)