mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
Also noticed I made a mistake in some specs, would normally optimise spec code later on. Added an extra test for when sell_in is 1 for safety as it depends on the order of operations in the extraction I make.
80 lines
1.9 KiB
Ruby
80 lines
1.9 KiB
Ruby
class GildedRose
|
|
|
|
def initialize(items)
|
|
@items = items
|
|
end
|
|
|
|
def update_quality
|
|
@items.each do |item|
|
|
case item.name
|
|
when 'Aged Brie'
|
|
update_aged_brie_quality(item)
|
|
when 'Sulfuras, Hand of Ragnaros'
|
|
update_sulfuras_quality(item)
|
|
else
|
|
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
|
|
if item.quality > 0
|
|
item.quality = item.quality - 1
|
|
end
|
|
else
|
|
if item.quality < 50
|
|
item.quality = item.quality + 1
|
|
if item.name == "Backstage passes to a TAFKAL80ETC concert"
|
|
if item.sell_in < 11
|
|
if item.quality < 50
|
|
item.quality = item.quality + 1
|
|
end
|
|
end
|
|
if item.sell_in < 6
|
|
if item.quality < 50
|
|
item.quality = item.quality + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
item.sell_in = item.sell_in - 1
|
|
if item.sell_in < 0
|
|
if item.name != "Aged Brie"
|
|
if item.name != "Backstage passes to a TAFKAL80ETC concert"
|
|
if item.quality > 0
|
|
item.quality = item.quality - 1
|
|
end
|
|
else
|
|
item.quality = item.quality - item.quality
|
|
end
|
|
else
|
|
if item.quality < 50
|
|
item.quality = item.quality + 1
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def update_aged_brie_quality(item)
|
|
item.sell_in -= 1
|
|
|
|
item.quality += 1
|
|
item.quality += 1 if item.sell_in.negative?
|
|
item.quality = 50 if item.quality > 50
|
|
end
|
|
|
|
def update_sulfuras_quality(item); 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
|