From 0c92ef8af3afbee922b2231427dff32a91b311a5 Mon Sep 17 00:00:00 2001 From: Sergey Gaychuk Date: Mon, 28 Nov 2011 12:39:59 +0300 Subject: [PATCH] app: refactoring float accounting extension Signed-off-by: Sergey Gaychuk Signed-off-by: Sergey Yanovich --- Gemfile.lock | 6 +-- Guardfile | 4 +- db/schema.rb | 12 ----- spec/lib/accounting_spec.rb | 40 +++++++++++++++++ test/unit/account_test.rb | 45 ------------------- vendor/plugins/accounting/init.rb | 10 +++++ .../plugins/accounting/lib/accounting.rb | 7 +-- 7 files changed, 58 insertions(+), 66 deletions(-) create mode 100644 spec/lib/accounting_spec.rb create mode 100644 vendor/plugins/accounting/init.rb rename app/models/float_accounting.rb => vendor/plugins/accounting/lib/accounting.rb (91%) diff --git a/Gemfile.lock b/Gemfile.lock index 57d58ee4..0f0ea35a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,11 +36,11 @@ GEM database_cleaner (0.7.0) diff-lcs (1.1.3) erubis (2.7.0) - factory_girl (2.3.1) + factory_girl (2.3.2) activesupport guard (0.8.8) thor (~> 0.14.6) - guard-rspec (0.5.7) + guard-rspec (0.5.8) guard (>= 0.8.4) guard-spork (0.3.2) guard (>= 0.8.4) @@ -100,7 +100,7 @@ GEM sprockets (2.0.3) hike (~> 1.2) rack (~> 1.0) - tilt (~> 1.1, != 1.3.0) + tilt (!= 1.3.0, ~> 1.1) sqlite3 (1.3.4) sqlite3-ruby (1.3.3) sqlite3 (>= 1.3.3) diff --git a/Guardfile b/Guardfile index 6ae4c1b4..f236f67a 100644 --- a/Guardfile +++ b/Guardfile @@ -13,8 +13,9 @@ guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAIL watch(%r{^spec/support/(.+)\.rb$}) end -guard 'rspec', :version => 2, :cli => '--drb', :all_on_start => false, :all_on_pass => false do watch(%r{^spec/.+_spec\.rb$}) +guard 'rspec', :version => 2, :cli => '--drb', :all_on_start => false, :all_on_pass => false do watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch(%r{^vendor/plugins/(.+)/lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } # Rails example @@ -22,6 +23,7 @@ guard 'rspec', :version => 2, :cli => '--drb', :all_on_start => false, :all_on_p watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch(%r{^vendor/plugins/(.+)/lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } watch(%r{^spec/support/(.+)\.rb$}) { "spec" } watch('spec/spec_helper.rb') { "spec" } diff --git a/db/schema.rb b/db/schema.rb index 2491409e..049fbab9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,14 +1,4 @@ # encoding: UTF-8 - -# Copyright (C) 2011 Sergey Yanovich -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License as -# published by the Free Software Foundation; either version 3 of the -# License, or (at your option) any later version. -# -# Please see ./COPYING for details - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -124,5 +114,3 @@ add_index "txns", ["fact_id"], :name => "index_txns_on_fact_id", :unique => true end - -# vim: ts=2 sts=2 sw=2 et: diff --git a/spec/lib/accounting_spec.rb b/spec/lib/accounting_spec.rb new file mode 100644 index 00000000..9ef6566e --- /dev/null +++ b/spec/lib/accounting_spec.rb @@ -0,0 +1,40 @@ +# Copyright (C) 2011 Sergey Yanovich +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Please see ./COPYING for details + +require "spec_helper" + +describe Float do + it "should add accounting methods" do + #describe "#accounting_zero?" + 0.0.accounting_zero?.should be_true + 0.00009.accounting_zero?.should be_false + -0.00009.accounting_zero?.should be_false + -0.000071.accounting_zero?.should be_true + 0.000081.accounting_zero?.should be_true + 0.03.accounting_zero?.should be_false + #describe "#accounting_round64" + 100.05.accounting_round64.should eq(100.0) + -100.05.accounting_round64.should eq(-100.0) + 100.8.accounting_round64.should eq(101.0) + -100.8.accounting_round64.should eq(-101.0) + #describe "#accounting_norm" + 1.0005.accounting_norm.should eq(1.0) + -1.0005.accounting_norm.should eq(-1.0) + 1.008.accounting_norm.should eq(1.01) + -1.008.accounting_norm.should eq(-1.01) + #describe "#accounting_negative?" + 0.0.accounting_negative?.should be_false + 0.00009.accounting_negative?.should be_false + -0.00009.accounting_negative?.should be_true + -0.000071.accounting_negative?.should be_false + 0.000081.accounting_negative?.should be_false + 0.03.accounting_negative?.should be_false + -0.03.accounting_negative?.should be_true + end +end diff --git a/test/unit/account_test.rb b/test/unit/account_test.rb index de8d4e66..8188e8b3 100644 --- a/test/unit/account_test.rb +++ b/test/unit/account_test.rb @@ -16,10 +16,6 @@ def setup end test "account" do - float_accounting_zero - float_accounting_round64 - float_accounting_norm - float_accounting_negative balance_should_save account_test loss_transaction @@ -33,47 +29,6 @@ def setup end private - def float_accounting_zero - assert 0.0.accounting_zero?, "0.0 is not zero" - assert !0.00009.accounting_zero?, "0.00009 is zero" - assert !-0.00009.accounting_zero?, "-0.00009 is zero" - assert -0.000071.accounting_zero?, "-0.000071 is not zero" - assert 0.000081.accounting_zero?, "0.000081 is not zero" - assert !0.03.accounting_zero?, "0.03 is zero" - end - - def float_accounting_round64 - assert_equal 100.0, 100.05.accounting_round64, - "100.05 accounting round fail" - assert_equal -100.0, -100.05.accounting_round64, - "-100.05 accounting round fail" - assert_equal 101.0, 100.8.accounting_round64, - "100.8 accounting round fail" - assert_equal -101.0, -100.8.accounting_round64, - "-100.8 accounting round fail" - end - - def float_accounting_norm - assert_equal 1.0, 1.0005.accounting_norm, - "1.0005 accounting round fail" - assert_equal -1.0, -1.0005.accounting_norm, - "-1.0005 accounting round fail" - assert_equal 1.01, 1.008.accounting_norm, - "1.008 accounting round fail" - assert_equal -1.01, -1.008.accounting_norm, - "-1.008 accounting round fail" - end - - def float_accounting_negative - assert !0.0.accounting_negative?, "0.0 is negative" - assert !0.00009.accounting_negative?, "0.00009 is negative" - assert -0.00009.accounting_negative?, "-0.00009 is not negative" - assert !-0.000071.accounting_negative?, "-0.000071 is negative" - assert !0.000081.accounting_negative?, "0.000081 is negative" - assert !0.03.accounting_negative?, "0.03 is negative" - assert -0.03.accounting_negative?, "-0.03 is not negative" - end - def balance_should_save assert_equal 0, Balance.all.count, "Balance count is not 0" b = Balance.new diff --git a/vendor/plugins/accounting/init.rb b/vendor/plugins/accounting/init.rb new file mode 100644 index 00000000..86d743b9 --- /dev/null +++ b/vendor/plugins/accounting/init.rb @@ -0,0 +1,10 @@ +# Copyright (C) 2011 Sergey Yanovich +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Please see ./COPYING for details + +require "accounting" diff --git a/app/models/float_accounting.rb b/vendor/plugins/accounting/lib/accounting.rb similarity index 91% rename from app/models/float_accounting.rb rename to vendor/plugins/accounting/lib/accounting.rb index ea7917da..645726c9 100644 --- a/app/models/float_accounting.rb +++ b/vendor/plugins/accounting/lib/accounting.rb @@ -6,7 +6,8 @@ # License, or (at your option) any later version. # # Please see ./COPYING for details -module FloatAccounting + +Float.class_eval do def accounting_zero? self < 0.00009 and self > -0.00009 end @@ -24,7 +25,3 @@ def accounting_negative? !self.accounting_zero? and self < 0.0 end end - -class Float - include FloatAccounting -end