-
Notifications
You must be signed in to change notification settings - Fork 0
/
gilded_rose.rb
149 lines (118 loc) · 2.58 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
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
# frozen_string_literal: true
require 'pry-byebug'
# frozen_string_literal: true
module Inventory
class Quality
attr_reader :amount
def initialize(amount)
@amount = amount
end
def degrade
@amount -= 1 if amount.positive?
end
def increase
@amount += 1 if amount < 50
end
def reset
@amount = 0
end
end
class Generic
attr_reader :sell_in
def initialize(quality, sell_in)
@quality = Quality.new(quality)
@sell_in = sell_in
end
def update
@quality.degrade
@sell_in -= 1
@quality.degrade if @sell_in.negative?
end
def quality
@quality.amount
end
end
class AgedBrie
attr_reader :sell_in
def initialize(quality, sell_in)
@quality = Quality.new(quality)
@sell_in = sell_in
end
def update
@quality.increase
@sell_in -= 1
@quality.increase if @sell_in.negative?
end
def quality
@quality.amount
end
end
class BackstagePass
attr_reader :sell_in
def initialize(quality, sell_in)
@quality = Quality.new(quality)
@sell_in = sell_in
end
def update
@quality.increase
@quality.increase if @sell_in < 11
@quality.increase if @sell_in < 6
@sell_in -= 1
@quality.reset if @sell_in.negative?
end
def quality
@quality.amount
end
end
end
# Handle Logic for GildedRose
class GildedRose
class GoodCategory
def build_for(item)
if generic?(item)
Inventory::Generic.new(item.quality, item.sell_in)
elsif aged_brie?(item)
Inventory::AgedBrie.new(item.quality, item.sell_in)
elsif backstage_pass?(item)
Inventory::BackstagePass.new(item.quality, item.sell_in)
end
end
private
def generic?(item)
!(backstage_pass?(item) || aged_brie?(item))
end
def backstage_pass?(item)
item.name == 'Backstage passes to a TAFKAL80ETC concert'
end
def aged_brie?(item)
item.name == 'Aged Brie'
end
end
def initialize(items)
@items = items
end
def update_quality
@items.each do |item|
good = GoodCategory.new.build_for(item)
next if sulfuras?(item)
item.sell_in -= 1
good.update
item.quality = good.quality
end
end
private
def sulfuras?(item)
item.name == 'Sulfuras, Hand of Ragnaros'
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