diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 5cced0fe..c4508e37 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,51 +1,77 @@ def update_quality(items) items.each do |item| - if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' - if item.quality > 0 - if item.name != 'Sulfuras, Hand of Ragnaros' - item.quality -= 1 - end - end - else - if item.quality < 50 - item.quality += 1 - if item.name == 'Backstage passes to a TAFKAL80ETC concert' - if item.sell_in < 11 - if item.quality < 50 - item.quality += 1 - end - end - if item.sell_in < 6 - if item.quality < 50 - item.quality += 1 - end - end - end - end - end - if item.name != 'Sulfuras, Hand of Ragnaros' - 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 -= 1 - end - end - else - item.quality = item.quality - item.quality - end - else - if item.quality < 50 - item.quality += 1 - end - end + case item.name + when 'NORMAL ITEM' + update_normal_item(item) + when 'Backstage passes to a TAFKAL80ETC concert' + update_backstage_pass(item) + when 'Aged Brie' + update_aged_brie(item) + when 'Conjured Mana Cake' + update_conjured_item(item) + else # Sulfuras + #No-Op end end end +def update_normal_item(item) + item.sell_in -= 1 + + if expired?(item) + decrement_quality(item, 2) + else + decrement_quality(item) + end +end + +def update_backstage_pass(item) + item.sell_in -= 1 + + if expired?(item) + item.quality = 0 + elsif item.sell_in < 5 + increment_quality(item, 3) + elsif item.sell_in < 10 + increment_quality(item, 2) + else + increment_quality(item) + end +end + +def update_aged_brie(item) + item.sell_in -= 1 + + if expired?(item) + increment_quality(item, 2) + else + increment_quality(item) + end +end + +def update_conjured_item(item) + item.sell_in -= 1 + + if expired?(item) + decrement_quality(item, 4) + else + decrement_quality(item, 2) + end +end + +def decrement_quality(item, amount = 1) + item.quality -= amount if item.quality > 0 +end + +def increment_quality(item, amount = 1) + item.quality += amount + item.quality = 50 if item.quality > 50 +end + +def expired?(item) + item.sell_in < 0 +end + #---------------------------- # DO NOT CHANGE THINGS BELOW #---------------------------- diff --git a/spec/gilded_rose_spec.rb b/spec/gilded_rose_spec.rb index 0503195f..ceb6e7ac 100644 --- a/spec/gilded_rose_spec.rb +++ b/spec/gilded_rose_spec.rb @@ -191,36 +191,36 @@ context 'before the sell date' do let(:initial_sell_in) { 5 } - xit { expect(item.quality).to eq(initial_quality - 2) } + it { expect(item.quality).to eq(initial_quality - 2) } context 'at zero quality' do let(:initial_quality) { 0 } - xit { expect(item.quality).to eq(initial_quality) } + it { expect(item.quality).to eq(initial_quality) } end end context 'on sell date' do let(:initial_sell_in) { 0 } - xit { expect(item.quality).to eq(initial_quality - 4) } + it { expect(item.quality).to eq(initial_quality - 4) } context 'at zero quality' do let(:initial_quality) { 0 } - xit { expect(item.quality).to eq(initial_quality) } + it { expect(item.quality).to eq(initial_quality) } end end context 'after sell date' do let(:initial_sell_in) { -10 } - xit { expect(item.quality).to eq(initial_quality - 4) } + it { expect(item.quality).to eq(initial_quality - 4) } context 'at zero quality' do let(:initial_quality) { 0 } - xit { expect(item.quality).to eq(initial_quality) } + it { expect(item.quality).to eq(initial_quality) } end end end