Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A "Functional" Solution #6

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ef03b11
Extract method to adjust quality.
plainprogrammer Oct 28, 2021
301dc4c
Extract method to decrement sell-in.
plainprogrammer Oct 28, 2021
c4ca9b5
Move quality floor guard into adjust_quantity method.
plainprogrammer Oct 28, 2021
9da68d2
Move quality ceiling guard into adjust_quantity method.
plainprogrammer Oct 28, 2021
40c9071
Clean up implementation of adjust_quality method.
plainprogrammer Oct 28, 2021
c27d619
Replace sell-in decrement conditional with case statement.
plainprogrammer Oct 28, 2021
63f5bac
Move first conditional for quality adjustment into case statement.
plainprogrammer Oct 28, 2021
30b95b6
Move second conditional for quality adjustment into case statement.
plainprogrammer Oct 28, 2021
f71f689
Extract case statement into update method.
plainprogrammer Oct 28, 2021
728bea8
Inlined checks for backstage passes time until sell-in passes.
plainprogrammer Oct 28, 2021
8c223dd
Refactor quality guard cases in adjust_quality method.
plainprogrammer Oct 28, 2021
592a88e
Refactor internals of update method to group all quality adjustments …
plainprogrammer Oct 28, 2021
5a5ea5e
Treat legendary item in update with guard clause and reduce duplicati…
plainprogrammer Oct 28, 2021
7c2b127
Extract quality update logic into Procs.
plainprogrammer Oct 28, 2021
957f81e
Reduce duplication of call methods into updater procs.
plainprogrammer Oct 28, 2021
83d663f
Replace guard clause with updater Proc for legendary item.
plainprogrammer Oct 28, 2021
e61ab80
Moved decrement sell-in behavior into updater Procs.
plainprogrammer Oct 28, 2021
f2f67b6
Introduce ITEM_TYPE_MAP to ease lookup of Proc from UPDATERS.
plainprogrammer Oct 28, 2021
e1fe166
Introduce Conjured Mana Cake into Gilded Rose inventory.
plainprogrammer Oct 28, 2021
690e56a
Extract item type lookup into own method.
plainprogrammer Oct 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 62 additions & 41 deletions lib/gilded_rose.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,70 @@
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
def adjust_quality(item, amount)
item.quality += amount
item.quality = 50 if item.quality > 50
item.quality = 0 if item.quality < 0
end

def decrement_sell_in(item)
item.sell_in -= 1
end

UPDATERS = {
normal: Proc.new { |item|
decrement_sell_in(item)
if item.sell_in < 0
adjust_quality(item, -2)
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
adjust_quality(item, -1)
end
if item.name != 'Sulfuras, Hand of Ragnaros'
item.sell_in -= 1
},
backstage_pass: Proc.new { |item|
decrement_sell_in(item)
if item.sell_in < 0
adjust_quality(item, -item.quality)
elsif item.sell_in >= 10
adjust_quality(item, 1)
elsif item.sell_in >= 5
adjust_quality(item, 2)
else
adjust_quality(item, 3)
end
},
aged_brie: Proc.new { |item|
decrement_sell_in(item)
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
adjust_quality(item, 2)
else
adjust_quality(item, 1)
end
end
},
legendary: Proc.new {|_item|},
conjured: Proc.new { |item|
decrement_sell_in(item)
if item.sell_in < 0
adjust_quality(item, -4)
else
adjust_quality(item, -2)
end
}
}

ITEM_TYPE_MAP = {
'Sulfuras, Hand of Ragnaros': :legendary,
'Aged Brie': :aged_brie,
'Backstage passes to a TAFKAL80ETC concert': :backstage_pass,
'Conjured Mana Cake': :conjured
}

def lookup_item_type(item)
ITEM_TYPE_MAP[item.name.to_sym] || :normal
end

def update(item)
UPDATERS[lookup_item_type(item)].call(item)
end

def update_quality(items)
items.each(&method(:update))
end

#----------------------------
Expand Down
12 changes: 6 additions & 6 deletions spec/gilded_rose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down