diff --git a/award.rb b/award.rb index 5208232..0e51bc4 100644 --- a/award.rb +++ b/award.rb @@ -1 +1,58 @@ -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 proc + is_expired = Proc.new { |date| date <= 0} + + # Calculate how many points the award will gain/lose based on a reducer variable + reducer = case @name + 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' + + 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' 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 + + # This is the end; my only friend. +end diff --git a/update_quality.rb b/update_quality.rb index bd1f10f..7df32ae 100644 --- a/update_quality.rb +++ b/update_quality.rb @@ -1,49 +1,9 @@ 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 - end - end - 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 - end + + 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 }