diff --git a/Gemfile b/Gemfile index fa75df1..a1b93f3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ -source 'https://rubygems.org' +source :rubygems gemspec diff --git a/lib/core_ext/big_decimal.rb b/lib/core_ext/big_decimal.rb index 2f88fc6..e07baef 100644 --- a/lib/core_ext/big_decimal.rb +++ b/lib/core_ext/big_decimal.rb @@ -1,9 +1,36 @@ -require 'corrency/corrency_mixin' +require 'bigdecimal' +require 'bigdecimal/util' class BigDecimal - include CorrencyMixin - def to_s(s = "%.2f") - sprintf(s, self) + def inc_vat + (self * vat_rate).to_d end + + def ex_vat + (self / vat_rate).to_d + end + + def next_ninety_nine + self.ceil - BigDecimal.new("0.01") + end + + def add_margin(margin) + ((self / (100 - margin)) * 100).to_d.round(2) + end + + def margin(cost) + ((self - BigDecimal.new(cost)) / self) * 100 + end + + old_to_s = instance_method :to_s + + define_method :to_s do |*param| + "%.2f" % self.round(2) + end + + protected + def vat_rate + 1 + (Corrency::Config.vat_rate.to_f / 100) + end end diff --git a/lib/core_ext/fixnum.rb b/lib/core_ext/fixnum.rb deleted file mode 100644 index c6cafd9..0000000 --- a/lib/core_ext/fixnum.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'corrency/corrency_mixin' - -class Fixnum - include CorrencyMixin -end diff --git a/lib/corrency.rb b/lib/corrency.rb index 0f736b1..398e831 100644 --- a/lib/corrency.rb +++ b/lib/corrency.rb @@ -1,3 +1,2 @@ require 'corrency/config' -require 'core_ext/big_decimal' -require 'core_ext/fixnum' +require 'core_ext/big_decimal' \ No newline at end of file diff --git a/lib/corrency/corrency_mixin.rb b/lib/corrency/corrency_mixin.rb deleted file mode 100644 index 7f4f73c..0000000 --- a/lib/corrency/corrency_mixin.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'bigdecimal' -require 'bigdecimal/util' - -module CorrencyMixin - def inc_vat - (self.to_d * vat_rate) - end - - def ex_vat - (self.to_d / vat_rate) - end - - def next_ninety_nine - self.ceil - BigDecimal.new("0.01") - end - - def add_margin(margin) - ((self.to_d / (100 - margin.to_d)) * 100).round(2) - end - - def margin(cost) - self_d = self.to_d - ((self_d - BigDecimal.new(cost)) / self_d) * 100 - end - - protected - def vat_rate - 1 + (Corrency::Config.vat_rate.to_d / 100) - end -end diff --git a/test/big_decimal_test.rb b/test/big_decimal_test.rb index d876219..f153321 100644 --- a/test/big_decimal_test.rb +++ b/test/big_decimal_test.rb @@ -8,17 +8,13 @@ @bd = BigDecimal.new('100.3799') assert_equal("100.38", BigDecimal.new("100.38000123").to_s) end - + it "should round to two decimal places if 0" do assert_equal "0.00", BigDecimal.new("0").to_s end - + it "should round to two decimal places if 1.20" do assert_equal "1.20", BigDecimal.new("1.2").to_s end - - it "should accept a format override" do - assert_equal '31.4159', BigDecimal.new('31.4159265358979').to_s('%.4f') - end end end diff --git a/test/fixnum_test.rb b/test/fixnum_test.rb deleted file mode 100644 index 044bcf6..0000000 --- a/test/fixnum_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'test_helper' - -describe Fixnum do - - describe "#inc_vate" do - - it "should return the same as a BigDecimal does" do - [0, 3, 1000].each do |num| - assert_equal(num.inc_vat, BigDecimal.new(num).inc_vat) - end - end - end -end