From ef03b11bd89c5b33249a1b6d0e7f482a4a32075a Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:01:01 -0400 Subject: [PATCH 01/20] Extract method to adjust quality. --- lib/gilded_rose.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 5cced0fe..f6fe9fc6 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,23 +1,27 @@ +def adjust_quality(item, amount) + item.quality += amount +end + 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 + adjust_quality(item, -1) end end else if item.quality < 50 - item.quality += 1 + adjust_quality(item, 1) if item.name == 'Backstage passes to a TAFKAL80ETC concert' if item.sell_in < 11 if item.quality < 50 - item.quality += 1 + adjust_quality(item, 1) end end if item.sell_in < 6 if item.quality < 50 - item.quality += 1 + adjust_quality(item, 1) end end end @@ -31,15 +35,15 @@ def update_quality(items) if item.name != 'Backstage passes to a TAFKAL80ETC concert' if item.quality > 0 if item.name != 'Sulfuras, Hand of Ragnaros' - item.quality -= 1 + adjust_quality(item, -1) end end else - item.quality = item.quality - item.quality + adjust_quality(item, -item.quality) end else if item.quality < 50 - item.quality += 1 + adjust_quality(item, 1) end end end From 301dc4c27ee4126b5ae1045f90dfe5fee3872128 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:01:51 -0400 Subject: [PATCH 02/20] Extract method to decrement sell-in. --- lib/gilded_rose.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index f6fe9fc6..b0d875d3 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -2,6 +2,10 @@ def adjust_quality(item, amount) item.quality += amount end +def decrement_sell_in(item) + item.sell_in -= 1 +end + def update_quality(items) items.each do |item| if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' @@ -28,7 +32,7 @@ def update_quality(items) end end if item.name != 'Sulfuras, Hand of Ragnaros' - item.sell_in -= 1 + decrement_sell_in(item) end if item.sell_in < 0 if item.name != "Aged Brie" From c4ca9b5f2bb068bc3f64c85c84d84a4226d5e041 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:03:47 -0400 Subject: [PATCH 03/20] Move quality floor guard into adjust_quantity method. --- lib/gilded_rose.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index b0d875d3..722dbb31 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,5 +1,5 @@ def adjust_quality(item, amount) - item.quality += amount + item.quality += amount if item.quality > 0 end def decrement_sell_in(item) @@ -9,10 +9,8 @@ def decrement_sell_in(item) 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' - adjust_quality(item, -1) - end + if item.name != 'Sulfuras, Hand of Ragnaros' + adjust_quality(item, -1) end else if item.quality < 50 @@ -37,10 +35,8 @@ def update_quality(items) 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' - adjust_quality(item, -1) - end + if item.name != 'Sulfuras, Hand of Ragnaros' + adjust_quality(item, -1) end else adjust_quality(item, -item.quality) From 9da68d26ad033e843811955364ad163f0f3c5f17 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:05:38 -0400 Subject: [PATCH 04/20] Move quality ceiling guard into adjust_quantity method. --- lib/gilded_rose.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 722dbb31..3036e51e 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,4 +1,5 @@ def adjust_quality(item, amount) + return if amount > 0 && item.quality == 50 item.quality += amount if item.quality > 0 end @@ -13,19 +14,13 @@ def update_quality(items) adjust_quality(item, -1) end else - if item.quality < 50 - adjust_quality(item, 1) - if item.name == 'Backstage passes to a TAFKAL80ETC concert' - if item.sell_in < 11 - if item.quality < 50 - adjust_quality(item, 1) - end - end - if item.sell_in < 6 - if item.quality < 50 - adjust_quality(item, 1) - end - end + adjust_quality(item, 1) + if item.name == 'Backstage passes to a TAFKAL80ETC concert' + if item.sell_in < 11 + adjust_quality(item, 1) + end + if item.sell_in < 6 + adjust_quality(item, 1) end end end @@ -42,9 +37,7 @@ def update_quality(items) adjust_quality(item, -item.quality) end else - if item.quality < 50 - adjust_quality(item, 1) - end + adjust_quality(item, 1) end end end From 40c90715c07c5bf47f7224563ee853b5b37fc20e Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:07:21 -0400 Subject: [PATCH 05/20] Clean up implementation of adjust_quality method. --- lib/gilded_rose.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 3036e51e..36627c7d 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,6 +1,8 @@ def adjust_quality(item, amount) return if amount > 0 && item.quality == 50 - item.quality += amount if item.quality > 0 + return if amount < 0 && item.quality == 0 + + item.quality += amount end def decrement_sell_in(item) From c27d6192fed1beb60a207e91a60791d96007121a Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:12:56 -0400 Subject: [PATCH 06/20] Replace sell-in decrement conditional with case statement. --- lib/gilded_rose.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 36627c7d..e6243274 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -26,9 +26,14 @@ def update_quality(items) end end end - if item.name != 'Sulfuras, Hand of Ragnaros' + + case item.name + when 'Sulfuras, Hand of Ragnaros' + # No-Op + else decrement_sell_in(item) end + if item.sell_in < 0 if item.name != "Aged Brie" if item.name != 'Backstage passes to a TAFKAL80ETC concert' From 63f5bacb56ef0cd6f942d6620988d1f438770628 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:16:19 -0400 Subject: [PATCH 07/20] Move first conditional for quality adjustment into case statement. --- lib/gilded_rose.rb | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index e6243274..388365ec 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -11,26 +11,23 @@ def decrement_sell_in(item) def update_quality(items) items.each do |item| - if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' - if item.name != 'Sulfuras, Hand of Ragnaros' - adjust_quality(item, -1) - end - else + case item.name + when 'Aged Brie' adjust_quality(item, 1) - if item.name == 'Backstage passes to a TAFKAL80ETC concert' - if item.sell_in < 11 - adjust_quality(item, 1) - end - if item.sell_in < 6 - adjust_quality(item, 1) - end + decrement_sell_in(item) + when 'Backstage passes to a TAFKAL80ETC concert' + adjust_quality(item, 1) + if item.sell_in < 11 + adjust_quality(item, 1) end - end - - case item.name + if item.sell_in < 6 + adjust_quality(item, 1) + end + decrement_sell_in(item) when 'Sulfuras, Hand of Ragnaros' # No-Op else + adjust_quality(item, -1) decrement_sell_in(item) end From 30b95b68cc74e598b6ccbf3f3ca7d27cb7b52722 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:18:28 -0400 Subject: [PATCH 08/20] Move second conditional for quality adjustment into case statement. --- lib/gilded_rose.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 388365ec..2d285c21 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -15,6 +15,7 @@ def update_quality(items) when 'Aged Brie' adjust_quality(item, 1) decrement_sell_in(item) + adjust_quality(item, 1) if item.sell_in < 0 when 'Backstage passes to a TAFKAL80ETC concert' adjust_quality(item, 1) if item.sell_in < 11 @@ -24,25 +25,13 @@ def update_quality(items) adjust_quality(item, 1) end decrement_sell_in(item) + adjust_quality(item, -item.quality) if item.sell_in < 0 when 'Sulfuras, Hand of Ragnaros' # No-Op else adjust_quality(item, -1) decrement_sell_in(item) - end - - if item.sell_in < 0 - if item.name != "Aged Brie" - if item.name != 'Backstage passes to a TAFKAL80ETC concert' - if item.name != 'Sulfuras, Hand of Ragnaros' - adjust_quality(item, -1) - end - else - adjust_quality(item, -item.quality) - end - else - adjust_quality(item, 1) - end + adjust_quality(item, -1) if item.sell_in < 0 end end end From f71f689f4751bbefbb050c480d8ac7ded7af1318 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:20:06 -0400 Subject: [PATCH 09/20] Extract case statement into update method. --- lib/gilded_rose.rb | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 2d285c21..290234cd 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -9,33 +9,35 @@ def decrement_sell_in(item) item.sell_in -= 1 end -def update_quality(items) - items.each do |item| - case item.name - when 'Aged Brie' +def update(item) + case item.name + when 'Aged Brie' + adjust_quality(item, 1) + decrement_sell_in(item) + adjust_quality(item, 1) if item.sell_in < 0 + when 'Backstage passes to a TAFKAL80ETC concert' + adjust_quality(item, 1) + if item.sell_in < 11 adjust_quality(item, 1) - decrement_sell_in(item) - adjust_quality(item, 1) if item.sell_in < 0 - when 'Backstage passes to a TAFKAL80ETC concert' + end + if item.sell_in < 6 adjust_quality(item, 1) - if item.sell_in < 11 - adjust_quality(item, 1) - end - if item.sell_in < 6 - adjust_quality(item, 1) - end - decrement_sell_in(item) - adjust_quality(item, -item.quality) if item.sell_in < 0 - when 'Sulfuras, Hand of Ragnaros' - # No-Op - else - adjust_quality(item, -1) - decrement_sell_in(item) - adjust_quality(item, -1) if item.sell_in < 0 end + decrement_sell_in(item) + adjust_quality(item, -item.quality) if item.sell_in < 0 + when 'Sulfuras, Hand of Ragnaros' + # No-Op + else + adjust_quality(item, -1) + decrement_sell_in(item) + adjust_quality(item, -1) if item.sell_in < 0 end end +def update_quality(items) + items.each(&method(:update)) +end + #---------------------------- # DO NOT CHANGE THINGS BELOW #---------------------------- From 728bea8b3ce5e1ed68c4ec7e299e0b8464983d27 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:21:13 -0400 Subject: [PATCH 10/20] Inlined checks for backstage passes time until sell-in passes. --- lib/gilded_rose.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 290234cd..356a4e0b 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -17,12 +17,8 @@ def update(item) adjust_quality(item, 1) if item.sell_in < 0 when 'Backstage passes to a TAFKAL80ETC concert' adjust_quality(item, 1) - if item.sell_in < 11 - adjust_quality(item, 1) - end - if item.sell_in < 6 - adjust_quality(item, 1) - end + adjust_quality(item, 1) if item.sell_in < 11 + adjust_quality(item, 1) if item.sell_in < 6 decrement_sell_in(item) adjust_quality(item, -item.quality) if item.sell_in < 0 when 'Sulfuras, Hand of Ragnaros' From 8c223dd705effb93a55cd8c896f402e27de944bc Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:39:56 -0400 Subject: [PATCH 11/20] Refactor quality guard cases in adjust_quality method. --- lib/gilded_rose.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 356a4e0b..a4bf4340 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,8 +1,7 @@ def adjust_quality(item, amount) - return if amount > 0 && item.quality == 50 - return if amount < 0 && item.quality == 0 - item.quality += amount + item.quality = 50 if item.quality > 50 + item.quality = 0 if item.quality < 0 end def decrement_sell_in(item) From 592a88e86617eb26779ce9066cca89753dd0acc4 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:40:46 -0400 Subject: [PATCH 12/20] Refactor internals of update method to group all quality adjustments together. --- lib/gilded_rose.rb | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index a4bf4340..386c810e 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -11,21 +11,35 @@ def decrement_sell_in(item) def update(item) case item.name when 'Aged Brie' - adjust_quality(item, 1) decrement_sell_in(item) - adjust_quality(item, 1) if item.sell_in < 0 + + if item.sell_in < 0 + adjust_quality(item, 2) + else + adjust_quality(item, 1) + end when 'Backstage passes to a TAFKAL80ETC concert' - adjust_quality(item, 1) - adjust_quality(item, 1) if item.sell_in < 11 - adjust_quality(item, 1) if item.sell_in < 6 decrement_sell_in(item) - adjust_quality(item, -item.quality) if item.sell_in < 0 + + 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 when 'Sulfuras, Hand of Ragnaros' # No-Op - else - adjust_quality(item, -1) + else # Normal Item decrement_sell_in(item) - adjust_quality(item, -1) if item.sell_in < 0 + + if item.sell_in < 0 + adjust_quality(item, -2) + else + adjust_quality(item, -1) + end end end From 5a5ea5e7e984d83379348f2c29205e7dac262793 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 14:44:13 -0400 Subject: [PATCH 13/20] Treat legendary item in update with guard clause and reduce duplication of calls to decrement_sell_in. --- lib/gilded_rose.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 386c810e..71c1c852 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -9,18 +9,18 @@ def decrement_sell_in(item) end def update(item) + return if item.name == 'Sulfuras, Hand of Ragnaros' + + decrement_sell_in(item) + case item.name when 'Aged Brie' - decrement_sell_in(item) - if item.sell_in < 0 adjust_quality(item, 2) else adjust_quality(item, 1) end when 'Backstage passes to a TAFKAL80ETC concert' - decrement_sell_in(item) - if item.sell_in < 0 adjust_quality(item, -item.quality) elsif item.sell_in >= 10 @@ -30,11 +30,7 @@ def update(item) else adjust_quality(item, 3) end - when 'Sulfuras, Hand of Ragnaros' - # No-Op else # Normal Item - decrement_sell_in(item) - if item.sell_in < 0 adjust_quality(item, -2) else From 7c2b1276c95f62f946226412b8ab48093178cf12 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:01:07 -0400 Subject: [PATCH 14/20] Extract quality update logic into Procs. --- lib/gilded_rose.rb | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 71c1c852..91797b3a 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -8,19 +8,15 @@ def decrement_sell_in(item) item.sell_in -= 1 end -def update(item) - return if item.name == 'Sulfuras, Hand of Ragnaros' - - decrement_sell_in(item) - - case item.name - when 'Aged Brie' +UPDATERS = { + normal: Proc.new { |item| if item.sell_in < 0 - adjust_quality(item, 2) + adjust_quality(item, -2) else - adjust_quality(item, 1) + adjust_quality(item, -1) end - when 'Backstage passes to a TAFKAL80ETC concert' + }, + backstage_pass: Proc.new { |item| if item.sell_in < 0 adjust_quality(item, -item.quality) elsif item.sell_in >= 10 @@ -30,12 +26,28 @@ def update(item) else adjust_quality(item, 3) end - else # Normal Item + }, + aged_brie: Proc.new { |item| if item.sell_in < 0 - adjust_quality(item, -2) + adjust_quality(item, 2) else - adjust_quality(item, -1) + adjust_quality(item, 1) end + } +} + +def update(item) + return if item.name == 'Sulfuras, Hand of Ragnaros' + + decrement_sell_in(item) + + case item.name + when 'Aged Brie' + UPDATERS[:aged_brie].call(item) + when 'Backstage passes to a TAFKAL80ETC concert' + UPDATERS[:backstage_pass].call(item) + else # Normal Item + UPDATERS[:normal].call(item) end end From 957f81eb1899d90d3d1009624470f5adb0ed0fd5 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:02:11 -0400 Subject: [PATCH 15/20] Reduce duplication of call methods into updater procs. --- lib/gilded_rose.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 91797b3a..940574ee 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -41,14 +41,16 @@ def update(item) decrement_sell_in(item) - case item.name + updater = case item.name when 'Aged Brie' - UPDATERS[:aged_brie].call(item) + UPDATERS[:aged_brie] when 'Backstage passes to a TAFKAL80ETC concert' - UPDATERS[:backstage_pass].call(item) + UPDATERS[:backstage_pass] else # Normal Item - UPDATERS[:normal].call(item) + UPDATERS[:normal] end + + updater.call(item) end def update_quality(items) From 83d663fd4162a117048f696627b4f37eb96415fd Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:03:50 -0400 Subject: [PATCH 16/20] Replace guard clause with updater Proc for legendary item. --- lib/gilded_rose.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 940574ee..917143c5 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -33,20 +33,22 @@ def decrement_sell_in(item) else adjust_quality(item, 1) end - } + }, + legendary: Proc.new {|_item|} } def update(item) - return if item.name == 'Sulfuras, Hand of Ragnaros' - - decrement_sell_in(item) - updater = case item.name + when 'Sulfuras, Hand of Ragnaros' + UPDATERS[:legendary] when 'Aged Brie' + decrement_sell_in(item) UPDATERS[:aged_brie] when 'Backstage passes to a TAFKAL80ETC concert' + decrement_sell_in(item) UPDATERS[:backstage_pass] else # Normal Item + decrement_sell_in(item) UPDATERS[:normal] end From e61ab8083b4305eba5505ee11b84bdc30c2f88ae Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:04:36 -0400 Subject: [PATCH 17/20] Moved decrement sell-in behavior into updater Procs. --- lib/gilded_rose.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 917143c5..fcb6c6eb 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -10,6 +10,7 @@ def decrement_sell_in(item) UPDATERS = { normal: Proc.new { |item| + decrement_sell_in(item) if item.sell_in < 0 adjust_quality(item, -2) else @@ -17,6 +18,7 @@ def decrement_sell_in(item) end }, backstage_pass: Proc.new { |item| + decrement_sell_in(item) if item.sell_in < 0 adjust_quality(item, -item.quality) elsif item.sell_in >= 10 @@ -28,6 +30,7 @@ def decrement_sell_in(item) end }, aged_brie: Proc.new { |item| + decrement_sell_in(item) if item.sell_in < 0 adjust_quality(item, 2) else @@ -42,13 +45,10 @@ def update(item) when 'Sulfuras, Hand of Ragnaros' UPDATERS[:legendary] when 'Aged Brie' - decrement_sell_in(item) UPDATERS[:aged_brie] when 'Backstage passes to a TAFKAL80ETC concert' - decrement_sell_in(item) UPDATERS[:backstage_pass] else # Normal Item - decrement_sell_in(item) UPDATERS[:normal] end From f2f67b677f6c916cd23b2dde0595193c548be58a Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:09:10 -0400 Subject: [PATCH 18/20] Introduce ITEM_TYPE_MAP to ease lookup of Proc from UPDATERS. --- lib/gilded_rose.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index fcb6c6eb..144425fe 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -40,19 +40,15 @@ def decrement_sell_in(item) legendary: Proc.new {|_item|} } -def update(item) - updater = case item.name - when 'Sulfuras, Hand of Ragnaros' - UPDATERS[:legendary] - when 'Aged Brie' - UPDATERS[:aged_brie] - when 'Backstage passes to a TAFKAL80ETC concert' - UPDATERS[:backstage_pass] - else # Normal Item - UPDATERS[:normal] - end +ITEM_TYPE_MAP = { + 'Sulfuras, Hand of Ragnaros': :legendary, + 'Aged Brie': :aged_brie, + 'Backstage passes to a TAFKAL80ETC concert': :backstage_pass +} - updater.call(item) +def update(item) + item_type = ITEM_TYPE_MAP[item.name.to_sym] || :normal + UPDATERS[item_type].call(item) end def update_quality(items) From e1fe1660e9adef2fc946d1f1e044c448a7afde70 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:10:31 -0400 Subject: [PATCH 19/20] Introduce Conjured Mana Cake into Gilded Rose inventory. --- lib/gilded_rose.rb | 13 +++++++++++-- spec/gilded_rose_spec.rb | 12 ++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 144425fe..ac186793 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -37,13 +37,22 @@ def decrement_sell_in(item) adjust_quality(item, 1) end }, - legendary: Proc.new {|_item|} + 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 + 'Backstage passes to a TAFKAL80ETC concert': :backstage_pass, + 'Conjured Mana Cake': :conjured } def update(item) 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 From 690e56af0791babf614c79f1316c6efe7da08220 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Thu, 28 Oct 2021 15:11:50 -0400 Subject: [PATCH 20/20] Extract item type lookup into own method. --- lib/gilded_rose.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index ac186793..7af590eb 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -55,9 +55,12 @@ def decrement_sell_in(item) 'Conjured Mana Cake': :conjured } +def lookup_item_type(item) + ITEM_TYPE_MAP[item.name.to_sym] || :normal +end + def update(item) - item_type = ITEM_TYPE_MAP[item.name.to_sym] || :normal - UPDATERS[item_type].call(item) + UPDATERS[lookup_item_type(item)].call(item) end def update_quality(items)