Value: make objects Comparable

This commit is contained in:
Josh Holtrop 2013-08-11 14:59:43 -04:00
parent 983861c1dc
commit 05cf6093d7
2 changed files with 12 additions and 0 deletions

View File

@ -1,5 +1,7 @@
module Gnucash module Gnucash
class Value class Value
include Comparable
attr_accessor :val attr_accessor :val
def initialize(val) def initialize(val)
@ -27,5 +29,9 @@ module Gnucash
def to_s def to_s
sprintf("%.02f", @val / 100.0) sprintf("%.02f", @val / 100.0)
end end
def <=>(other)
@val <=> other.val
end
end end
end end

View File

@ -34,5 +34,11 @@ module Gnucash
it "formats the number with two decimal places" do it "formats the number with two decimal places" do
Value.new("1400/100").to_s.should == "14.00" Value.new("1400/100").to_s.should == "14.00"
end end
it "supports comparisons between two Value objects" do
Value.new("1234/100").should == Value.new(1234)
(Value.new("89/100") < Value.new("100/100")).should be_true
(Value.new("1234/100") > Value.new("222/100")).should be_true
end
end end
end end