Skip to content

An Object-Oriented Solution #5

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

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
b97af8c
Wrap item struct in a decorator to give us a place to hang behavior.
plainprogrammer Oct 27, 2021
1ab0a5d
Extract decrement quality behavior to ItemDecorator.
plainprogrammer Oct 27, 2021
e56f98f
Extract increment quality behavior to ItemDecorator.
plainprogrammer Oct 27, 2021
2cd52a1
Extract decrement sell-in behavior to ItemDecorator.
plainprogrammer Oct 27, 2021
53915d7
Extract behavior for zeroing out quality to ItemDecorator.
plainprogrammer Oct 27, 2021
af5209e
Moved quality guard clauses into ItemDecorator.
plainprogrammer Oct 28, 2021
d0961d1
Restructure conditionals into flatter case statement.
plainprogrammer Oct 28, 2021
6d76147
Consolidate case statements to capture type differentiation.
plainprogrammer Oct 28, 2021
52d025c
Move conditional into ItemDecorator#update method.
plainprogrammer Oct 28, 2021
049d1e7
Make internal methods for ItemDecorator private.
plainprogrammer Oct 28, 2021
b92abb3
Isolate Aged Brie's update logic into subclass.
plainprogrammer Oct 28, 2021
e2077fd
Isolate Backstage Pass' update logic into subclass.
plainprogrammer Oct 28, 2021
f0ec621
Isolate Legendary item update logic into subclass and clean up normal…
plainprogrammer Oct 28, 2021
faf7bc4
Inlined guard clauses for Backstage Pass' update method.
plainprogrammer Oct 28, 2021
01e20ca
Isolate factory behavior in ItemDecorator.decorate class method.
plainprogrammer Oct 28, 2021
c278d35
Introduce Conjured items into the Gilded Rose.
plainprogrammer Oct 28, 2021
c0923a2
Fix issue with SimpleDelegator inclusion for Ruby 2.7+
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
119 changes: 76 additions & 43 deletions lib/gilded_rose.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,82 @@
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
require 'delegate'

class ItemDecorator < SimpleDelegator
def self.decorate(item)
case item.name
when 'Aged Brie'
AgedBrie.new(item)
when 'Backstage passes to a TAFKAL80ETC concert'
BackstagePass.new(item)
when 'Sulfuras, Hand of Ragnaros'
LegendaryItem.new(item)
when 'Conjured Mana Cake'
ConjuredItem.new(item)
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
ItemDecorator.new(item)
end
end

def update
decrement_quality
decrement_sell_in
decrement_quality if self.sell_in < 0
end

private

def decrement_quality
self.quality -= 1 if self.quality > 0
end

def increment_quality
self.quality += 1 if self.quality < 50
end

def decrement_sell_in
self.sell_in -= 1
end

def zero_out_quality
self.quality = 0
end
end

class AgedBrie < ItemDecorator
def update
increment_quality
decrement_sell_in
increment_quality if self.sell_in < 0
end
end

class BackstagePass < ItemDecorator
def update
increment_quality
increment_quality if self.sell_in < 11
increment_quality if self.sell_in < 6
decrement_sell_in
zero_out_quality if self.sell_in < 0
end
end

class LegendaryItem < ItemDecorator
def update; end # No-Op
end

class ConjuredItem < ItemDecorator
def update
decrement_quality
decrement_quality
decrement_sell_in
decrement_quality if self.sell_in < 0
decrement_quality if self.sell_in < 0
end
end

def update_quality(items)
items.each do |item|
ItemDecorator.decorate(item).update
end
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