diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index e177a497..230d0a3b 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -1,56 +1,48 @@ class GildedRose - def initialize(items) @items = items end - def update_quality() + 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 + # Skip updating 'Sulfuras' items, as they have fixed quality and do not degrade + next if item.name == 'Sulfuras, Hand of Ragnaros' + + # Update item quality based on item type + item.quality = case item.name + when 'Aged Brie' + [50, item.quality + update_count(item)].min + when 'Backstage passes to a TAFKAL80ETC concert' + backstage_passes(item) + when 'Conjured Mana Cake' + [0, item.quality - update_count(item) * 2].max + else + [0, item.quality - update_count(item)].max + end + + item.sell_in -= 1 end end + + private + + # Helper method to determine the update count based on sell_in + def update_count(item) + (item.sell_in.negative? ? 2 : 1) + end + + # Helper method to handle 'Backstage passes' quality update + def backstage_passes(item) + return 0 if item.sell_in.negative? + + if item.sell_in <= 5 + [50, item.quality + 3] + elsif item.sell_in <= 10 + [50, item.quality + 2] + else + [50, item.quality + 1] + end.min + end end class Item diff --git a/ruby/gilded_rose_spec.rb b/ruby/gilded_rose_spec.rb index 015a759f..dade83dc 100644 --- a/ruby/gilded_rose_spec.rb +++ b/ruby/gilded_rose_spec.rb @@ -1,11 +1,85 @@ require 'rspec' - require File.join(File.dirname(__FILE__), 'gilded_rose') describe GildedRose do - it "does not change the name" do - items = [Item.new("foo", 0, 0)] - GildedRose.new(items).update_quality() - expect(items[0].name).to eq "fixme" + it 'does not change the name' do + items = [Item.new('foo', 0, 0)] + GildedRose.new(items).update_quality + expect(items[0].name).to eq 'foo' + end + + let(:items) do + [ + Item.new('Aged Brie', 2, 0), + Item.new('Elixir of the Mongoose', 5, 7), + Item.new('Sulfuras, Hand of Ragnaros', 0, 80), + Item.new('Backstage passes to a TAFKAL80ETC concert', 15, 20), + Item.new('Conjured Mana Cake', 3, 6), + Item.new('Apple Furit', 10, 20) + ] + end + + let(:rose) do + GildedRose.new(items) + end + + let('after_1_day_item') do + { + 'Aged Brie' => [1, 1], + 'Elixir of the Mongoose' => [4, 6], + 'Sulfuras, Hand of Ragnaros' => [0, 80], + 'Backstage passes to a TAFKAL80ETC concert' => [14, 21], + 'Conjured Mana Cake' => [2, 4], + 'Apple Furit' => [9, 19] + } + end + + it 'test after 1 day' do + rose.update_quality + + items.each do |item| + expect(item.sell_in).to eq after_1_day_item[item.name][0] + expect(item.quality).to eq after_1_day_item[item.name][1] + end + end + + let('after_2_day_item') do + { + 'Aged Brie' => [0, 2], + 'Elixir of the Mongoose' => [3, 5], + 'Sulfuras, Hand of Ragnaros' => [0, 80], + 'Backstage passes to a TAFKAL80ETC concert' => [13, 22], + 'Conjured Mana Cake' => [1, 2], + 'Apple Furit' => [8, 18] + } + end + + it 'test after 2 day' do + rose.update_quality + + items.each do |item| + expect(item.sell_in).to eq after_1_day_item[item.name][0] + expect(item.quality).to eq after_1_day_item[item.name][1] + end + end + + let('after_3_day_item') do + { + 'Aged Brie' => [-1, 4], + 'Elixir of the Mongoose' => [2, 4], + 'Sulfuras, Hand of Ragnaros' => [0, 80], + 'Backstage passes to a TAFKAL80ETC concert' => [12, 23], + 'Conjured Mana Cake' => [0, 0], + 'Apple Furit' => [7, 17] + } + end + + it 'test after 3 day' do + rose.update_quality + + items.each do |item| + expect(item.sell_in).to eq after_1_day_item[item.name][0] + expect(item.quality).to eq after_1_day_item[item.name][1] + end end end