mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
customize current ruby code
This commit is contained in:
parent
eb11c58bdf
commit
a37f2a5225
@ -4,52 +4,66 @@ class GildedRose
|
||||
@items = items
|
||||
end
|
||||
|
||||
def update_quality()
|
||||
@items.each do |item|
|
||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
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
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.sell_in = item.sell_in - 1
|
||||
end
|
||||
if item.sell_in < 0
|
||||
if item.name != "Aged Brie"
|
||||
if item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
if item.quality > 0
|
||||
if item.name != "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = item.quality - 1
|
||||
end
|
||||
end
|
||||
else
|
||||
item.quality = item.quality - item.quality
|
||||
end
|
||||
else
|
||||
if item.quality < 50
|
||||
item.quality = item.quality + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
def update_quality
|
||||
@items.each { |item| update_item(item) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_item(item)
|
||||
validate_item(item)
|
||||
case item.name
|
||||
when "Aged Brie"
|
||||
update_aged_brie(item)
|
||||
when "Sulfuras, Hand of Ragnaros"
|
||||
item.quality = 80 # Ensure quality remains 80
|
||||
when "Backstage passes to a TAFKAL80ETC concert"
|
||||
update_backstage_passes(item)
|
||||
else
|
||||
update_standard_item(item)
|
||||
end
|
||||
item.sell_in -= 1 unless item.name == "Sulfuras, Hand of Ragnaros"
|
||||
handle_expired(item)
|
||||
end
|
||||
|
||||
def validate_item(item)
|
||||
raise StandardError, "Invalid sell_in value" unless item.sell_in.is_a?(Integer)
|
||||
raise StandardError, "Invalid quality value" unless item.quality.is_a?(Integer)
|
||||
end
|
||||
|
||||
def update_aged_brie(item)
|
||||
increase_quality(item)
|
||||
end
|
||||
|
||||
def update_backstage_passes(item)
|
||||
increase_quality(item)
|
||||
increase_quality(item) if item.sell_in <= 10
|
||||
increase_quality(item) if item.sell_in <= 5
|
||||
end
|
||||
|
||||
def update_standard_item(item)
|
||||
decrease_quality(item)
|
||||
end
|
||||
|
||||
def handle_expired(item)
|
||||
return unless item.sell_in < 0
|
||||
|
||||
case item.name
|
||||
when "Aged Brie"
|
||||
increase_quality(item)
|
||||
when "Backstage passes to a TAFKAL80ETC concert"
|
||||
item.quality = 0
|
||||
else
|
||||
decrease_quality(item) unless item.name == "Sulfuras, Hand of Ragnaros"
|
||||
end
|
||||
end
|
||||
|
||||
def increase_quality(item)
|
||||
item.quality += 1 if item.quality < 50
|
||||
end
|
||||
|
||||
def decrease_quality(item)
|
||||
item.quality -= 1 if item.quality > 0
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user