From b2422b28048e3dccace0defcc41a31049bc95da4 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:48:50 -0700 Subject: [PATCH 01/22] Flatten first conditional block to clarify what item type is in scope. --- 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 5cced0fe..583aede9 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,11 +1,7 @@ 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 + if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' && item.name != 'Sulfuras, Hand of Ragnaros' + item.quality -= 1 if item.quality > 0 else if item.quality < 50 item.quality += 1 From 82d71d39062482bd0cbf46e84d39a322ef6317b4 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:50:27 -0700 Subject: [PATCH 02/22] Replace negative condition with positive condition to reveal what item type we are checking in first conditional. --- lib/gilded_rose.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 583aede9..4b29fe58 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,6 +1,6 @@ def update_quality(items) items.each do |item| - if item.name != 'Aged Brie' && item.name != 'Backstage passes to a TAFKAL80ETC concert' && item.name != 'Sulfuras, Hand of Ragnaros' + if item.name == 'NORMAL ITEM' item.quality -= 1 if item.quality > 0 else if item.quality < 50 From 7ec8972371dfb27b7c07bab8d3f7df169b65b7b9 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:52:00 -0700 Subject: [PATCH 03/22] Convert second top-level condition to unless for clarity of check. --- 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 4b29fe58..e2438d3c 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -19,9 +19,11 @@ def update_quality(items) end end end - if item.name != 'Sulfuras, Hand of Ragnaros' + + unless 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' From 262663d858839d6688ca776bc6a33634f08fc4e4 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:54:18 -0700 Subject: [PATCH 04/22] Reverse conditional structure to state positive case for backstage pass item type in third conditional block. --- 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 e2438d3c..e97c9367 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -26,14 +26,14 @@ 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.name == 'Backstage passes to a TAFKAL80ETC concert' + item.quality = item.quality - item.quality + else 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 From 3548bede398302ee84e6f284ed07f19bd6433048 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:55:08 -0700 Subject: [PATCH 05/22] Pulled nested conditional to higher level to reduce nesting in third conditional section. --- lib/gilded_rose.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index e97c9367..ea5598ae 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -25,14 +25,12 @@ def update_quality(items) end if item.sell_in < 0 - if item.name != "Aged Brie" + if item.name != "Aged Brie" && item.name != 'Sulfuras, Hand of Ragnaros' if item.name == 'Backstage passes to a TAFKAL80ETC concert' item.quality = item.quality - item.quality else if item.quality > 0 - if item.name != 'Sulfuras, Hand of Ragnaros' - item.quality -= 1 - end + item.quality -= 1 end end else From 5a4aa4186c9a66f705396b5c12e45f0263042a48 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 15:58:58 -0700 Subject: [PATCH 06/22] Inlined all quality guard clauses to further flatten conditionals. --- lib/gilded_rose.rb | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index ea5598ae..49573ae5 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -3,19 +3,14 @@ def update_quality(items) if item.name == 'NORMAL ITEM' item.quality -= 1 if item.quality > 0 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 + item.quality += 1 if item.quality < 50 + + if item.name == 'Backstage passes to a TAFKAL80ETC concert' + if item.sell_in < 11 + item.quality += 1 if item.quality < 50 + end + if item.sell_in < 6 + item.quality += 1 if item.quality < 50 end end end @@ -29,14 +24,10 @@ def update_quality(items) if item.name == 'Backstage passes to a TAFKAL80ETC concert' item.quality = item.quality - item.quality else - if item.quality > 0 - item.quality -= 1 - end + item.quality -= 1 if item.quality > 0 end else - if item.quality < 50 - item.quality += 1 - end + item.quality += 1 if item.quality < 50 end end end From 5fc9d4835e6d949bb14a36848dc9b8e7085c743a Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:09:14 -0700 Subject: [PATCH 07/22] Extract decrement quality method to reduce duplication. --- lib/gilded_rose.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 49573ae5..5cbe2bd7 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,7 +1,11 @@ +def decrement_quality(item) + item.quality -= 1 if item.quality > 0 +end + def update_quality(items) items.each do |item| if item.name == 'NORMAL ITEM' - item.quality -= 1 if item.quality > 0 + decrement_quality(item) else item.quality += 1 if item.quality < 50 @@ -24,7 +28,7 @@ def update_quality(items) if item.name == 'Backstage passes to a TAFKAL80ETC concert' item.quality = item.quality - item.quality else - item.quality -= 1 if item.quality > 0 + decrement_quality(item) end else item.quality += 1 if item.quality < 50 From 27f9b344280807391526794fa255afe51170125c Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:09:49 -0700 Subject: [PATCH 08/22] Extract increment quality method to reduce duplication. --- lib/gilded_rose.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 5cbe2bd7..acb2384a 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -2,19 +2,23 @@ def decrement_quality(item) item.quality -= 1 if item.quality > 0 end +def increment_quality(item) + item.quality += 1 if item.quality < 50 +end + def update_quality(items) items.each do |item| if item.name == 'NORMAL ITEM' decrement_quality(item) else - item.quality += 1 if item.quality < 50 + increment_quality(item) if item.name == 'Backstage passes to a TAFKAL80ETC concert' if item.sell_in < 11 - item.quality += 1 if item.quality < 50 + increment_quality(item) end if item.sell_in < 6 - item.quality += 1 if item.quality < 50 + increment_quality(item) end end end @@ -31,7 +35,7 @@ def update_quality(items) decrement_quality(item) end else - item.quality += 1 if item.quality < 50 + increment_quality(item) end end end From b4076af3a570f81856ffbc57105c03f33ce8b17f Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:14:36 -0700 Subject: [PATCH 09/22] Changed first conditional section to a case statement to clarify item type behavior. --- lib/gilded_rose.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index acb2384a..697ebb74 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -8,19 +8,20 @@ def increment_quality(item) def update_quality(items) items.each do |item| - if item.name == 'NORMAL ITEM' + case item.name + when 'NORMAL ITEM' decrement_quality(item) - else + when 'Backstage passes to a TAFKAL80ETC concert' increment_quality(item) - if item.name == 'Backstage passes to a TAFKAL80ETC concert' - if item.sell_in < 11 - increment_quality(item) - end - if item.sell_in < 6 - increment_quality(item) - end + if item.sell_in < 11 + increment_quality(item) end + if item.sell_in < 6 + increment_quality(item) + end + else + increment_quality(item) end unless item.name == 'Sulfuras, Hand of Ragnaros' From b0e62420a6ceacebd1ce0d212d40351281143eb4 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:17:21 -0700 Subject: [PATCH 10/22] Introduce ability to adjust quality increment via argument. --- lib/gilded_rose.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 697ebb74..be5b4a66 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -2,8 +2,8 @@ def decrement_quality(item) item.quality -= 1 if item.quality > 0 end -def increment_quality(item) - item.quality += 1 if item.quality < 50 +def increment_quality(item, amount = 1) + item.quality += amount if item.quality < 50 end def update_quality(items) From f98b49c64d26bc039cc28b5081ea4c48dbe77a2b Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:21:13 -0700 Subject: [PATCH 11/22] Use ability to specify quality increment amount to clarify how backstage passes behave. --- lib/gilded_rose.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index be5b4a66..81cfe02c 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -12,12 +12,11 @@ def update_quality(items) when 'NORMAL ITEM' decrement_quality(item) when 'Backstage passes to a TAFKAL80ETC concert' - increment_quality(item) - - if item.sell_in < 11 - increment_quality(item) - end if item.sell_in < 6 + increment_quality(item, 3) + elsif item.sell_in < 11 + increment_quality(item, 2) + else increment_quality(item) end else From 93f2801c67fc1fcfee121a87d213c3d729fbd831 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:22:48 -0700 Subject: [PATCH 12/22] Flatten nested conditional in third conditional section to clarify item type behavior. --- lib/gilded_rose.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 81cfe02c..5a7c9e30 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -28,14 +28,14 @@ def update_quality(items) end if item.sell_in < 0 - if item.name != "Aged Brie" && item.name != 'Sulfuras, Hand of Ragnaros' - if item.name == 'Backstage passes to a TAFKAL80ETC concert' - item.quality = item.quality - item.quality - else - decrement_quality(item) - end - else + if item.name == 'Backstage passes to a TAFKAL80ETC concert' + item.quality = item.quality - item.quality + end + + if item.name == "Aged Brie" || item.name == 'Sulfuras, Hand of Ragnaros' increment_quality(item) + else + decrement_quality(item) end end end From c7e74a2875b312dd11d3de1527aa4449c40441f9 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:24:29 -0700 Subject: [PATCH 13/22] Change third conditional section to use case to clarify item type behavior. --- lib/gilded_rose.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 5a7c9e30..c7efb907 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -28,13 +28,12 @@ def update_quality(items) end if item.sell_in < 0 - if item.name == 'Backstage passes to a TAFKAL80ETC concert' + case item.name + when 'Backstage passes to a TAFKAL80ETC concert' item.quality = item.quality - item.quality - end - - if item.name == "Aged Brie" || item.name == 'Sulfuras, Hand of Ragnaros' + when 'Aged Brie' increment_quality(item) - else + when 'NORMAL ITEM' decrement_quality(item) end end From 8ff3fb5f89815a45b1a817bb3b38ce5c9ebb15df Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:26:15 -0700 Subject: [PATCH 14/22] Extracted conditional for expiration detection into method to clarify intent. --- 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 c7efb907..66365485 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -6,6 +6,10 @@ def increment_quality(item, amount = 1) item.quality += amount if item.quality < 50 end +def expired?(item) + item.sell_in < 0 +end + def update_quality(items) items.each do |item| case item.name @@ -27,7 +31,7 @@ def update_quality(items) item.sell_in -= 1 end - if item.sell_in < 0 + if expired?(item) case item.name when 'Backstage passes to a TAFKAL80ETC concert' item.quality = item.quality - item.quality From a27f6d8259587ba11e9daeb089aaa8f02e1e2313 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:32:57 -0700 Subject: [PATCH 15/22] Consolidate all item behaviors into singular case statement within update quality method. --- lib/gilded_rose.rb | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 66365485..be4d776f 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -15,6 +15,8 @@ def update_quality(items) case item.name when 'NORMAL ITEM' decrement_quality(item) + item.sell_in -= 1 + decrement_quality(item) if expired?(item) when 'Backstage passes to a TAFKAL80ETC concert' if item.sell_in < 6 increment_quality(item, 3) @@ -23,23 +25,16 @@ def update_quality(items) else increment_quality(item) end - else - increment_quality(item) - end - unless item.name == 'Sulfuras, Hand of Ragnaros' item.sell_in -= 1 - end - if expired?(item) - case item.name - when 'Backstage passes to a TAFKAL80ETC concert' - item.quality = item.quality - item.quality - when 'Aged Brie' - increment_quality(item) - when 'NORMAL ITEM' - decrement_quality(item) - end + item.quality = item.quality - item.quality if expired?(item) + when 'Aged Brie' + increment_quality(item) + item.sell_in -= 1 + increment_quality(item) if expired?(item) + else # Sulfuras + #No-Op end end end From d88e08da883f450afff88f5abd8131a1debadecd Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:34:33 -0700 Subject: [PATCH 16/22] Extract item type behaviors into discreet update methods. --- lib/gilded_rose.rb | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index be4d776f..194a4c68 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -10,29 +10,41 @@ def expired?(item) item.sell_in < 0 end +def update_normal_item(item) + decrement_quality(item) + item.sell_in -= 1 + decrement_quality(item) if expired?(item) +end + +def update_backstage_pass(item) + if item.sell_in < 6 + increment_quality(item, 3) + elsif item.sell_in < 11 + increment_quality(item, 2) + else + increment_quality(item) + end + + item.sell_in -= 1 + + item.quality = item.quality - item.quality if expired?(item) +end + +def update_aged_brie(item) + increment_quality(item) + item.sell_in -= 1 + increment_quality(item) if expired?(item) +end + def update_quality(items) items.each do |item| case item.name when 'NORMAL ITEM' - decrement_quality(item) - item.sell_in -= 1 - decrement_quality(item) if expired?(item) + update_normal_item(item) when 'Backstage passes to a TAFKAL80ETC concert' - if item.sell_in < 6 - increment_quality(item, 3) - elsif item.sell_in < 11 - increment_quality(item, 2) - else - increment_quality(item) - end - - item.sell_in -= 1 - - item.quality = item.quality - item.quality if expired?(item) + update_backstage_pass(item) when 'Aged Brie' - increment_quality(item) - item.sell_in -= 1 - increment_quality(item) if expired?(item) + update_aged_brie(item) else # Sulfuras #No-Op end From ca13f0b621fa1d3b7ec9ab1a3387cdac67acf407 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:37:07 -0700 Subject: [PATCH 17/22] Reordered methods to clarify their relationship to each other. --- lib/gilded_rose.rb | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 194a4c68..75bb4b68 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -1,13 +1,16 @@ -def decrement_quality(item) - item.quality -= 1 if item.quality > 0 -end - -def increment_quality(item, amount = 1) - item.quality += amount if item.quality < 50 -end - -def expired?(item) - item.sell_in < 0 +def update_quality(items) + items.each do |item| + 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) + else # Sulfuras + #No-Op + end + end end def update_normal_item(item) @@ -36,19 +39,16 @@ def update_aged_brie(item) increment_quality(item) if expired?(item) end -def update_quality(items) - items.each do |item| - 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) - else # Sulfuras - #No-Op - end - end +def decrement_quality(item) + item.quality -= 1 if item.quality > 0 +end + +def increment_quality(item, amount = 1) + item.quality += amount if item.quality < 50 +end + +def expired?(item) + item.sell_in < 0 end #---------------------------- From 5386e024c83010cc8461fdd8160b19483ef026b0 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:38:11 -0700 Subject: [PATCH 18/22] Changed expiration behavior for updating backstage pass for increased clarity. --- lib/gilded_rose.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 75bb4b68..494e6e51 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -30,7 +30,7 @@ def update_backstage_pass(item) item.sell_in -= 1 - item.quality = item.quality - item.quality if expired?(item) + item.quality = 0 if expired?(item) end def update_aged_brie(item) From e2ea8b417bc554747fcc9f7822be7c2e1c5be247 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:41:13 -0700 Subject: [PATCH 19/22] Rearranged code for updating aged brie to adjust sell in before adjusting quality. --- lib/gilded_rose.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 494e6e51..3b149350 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -34,9 +34,13 @@ def update_backstage_pass(item) end def update_aged_brie(item) - increment_quality(item) item.sell_in -= 1 - increment_quality(item) if expired?(item) + + if expired?(item) + increment_quality(item, 2) + else + increment_quality(item) + end end def decrement_quality(item) @@ -44,7 +48,8 @@ def decrement_quality(item) end def increment_quality(item, amount = 1) - item.quality += amount if item.quality < 50 + item.quality += amount + item.quality = 50 if item.quality > 50 end def expired?(item) From ce0c8f4dd91519377c21785a6ee6cc12177e222d Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:42:42 -0700 Subject: [PATCH 20/22] Rearranged code for updating backstage passes to adjust sell in before adjusting quality. --- lib/gilded_rose.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 3b149350..aa846144 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -20,17 +20,17 @@ def update_normal_item(item) end def update_backstage_pass(item) - if item.sell_in < 6 + item.sell_in -= 1 + + if expired?(item) + item.quality = 0 + elsif item.sell_in < 5 increment_quality(item, 3) - elsif item.sell_in < 11 + elsif item.sell_in < 10 increment_quality(item, 2) else increment_quality(item) end - - item.sell_in -= 1 - - item.quality = 0 if expired?(item) end def update_aged_brie(item) From c95a9aac7c5d7433c0761d98b0da70c15e35a661 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:43:51 -0700 Subject: [PATCH 21/22] Rearranged code for updating normal items to adjust sell in before adjusting quality. --- lib/gilded_rose.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index aa846144..8d92a92a 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -14,9 +14,13 @@ def update_quality(items) end def update_normal_item(item) - decrement_quality(item) item.sell_in -= 1 - decrement_quality(item) if expired?(item) + + if expired?(item) + decrement_quality(item, 2) + else + decrement_quality(item) + end end def update_backstage_pass(item) @@ -43,8 +47,8 @@ def update_aged_brie(item) end end -def decrement_quality(item) - item.quality -= 1 if item.quality > 0 +def decrement_quality(item, amount = 1) + item.quality -= amount if item.quality > 0 end def increment_quality(item, amount = 1) From 58de3578d49787cb13d4b1b9f492866c4feb7927 Mon Sep 17 00:00:00 2001 From: James Thompson Date: Wed, 20 Oct 2021 16:49:19 -0700 Subject: [PATCH 22/22] Add support for the Conjured Mana Cake item. --- lib/gilded_rose.rb | 12 ++++++++++++ spec/gilded_rose_spec.rb | 12 ++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/gilded_rose.rb b/lib/gilded_rose.rb index 8d92a92a..c4508e37 100644 --- a/lib/gilded_rose.rb +++ b/lib/gilded_rose.rb @@ -7,6 +7,8 @@ def update_quality(items) update_backstage_pass(item) when 'Aged Brie' update_aged_brie(item) + when 'Conjured Mana Cake' + update_conjured_item(item) else # Sulfuras #No-Op end @@ -47,6 +49,16 @@ def update_aged_brie(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 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