From 185e540fa9b5649b3018131c4b98b89d05e22be3 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Sun, 14 Aug 2022 17:52:41 -0500 Subject: [PATCH 01/11] Starting the test. --- README.md | 2 +- update_quality.rb | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 43d9b8d..5324851 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ We have recently gotten a request from our clients to include an additional awar Here is the business story: -- In order to distinguish between providers of high quality, as a consumer, I want to see "Blue Star" awarded providers near the top of the results when the award is initially granted, but it's impact should be smaller the longer it has been from the grant date. +- In order to distinguish between providers of high quality, as a consumer, I want to see "Blue Star" awarded providers near the top of the results when the award is initially granted, but it's impact should be smaller the longer it has been from the grant date. - Acceptance Criteria - "Blue Star" awards should lose quality value twice as fast as normal awards. diff --git a/update_quality.rb b/update_quality.rb index bd1f10f..29c5c7c 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -1,49 +1,54 @@ require 'award' def update_quality(awards) - awards.each do |award| - if award.name != 'Blue First' && award.name != 'Blue Compare' - if award.quality > 0 - if award.name != 'Blue Distinction Plus' - award.quality -= 1 + + awards.each do |award| #awards is an array + + if award.name != 'Blue First' && award.name != 'Blue Compare' # Checking if it is a normal award + if award.quality > 0 # Checking that its quality is not already the minimum (0) + if award.name != 'Blue Distinction Plus' # You can't reduce the quality of Blue Plus + award.quality -= 1 # If it gets here it means it's a normal award end end + else - if award.quality < 50 + if award.quality < 50 # Some awards increase their quality everyday, check if not already at max(50) award.quality += 1 - if award.name == 'Blue Compare' + if award.name == 'Blue Compare' # Blue Compare has special conditions around expiration date if award.expires_in < 11 if award.quality < 50 - award.quality += 1 + award.quality += 1 # Increase an extra point if exp date within 10 days end end if award.expires_in < 6 if award.quality < 50 - award.quality += 1 + award.quality += 1 # Another extra point if exp date within 5 days end end end end end - if award.name != 'Blue Distinction Plus' + if award.name != 'Blue Distinction Plus' # Blue Plus never expires award.expires_in -= 1 end - if award.expires_in < 0 + if award.expires_in < 0 # Functionality after an award expires if award.name != 'Blue First' if award.name != 'Blue Compare' if award.quality > 0 if award.name != 'Blue Distinction Plus' - award.quality -= 1 + award.quality -= 1 # If it's a normal award; it loses one point of quality end end else - award.quality = award.quality - award.quality + award.quality = award.quality - award.quality # If it is Blue Compare, its quality drops to 0 after exp date end else if award.quality < 50 - award.quality += 1 + award.quality += 1 # All other awards increase quality(except Blue Plus) unless at max. end end end + end + end From 549875f0e36545511d33b5f13397a900f02e1b61 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 07:45:59 -0500 Subject: [PATCH 02/11] Revert "Starting the test." This reverts commit 185e540fa9b5649b3018131c4b98b89d05e22be3. --- README.md | 2 +- update_quality.rb | 33 ++++++++++++++------------------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 5324851..43d9b8d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ We have recently gotten a request from our clients to include an additional awar Here is the business story: -- In order to distinguish between providers of high quality, as a consumer, I want to see "Blue Star" awarded providers near the top of the results when the award is initially granted, but it's impact should be smaller the longer it has been from the grant date. +- In order to distinguish between providers of high quality, as a consumer, I want to see "Blue Star" awarded providers near the top of the results when the award is initially granted, but it's impact should be smaller the longer it has been from the grant date. - Acceptance Criteria - "Blue Star" awards should lose quality value twice as fast as normal awards. diff --git a/update_quality.rb b/update_quality.rb index 29c5c7c..bd1f10f 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -1,54 +1,49 @@ require 'award' def update_quality(awards) - - awards.each do |award| #awards is an array - - if award.name != 'Blue First' && award.name != 'Blue Compare' # Checking if it is a normal award - if award.quality > 0 # Checking that its quality is not already the minimum (0) - if award.name != 'Blue Distinction Plus' # You can't reduce the quality of Blue Plus - award.quality -= 1 # If it gets here it means it's a normal award + awards.each do |award| + if award.name != 'Blue First' && award.name != 'Blue Compare' + if award.quality > 0 + if award.name != 'Blue Distinction Plus' + award.quality -= 1 end end - else - if award.quality < 50 # Some awards increase their quality everyday, check if not already at max(50) + if award.quality < 50 award.quality += 1 - if award.name == 'Blue Compare' # Blue Compare has special conditions around expiration date + if award.name == 'Blue Compare' if award.expires_in < 11 if award.quality < 50 - award.quality += 1 # Increase an extra point if exp date within 10 days + award.quality += 1 end end if award.expires_in < 6 if award.quality < 50 - award.quality += 1 # Another extra point if exp date within 5 days + award.quality += 1 end end end end end - if award.name != 'Blue Distinction Plus' # Blue Plus never expires + if award.name != 'Blue Distinction Plus' award.expires_in -= 1 end - if award.expires_in < 0 # Functionality after an award expires + if award.expires_in < 0 if award.name != 'Blue First' if award.name != 'Blue Compare' if award.quality > 0 if award.name != 'Blue Distinction Plus' - award.quality -= 1 # If it's a normal award; it loses one point of quality + award.quality -= 1 end end else - award.quality = award.quality - award.quality # If it is Blue Compare, its quality drops to 0 after exp date + award.quality = award.quality - award.quality end else if award.quality < 50 - award.quality += 1 # All other awards increase quality(except Blue Plus) unless at max. + award.quality += 1 end end end - end - end From 0ebbd15613057f3c28bd8f7a5f90ac10e620e818 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 08:09:03 -0500 Subject: [PATCH 03/11] First draft of a solution --- update_quality.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/update_quality.rb b/update_quality.rb index bd1f10f..7c0a196 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -1,7 +1,36 @@ require 'award' def update_quality(awards) + + expired = Proc.new { |date| date <= 0} + max = Proc.new { |quality| quality == 50} + min = Proc.new { |quality| quality == 0} + + reductor = lambda { |name, expired| + puts name, expired + + reductor = case name + when 'Blue Distinction Plus' then return reductor = 0 + when 'Blue First','Blue Compare' then 1 + when 'Blue Star' then -2 + when 'NORMAL ITEM' then -1 + end + + reductor = reductor * 2 if expired + + reductor + } + + awards.each do |award| + + + reducer = reductor.call(award.name, expired.call(award.expires_in)) + puts reducer + + + + if award.name != 'Blue First' && award.name != 'Blue Compare' if award.quality > 0 if award.name != 'Blue Distinction Plus' From e68ac4eadc67318e5c2481c77f0618e87a040075 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 09:43:18 -0500 Subject: [PATCH 04/11] Blue compare is working. --- update_quality.rb | 78 +++++++++++++++++++---------------------------- 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/update_quality.rb b/update_quality.rb index 7c0a196..efddac6 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -3,11 +3,8 @@ def update_quality(awards) expired = Proc.new { |date| date <= 0} - max = Proc.new { |quality| quality == 50} - min = Proc.new { |quality| quality == 0} reductor = lambda { |name, expired| - puts name, expired reductor = case name when 'Blue Distinction Plus' then return reductor = 0 @@ -21,58 +18,47 @@ def update_quality(awards) reductor } + blue_compare = lambda { |expires, reductor, quality| + puts "-+-+ Inside Compare Proc: Expires in: #{expires} and current reductor: #{reductor} and QUALITY: #{quality}" - awards.each do |award| + + case expires + when expires > 10 then reductor + when 6..10 then reductor += 1 + when 1..5 then reductor += 2 + when -1000..0 then reductor = -quality + end + reductor + } + awards.each do |award| + reducer = reductor.call(award.name, expired.call(award.expires_in)) - puts reducer + puts "------The name is: #{name} initial quality is: #{award.quality} the reducer: #{reducer} expires in: #{award.expires_in}" - if award.name != 'Blue First' && award.name != 'Blue Compare' - if award.quality > 0 - if award.name != 'Blue Distinction Plus' - award.quality -= 1 - end - end + + if name == 'Blue Compare' + puts " +++++++++++++++++++ IT IS COMPARE ++++++++++++++++++++++++=" + puts "reducer before COMPARE: #{reducer}" + reducer = blue_compare.call(award.expires_in, reducer, award.quality) + puts "- reducer after COMPARE: #{reducer}" else - if award.quality < 50 - award.quality += 1 - if award.name == 'Blue Compare' - if award.expires_in < 11 - if award.quality < 50 - award.quality += 1 - end - end - if award.expires_in < 6 - if award.quality < 50 - award.quality += 1 - end - end - end - end - end - if award.name != 'Blue Distinction Plus' - award.expires_in -= 1 - end - if award.expires_in < 0 - if award.name != 'Blue First' - if award.name != 'Blue Compare' - if award.quality > 0 - if award.name != 'Blue Distinction Plus' - award.quality -= 1 - end - end - else - award.quality = award.quality - award.quality - end - else - if award.quality < 50 - award.quality += 1 - end - end + puts "IT IS NOT COMPARE - - - -" + reducer = reducer end + + award.quality += reducer if award.quality > 0 and award.quality < 50 # Quality reduce + + + + + award.expires_in -= 1 unless name == 'Blue Distinction Plus' + + puts "- Aftermath Quality: #{award.quality} and expires in: #{award.expires_in}" # Expires_in reduce + end end From 9c2fe8d41c99f3b7aca0792138886fc285826b29 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 09:47:14 -0500 Subject: [PATCH 05/11] Removing puts --- update_quality.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/update_quality.rb b/update_quality.rb index efddac6..e9b39ab 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -19,8 +19,6 @@ def update_quality(awards) } blue_compare = lambda { |expires, reductor, quality| - puts "-+-+ Inside Compare Proc: Expires in: #{expires} and current reductor: #{reductor} and QUALITY: #{quality}" - case expires when expires > 10 then reductor @@ -28,6 +26,7 @@ def update_quality(awards) when 1..5 then reductor += 2 when -1000..0 then reductor = -quality end + reductor } @@ -38,17 +37,9 @@ def update_quality(awards) puts "------The name is: #{name} initial quality is: #{award.quality} the reducer: #{reducer} expires in: #{award.expires_in}" - - - - if name == 'Blue Compare' - puts " +++++++++++++++++++ IT IS COMPARE ++++++++++++++++++++++++=" - puts "reducer before COMPARE: #{reducer}" - reducer = blue_compare.call(award.expires_in, reducer, award.quality) - puts "- reducer after COMPARE: #{reducer}" - else - puts "IT IS NOT COMPARE - - - -" - reducer = reducer + reducer = case name + when 'Blue Compare' then blue_compare.call(award.expires_in, reducer, award.quality) + else reducer end award.quality += reducer if award.quality > 0 and award.quality < 50 # Quality reduce From 14c0298c9e0de678c5f6cad499a94bf93f77bcb3 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 09:57:00 -0500 Subject: [PATCH 06/11] Refactoring. --- update_quality.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/update_quality.rb b/update_quality.rb index e9b39ab..2181cbc 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -6,6 +6,7 @@ def update_quality(awards) reductor = lambda { |name, expired| + puts "-=-=-= REDUCTOR CALLED =-=-=-" reductor = case name when 'Blue Distinction Plus' then return reductor = 0 when 'Blue First','Blue Compare' then 1 @@ -18,28 +19,29 @@ def update_quality(awards) reductor } - blue_compare = lambda { |expires, reductor, quality| + blue_compare = lambda { |expires, quality| + puts "-=-=-= COMPARE CALLED =-=-=-" case expires - when expires > 10 then reductor - when 6..10 then reductor += 1 - when 1..5 then reductor += 2 - when -1000..0 then reductor = -quality + when 11..Float::INFINITY then 1 + when 6..10 then 2 + when 1..5 then 3 + when -Float::INFINITY..0 then -quality end - reductor + # reductor } awards.each do |award| - reducer = reductor.call(award.name, expired.call(award.expires_in)) + #reducer = reductor.call(award.name, expired.call(award.expires_in)) - puts "------The name is: #{name} initial quality is: #{award.quality} the reducer: #{reducer} expires in: #{award.expires_in}" + puts "------The name is: #{name} initial quality is: #{award.quality} expires in: #{award.expires_in}" reducer = case name - when 'Blue Compare' then blue_compare.call(award.expires_in, reducer, award.quality) - else reducer + when 'Blue Compare' then blue_compare.call(award.expires_in, award.quality) + else reductor.call(award.name, expired.call(award.expires_in)) end award.quality += reducer if award.quality > 0 and award.quality < 50 # Quality reduce From 9e6c19d9be553d36207ab5ad146a71bc795d798f Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 10:09:16 -0500 Subject: [PATCH 07/11] I keep getting errors on Blue Star even tho it's working. --- update_quality.rb | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/update_quality.rb b/update_quality.rb index 2181cbc..c9d71df 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -5,39 +5,27 @@ def update_quality(awards) expired = Proc.new { |date| date <= 0} reductor = lambda { |name, expired| - - puts "-=-=-= REDUCTOR CALLED =-=-=-" reductor = case name when 'Blue Distinction Plus' then return reductor = 0 when 'Blue First','Blue Compare' then 1 when 'Blue Star' then -2 when 'NORMAL ITEM' then -1 end - - reductor = reductor * 2 if expired - - reductor + (expired == true) ? reductor * 2 : reductor } blue_compare = lambda { |expires, quality| - puts "-=-=-= COMPARE CALLED =-=-=-" - case expires when 11..Float::INFINITY then 1 when 6..10 then 2 when 1..5 then 3 when -Float::INFINITY..0 then -quality end - - # reductor } - awards.each do |award| - #reducer = reductor.call(award.name, expired.call(award.expires_in)) - - puts "------The name is: #{name} initial quality is: #{award.quality} expires in: #{award.expires_in}" + puts "------The name is: #{name} initial quality is: #{award.quality} expires in: #{award.expires_in}" reducer = case name when 'Blue Compare' then blue_compare.call(award.expires_in, award.quality) @@ -45,9 +33,7 @@ def update_quality(awards) end award.quality += reducer if award.quality > 0 and award.quality < 50 # Quality reduce - - - + (award.quality > 50 && name != 'Blue Distinction Plus') ? award.quality = 50 : award.quality award.expires_in -= 1 unless name == 'Blue Distinction Plus' From 5780b4030b486eaabcc5842c93b4658c4642372e Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 13:29:02 -0500 Subject: [PATCH 08/11] Refactored the function into a Class method. --- award.rb | 48 +++++++++++++++++++++++++++++++++++++++++- update_quality.rb | 38 ++------------------------------- update_quality_spec.rb | 2 +- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/award.rb b/award.rb index 5208232..4b553e6 100644 --- a/award.rb +++ b/award.rb @@ -1 +1,47 @@ -Award = Struct.new(:name, :expires_in, :quality) +class Award + + attr_accessor :name, :expires_in, :quality + # Constructor + def initialize(name, expires_in, quality) + @name = name + @expires_in = expires_in + @quality = quality + end + + #Update quality and expiration date method + def update_quality + # Checks if expired + is_expired = Proc.new { |date| date <= 0} + # Calculate the amount of points an award will gain/lose + reductor_calculator = lambda { |name, expired| + reductor = case name + when 'Blue Distinction Plus' then return reductor = 0 + when 'Blue First','Blue Compare' then 1 + when 'Blue Star' then -2 + when 'NORMAL ITEM' then -1 + end + (expired == true) ? reductor * 2 : reductor + } + # Calculate the amount of points Blue Compare would gain/lose + blue_compare_calculator = lambda { |expires, quality| + case expires + when 11..Float::INFINITY then 1 + when 6..10 then 2 + when 1..5 then 3 + when -Float::INFINITY..0 then -quality + end + } + # Modify current quality based on the reducer amount + reducer = case @name + when 'Blue Compare' then blue_compare_calculator.call(@expires_in, @quality) + else reductor_calculator.call(@name, is_expired.call(@expires_in)) + end + # Check that it's not going over 50 or under 0 + @quality += reducer if @quality > 0 and @quality < 50 + # Patches the quality for edge cases. + (@quality > 50 && name != 'Blue Distinction Plus') ? @quality = 50 : @quality + # Does not change expiration date if it is Blue Disctintion Plus + @expires_in -= 1 unless name == 'Blue Distinction Plus' + + end +end diff --git a/update_quality.rb b/update_quality.rb index c9d71df..7df32ae 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -2,42 +2,8 @@ def update_quality(awards) - expired = Proc.new { |date| date <= 0} - - reductor = lambda { |name, expired| - reductor = case name - when 'Blue Distinction Plus' then return reductor = 0 - when 'Blue First','Blue Compare' then 1 - when 'Blue Star' then -2 - when 'NORMAL ITEM' then -1 - end - (expired == true) ? reductor * 2 : reductor - } - - blue_compare = lambda { |expires, quality| - case expires - when 11..Float::INFINITY then 1 - when 6..10 then 2 - when 1..5 then 3 - when -Float::INFINITY..0 then -quality - end - } - awards.each do |award| - puts "------The name is: #{name} initial quality is: #{award.quality} expires in: #{award.expires_in}" - - reducer = case name - when 'Blue Compare' then blue_compare.call(award.expires_in, award.quality) - else reductor.call(award.name, expired.call(award.expires_in)) - end - - award.quality += reducer if award.quality > 0 and award.quality < 50 # Quality reduce - (award.quality > 50 && name != 'Blue Distinction Plus') ? award.quality = 50 : award.quality - - award.expires_in -= 1 unless name == 'Blue Distinction Plus' - - puts "- Aftermath Quality: #{award.quality} and expires in: #{award.expires_in}" # Expires_in reduce - + award.update_quality end -end +end \ No newline at end of file diff --git a/update_quality_spec.rb b/update_quality_spec.rb index 973940d..a893bbf 100644 --- a/update_quality_spec.rb +++ b/update_quality_spec.rb @@ -177,7 +177,7 @@ end context 'given a Blue Star award' do - before { pending } + # before { pending } let(:name) { 'Blue Star' } before { award.expires_in.should == initial_expires_in-1 } From dd409c56223ed8074599e37fdaf72cbad100b64c Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 13:44:45 -0500 Subject: [PATCH 09/11] Refactored lambdas into private methods. --- award.rb | 56 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/award.rb b/award.rb index 4b553e6..2c6b1bb 100644 --- a/award.rb +++ b/award.rb @@ -10,38 +10,48 @@ def initialize(name, expires_in, quality) #Update quality and expiration date method def update_quality - # Checks if expired + + # Checks if expired proc is_expired = Proc.new { |date| date <= 0} - # Calculate the amount of points an award will gain/lose - reductor_calculator = lambda { |name, expired| - reductor = case name - when 'Blue Distinction Plus' then return reductor = 0 - when 'Blue First','Blue Compare' then 1 - when 'Blue Star' then -2 - when 'NORMAL ITEM' then -1 - end - (expired == true) ? reductor * 2 : reductor - } - # Calculate the amount of points Blue Compare would gain/lose - blue_compare_calculator = lambda { |expires, quality| - case expires - when 11..Float::INFINITY then 1 - when 6..10 then 2 - when 1..5 then 3 - when -Float::INFINITY..0 then -quality - end - } - # Modify current quality based on the reducer amount + + # Calculate how many points the award will gain/lose based on a reducer variable reducer = case @name - when 'Blue Compare' then blue_compare_calculator.call(@expires_in, @quality) - else reductor_calculator.call(@name, is_expired.call(@expires_in)) + when 'Blue Compare' then blue_compare_calculator(@expires_in, @quality) + else reductor_calculator(@name, is_expired.call(@expires_in)) end + # Check that it's not going over 50 or under 0 @quality += reducer if @quality > 0 and @quality < 50 + # Patches the quality for edge cases. (@quality > 50 && name != 'Blue Distinction Plus') ? @quality = 50 : @quality + # Does not change expiration date if it is Blue Disctintion Plus @expires_in -= 1 unless name == 'Blue Distinction Plus' + # This is the end; my only friend. + end + + private + + # Calculate the amount of points an award will gain/lose method + def reductor_calculator (name, expired) + reductor = case name + when 'Blue Distinction Plus' then return reductor = 0 + when 'Blue First','Blue Compare' then 1 + when 'Blue Star' then -2 + when 'NORMAL ITEM' then -1 + end + (expired == true) ? reductor * 2 : reductor + end + + # Calculate the amount of points Blue Compare would gain/lose method + def blue_compare_calculator (expires, quality) + case expires + when 11..Float::INFINITY then 1 + when 6..10 then 2 + when 1..5 then 3 + when -Float::INFINITY..0 then -quality + end end end From 9f1142b4dd9761346e56c412b8cdcbbaa55a2b01 Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Mon, 15 Aug 2022 13:45:21 -0500 Subject: [PATCH 10/11] Minor update. --- award.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/award.rb b/award.rb index 2c6b1bb..286e0b7 100644 --- a/award.rb +++ b/award.rb @@ -29,7 +29,6 @@ def update_quality # Does not change expiration date if it is Blue Disctintion Plus @expires_in -= 1 unless name == 'Blue Distinction Plus' - # This is the end; my only friend. end private @@ -54,4 +53,6 @@ def blue_compare_calculator (expires, quality) when -Float::INFINITY..0 then -quality end end + + # This is the end; my only friend. end From 0a0fdd15e76d8f88e9669f157a50a652d9f0ac9b Mon Sep 17 00:00:00 2001 From: Itahand Naizir Date: Wed, 17 Aug 2022 11:45:14 -0500 Subject: [PATCH 11/11] Removed redundant 'Blue Compare' from calculator. It wasn't necessary and was never going to apply. --- award.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/award.rb b/award.rb index 286e0b7..0e51bc4 100644 --- a/award.rb +++ b/award.rb @@ -37,7 +37,7 @@ def update_quality def reductor_calculator (name, expired) reductor = case name when 'Blue Distinction Plus' then return reductor = 0 - when 'Blue First','Blue Compare' then 1 + when 'Blue First' then 1 when 'Blue Star' then -2 when 'NORMAL ITEM' then -1 end