Skip to content
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

Finished the test with Ruby. #57

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
59 changes: 58 additions & 1 deletion award.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 4 additions & 44 deletions update_quality.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion update_quality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down