diff --git a/lib/gnucash/value.rb b/lib/gnucash/value.rb index dce940b..6b69454 100644 --- a/lib/gnucash/value.rb +++ b/lib/gnucash/value.rb @@ -1,5 +1,7 @@ module Gnucash class Value + include Comparable + attr_accessor :val def initialize(val) @@ -27,5 +29,9 @@ module Gnucash def to_s sprintf("%.02f", @val / 100.0) end + + def <=>(other) + @val <=> other.val + end end end diff --git a/spec/gnucash/value_spec.rb b/spec/gnucash/value_spec.rb index 61760a2..7636529 100644 --- a/spec/gnucash/value_spec.rb +++ b/spec/gnucash/value_spec.rb @@ -34,5 +34,11 @@ module Gnucash it "formats the number with two decimal places" do Value.new("1400/100").to_s.should == "14.00" 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