Skip to content

Commit

Permalink
app: refactoring float accounting extension
Browse files Browse the repository at this point in the history
Signed-off-by: Sergey Gaychuk <[email protected]>
Signed-off-by: Sergey Yanovich <[email protected]>
  • Loading branch information
sergeygaychuk authored and yanovich committed Dec 27, 2011
1 parent b0293d5 commit 0c92ef8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 66 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ 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
watch(%r{^spec/.+_spec\.rb$})
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" }
Expand Down
12 changes: 0 additions & 12 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
# encoding: UTF-8

# Copyright (C) 2011 Sergey Yanovich <[email protected]>
#
# 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.
Expand Down Expand Up @@ -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:
40 changes: 40 additions & 0 deletions spec/lib/accounting_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (C) 2011 Sergey Yanovich <[email protected]>
#
# 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
45 changes: 0 additions & 45 deletions test/unit/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions vendor/plugins/accounting/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2011 Sergey Yanovich <[email protected]>
#
# 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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +25,3 @@ def accounting_negative?
!self.accounting_zero? and self < 0.0
end
end

class Float
include FloatAccounting
end

0 comments on commit 0c92ef8

Please sign in to comment.