add mathematical operators for Value to operate with Numeric objects
This commit is contained in:
parent
31d89371ff
commit
d7475fb09e
@ -35,14 +35,48 @@ module Gnucash
|
||||
end
|
||||
end
|
||||
|
||||
# Add two Value objects
|
||||
# Add to a Value object
|
||||
# +other+ can be another Value or a Numeric
|
||||
def +(other)
|
||||
Value.new(@val + other.val)
|
||||
if other.is_a?(Value)
|
||||
Value.new(@val + other.val)
|
||||
elsif other.is_a?(Numeric)
|
||||
(to_f + other).round(2)
|
||||
else
|
||||
raise "Unexpected argument"
|
||||
end
|
||||
end
|
||||
|
||||
# Subtract two Value objects
|
||||
# Subtract from a Value object
|
||||
# +other+ can be another Value or a Numeric
|
||||
def -(other)
|
||||
Value.new(@val - other.val)
|
||||
if other.is_a?(Value)
|
||||
Value.new(@val - other.val)
|
||||
elsif other.is_a?(Numeric)
|
||||
(to_f - other).round(2)
|
||||
else
|
||||
raise "Unexpected argument"
|
||||
end
|
||||
end
|
||||
|
||||
# Multiply a Value object
|
||||
# +other+ should be a Numeric
|
||||
def *(other)
|
||||
if other.is_a?(Numeric)
|
||||
(to_f * other).round(2)
|
||||
else
|
||||
raise "Unexpected argument"
|
||||
end
|
||||
end
|
||||
|
||||
# Divide a Value object
|
||||
# +other+ should be a Numeric
|
||||
def /(other)
|
||||
if other.is_a?(Numeric)
|
||||
(to_f / other).round(2)
|
||||
else
|
||||
raise "Unexpected argument"
|
||||
end
|
||||
end
|
||||
|
||||
# Represent the Value as a string (two decimal places)
|
||||
|
@ -30,6 +30,13 @@ module Gnucash
|
||||
c.to_s.should == "35.79"
|
||||
end
|
||||
|
||||
it "allows adding a Value object to a Numeric" do
|
||||
a = Value.new("1234/100")
|
||||
b = 15
|
||||
c = a + b
|
||||
c.should == 27.34
|
||||
end
|
||||
|
||||
it "allows subtracting two value objects" do
|
||||
a = Value.new("-12300/100")
|
||||
b = Value.new("99/100")
|
||||
@ -37,6 +44,27 @@ module Gnucash
|
||||
c.to_s.should == "-123.99"
|
||||
end
|
||||
|
||||
it "allows subtracting a Numeric from a Value object" do
|
||||
a = Value.new("890/100")
|
||||
b = 7.8
|
||||
c = a - b
|
||||
c.should == 1.1
|
||||
end
|
||||
|
||||
it "allows multiplying a Value by a Numeric" do
|
||||
a = Value.new("100/100")
|
||||
b = 12
|
||||
c = a * b
|
||||
c.should == 12.00
|
||||
end
|
||||
|
||||
it "allows dividing a Value by a Numeric" do
|
||||
a = Value.new("150000/100")
|
||||
b = 150
|
||||
c = a / b
|
||||
c.should == 10.0
|
||||
end
|
||||
|
||||
it "formats the number with two decimal places" do
|
||||
Value.new("1400/100").to_s.should == "14.00"
|
||||
end
|
||||
@ -46,5 +74,23 @@ module Gnucash
|
||||
(Value.new("89/100") < Value.new("100/100")).should be_true
|
||||
(Value.new("1234/100") > Value.new("222/100")).should be_true
|
||||
end
|
||||
|
||||
context "errors" do
|
||||
it "raises an error when attempting to add an unknown object type" do
|
||||
expect { Value.new(33) + nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to subtract an unknown object type" do
|
||||
expect { Value.new(33) - nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to multiply by an unknown object type" do
|
||||
expect { Value.new(33) * nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
|
||||
it "raises an error when attempting to divide by an unknown object type" do
|
||||
expect { Value.new(33) / nil }.to raise_error /Unexpected argument/i
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user