Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from kitcheck/codeclimate_changes
Browse files Browse the repository at this point in the history
Remove duplication
  • Loading branch information
Alex Johnson committed Jan 5, 2016
2 parents 7d3e19f + 856ffae commit 9eddf56
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 52 deletions.
28 changes: 13 additions & 15 deletions lib/unit/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,15 @@ def +(other)
if @uom == other.uom
self.class.new((scalar + other.scalar), @uom, @components + other.components)
else
if self < other
scaled_other = other.convert_to(self.uom)
self.class.new((@scalar + scaled_other.scalar), scaled_other.uom, @components + other.components)
else
scaled_self = self.convert_to(other.uom)
self.class.new((scaled_self.scalar + other.scalar), scaled_self.uom, @components + other.components)
end
use_operator_on_other(:+, other)
end
end

def -(other)
if @uom == other.uom
self.class.new((@scalar - other.scalar), @uom, @components + other.components)
else
if self < other
scaled_other = other.convert_to(self.uom)
self.class.new((@scalar - scaled_other.scalar), scaled_other.uom, @components + other.components)
else
scaled_self = self.convert_to(other.uom)
self.class.new((scaled_self.scalar - other.scalar), scaled_self.uom, @components + other.components)
end
use_operator_on_other(:-, other)
end
end

Expand Down Expand Up @@ -147,6 +135,16 @@ def concentration?
false
end

#Display methods
private

def use_operator_on_other(operator, other)
if self < other
scaled_other = other.convert_to(self.uom)
self.class.new((@scalar.send(operator, scaled_other.scalar)), scaled_other.uom, @components + other.components)
else
scaled_self = self.convert_to(other.uom)
self.class.new((scaled_self.scalar.send(operator, other.scalar)), scaled_self.uom, @components + other.components)
end
end
end
end
40 changes: 17 additions & 23 deletions lib/unit/concentration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,11 @@ def initialize(numerator, denominator)
end

def +(other)
if other.is_a? Concentration
#Add numerators
con1, con2 = Concentration.equivalise(self, other)
Concentration.new(con1.numerator + con2.numerator,
con1.denominator #This is the same for both cons because of the equivalise method
)
elsif other.is_a? Mass
Concentration.new(self.numerator + other, self.denominator)
elsif other.is_a? Volume
#Making the assumption that you're diluting
Concentration.new(self.numerator, self.denominator + other)
end
use_operator_on_other(:+, other)
end

def -(other)
if other.is_a? Concentration
con1, con2 = Concentration.equivalise(self, other)
Concentration.new(con1.numerator - con2.numerator,
con1.denominator #This is the same for both cons because of the equivalise method
)
elsif other.is_a? Mass
Concentration.new(self.numerator - other, self.denominator)
elsif other.is_a? Volume
#Making the assumption that you're diluting
Concentration.new(self.numerator, self.denominator - other)
end
use_operator_on_other(:-, other)
end

def /(other)
Expand Down Expand Up @@ -113,6 +92,21 @@ def unit?

private

def use_operator_on_other(operator, other)
if other.is_a? Concentration
#Add numerators
con1, con2 = Concentration.equivalise(self, other)
Concentration.new(con1.numerator.send(operator, con2.numerator),
con1.denominator #This is the same for both cons because of the equivalise method
)
elsif other.is_a? Mass
Concentration.new(self.numerator.send(operator, other), self.denominator)
elsif other.is_a? Volume
#Making the assumption that you're diluting
Concentration.new(self.numerator, self.denominator.send(operator, other))
end
end

def self.equivalise(con1, con2)
#if equivalent denoms, don't need to convert
if con1.denominator == con2.denominator
Expand Down
32 changes: 18 additions & 14 deletions lib/unit/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,17 @@ def self.scale_hash
#We need to make sure we can't add different unitless objects together

def +(other)
if self.uom != other.uom && other.is_a?(Unit)
raise IncompatibleUnitsError.new("You cannot add two different unitless units together")
end
check_compatibility("add", other)
super(other)
end

def -(other)
if self.uom != other.uom && other.is_a?(Unit)
raise IncompatibleUnitsError.new("You cannot subtract two different unitless units together")
end
check_compatibility("subtract", other)
super(other)
end

def /(other)
if self.uom != other.uom && other.is_a?(Unit)
raise IncompatibleUnitsError.new("You cannot divide two different unitless units together")
end
check_compatibility("divide", other)
if other.is_a?(Volume)
Concentration.new(self, other)
else
Expand All @@ -36,25 +30,35 @@ def /(other)
end

def <=>(other)
if self.uom != other.uom && other.is_a?(Unit)
raise IncompatibleUnitsError.new("You cannot compare two different unitless units together")
end
check_compatibility("compare", other)
super(other)
end

def convert_to(uom)
if self.uom != uom
raise IncompatibleUnitsError.new("You cannot compare two different unitless units together")
raise_incompatible_error("compare")
end
super(uom)
end
alias_method :>>, :convert_to

def self.equivalise(u1, u2)
if u1.uom != u2.uom && other.is_a?(Unit)
raise IncompatibleUnitsError.new("You cannot compare two different unitless units together")
raise_incompatible_error("compare")
end
super(u1, u2)
end

private

def check_compatibility(operation, other)
if self.uom != other.uom && other.is_a?(Unit)
raise_incompatible_error(operation)
end
end

def raise_incompatible_error(operation)
raise IncompatibleUnitsError.new("You cannot #{operation} two different unitless units together")
end
end
end

0 comments on commit 9eddf56

Please sign in to comment.