From a37f2a52254476a0bd5ff1f090273e9e5caf1d9e Mon Sep 17 00:00:00 2001 From: Shesh Santosh Date: Fri, 14 Feb 2025 16:18:22 +0530 Subject: [PATCH] customize current ruby code --- ruby/gilded_rose.rb | 104 +++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 45 deletions(-) diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..ca4f715c 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -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