diff --git a/ruby/gilded_rose.rb b/ruby/gilded_rose.rb index 106aa2bd..a0c66731 100644 --- a/ruby/gilded_rose.rb +++ b/ruby/gilded_rose.rb @@ -1,4 +1,6 @@ class GildedRose + QUALITY_LOWER_LIMIT = 0 + QUALITY_UPPER_LIMIT = 50 def initialize(items) @items = items @@ -11,6 +13,8 @@ class GildedRose update_aged_brie_quality(item) when 'Backstage passes to a TAFKAL80ETC concert' update_backstage_passes_quality(item) + when 'Conjured Mana Cake' + update_conjured_quality(item) when 'Sulfuras, Hand of Ragnaros' update_sulfuras_quality(item) else @@ -20,31 +24,66 @@ class GildedRose end def update_aged_brie_quality(item) - item.sell_in -= 1 + decrease_sell_in_day(item) - item.quality += 1 - item.quality += 1 if item.sell_in.negative? - item.quality = 50 if item.quality > 50 + increase_item_quality(item) + increase_item_quality(item) if item.sell_in.negative? + + keep_quality_upper_limit_in_bounds(item) end def update_backstage_passes_quality(item) - item.sell_in -= 1 + decrease_sell_in_day(item) return item.quality = 0 if item.sell_in.negative? - item.quality += 1 - item.quality += 1 if item.sell_in < 10 - item.quality += 1 if item.sell_in < 5 - item.quality = 50 if item.quality > 50 + increase_item_quality(item) + increase_item_quality(item) if item.sell_in < 10 + increase_item_quality(item) if item.sell_in < 5 + + keep_quality_upper_limit_in_bounds(item) + end + + def update_conjured_quality(item) + decrease_sell_in_day(item) + + decrease_item_quality(item, 2) + decrease_item_quality(item, 2) if item.sell_in.negative? + + keep_quality_lower_limit_in_bounds(item) end def update_normal_quality(item) - item.sell_in -= 1 - item.quality -= 1 - item.quality -= 1 if item.sell_in.negative? - item.quality = 0 if item.quality.negative? + decrease_sell_in_day(item) + + decrease_item_quality(item) + decrease_item_quality(item) if item.sell_in.negative? + + keep_quality_lower_limit_in_bounds(item) end def update_sulfuras_quality(item); end + + private + + def decrease_sell_in_day(item) + item.sell_in -= 1 + end + + def increase_item_quality(item) + item.quality += 1 + end + + def decrease_item_quality(item, by = 1) + item.quality -= by + end + + def keep_quality_upper_limit_in_bounds(item) + item.quality = QUALITY_UPPER_LIMIT if item.quality > QUALITY_UPPER_LIMIT + end + + def keep_quality_lower_limit_in_bounds(item) + item.quality = QUALITY_LOWER_LIMIT if item.quality < QUALITY_LOWER_LIMIT + end end class Item diff --git a/ruby/specs/gilded_rose_spec.rb b/ruby/specs/gilded_rose_spec.rb index 98828409..ed90265e 100644 --- a/ruby/specs/gilded_rose_spec.rb +++ b/ruby/specs/gilded_rose_spec.rb @@ -220,7 +220,7 @@ describe GildedRose do end end - xcontext "when item name is 'Conjured Mana Cake'" do + context "when item name is 'Conjured Mana Cake'" do let(:item_name) { 'Conjured Mana Cake' } context 'when sell by date has not passed' do @@ -245,7 +245,7 @@ describe GildedRose do it 'lowers sell_in by 1' do subject.update_quality - expect(items[0].sell_in).to eq 2 + expect(items[0].sell_in).to eq 0 end end diff --git a/ruby/texttest_fixture.rb b/ruby/texttest_fixture.rb index ad76aacf..2bd2a8cf 100644 --- a/ruby/texttest_fixture.rb +++ b/ruby/texttest_fixture.rb @@ -12,8 +12,7 @@ items = [ Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=15, quality=20), Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=10, quality=49), Item.new(name="Backstage passes to a TAFKAL80ETC concert", sell_in=5, quality=49), - # This Conjured item does not work properly yet - Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), # <-- :O + Item.new(name="Conjured Mana Cake", sell_in=3, quality=6), ] days = 2