financial 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/.gitignore +4 -0
  2. data/.infinity_test +16 -0
  3. data/.rspec +3 -0
  4. data/.rvmrc +1 -0
  5. data/Gemfile +15 -0
  6. data/Gemfile.lock +74 -0
  7. data/README.markdown +157 -0
  8. data/Rakefile +1 -0
  9. data/duck_tales.gif +0 -0
  10. data/example.png +0 -0
  11. data/examples/two_accounts.rb +32 -0
  12. data/features/portuguese_dsl.feature +93 -0
  13. data/features/print_financial_table.feature +318 -0
  14. data/features/support/env.rb +1 -0
  15. data/financial.gemspec +22 -0
  16. data/lib/financial.rb +39 -0
  17. data/lib/financial/account.rb +129 -0
  18. data/lib/financial/account_manager.rb +26 -0
  19. data/lib/financial/balance.rb +11 -0
  20. data/lib/financial/balance_calculation.rb +35 -0
  21. data/lib/financial/cost.rb +34 -0
  22. data/lib/financial/costs.rb +30 -0
  23. data/lib/financial/deposit.rb +30 -0
  24. data/lib/financial/deposits.rb +15 -0
  25. data/lib/financial/dsl.rb +16 -0
  26. data/lib/financial/financial_date.rb +18 -0
  27. data/lib/financial/financial_table.rb +109 -0
  28. data/lib/financial/locale.rb +153 -0
  29. data/lib/financial/locales/en.yml +34 -0
  30. data/lib/financial/locales/pt.yml +67 -0
  31. data/lib/financial/parcels.rb +87 -0
  32. data/lib/financial/per_cent.rb +12 -0
  33. data/lib/financial/print_table.rb +34 -0
  34. data/lib/financial/revenue.rb +40 -0
  35. data/lib/financial/revenues.rb +16 -0
  36. data/lib/financial/rspec_matchers.rb +32 -0
  37. data/lib/financial/tax.rb +4 -0
  38. data/lib/financial/version.rb +3 -0
  39. data/spec/financial/account_manager_spec.rb +38 -0
  40. data/spec/financial/account_spec.rb +399 -0
  41. data/spec/financial/balance_spec.rb +44 -0
  42. data/spec/financial/cost_spec.rb +78 -0
  43. data/spec/financial/costs_spec.rb +18 -0
  44. data/spec/financial/deposit_spec.rb +78 -0
  45. data/spec/financial/deposits_spec.rb +23 -0
  46. data/spec/financial/english_spec.rb +76 -0
  47. data/spec/financial/financial_date_spec.rb +50 -0
  48. data/spec/financial/financial_spec.rb +14 -0
  49. data/spec/financial/financial_table_spec.rb +31 -0
  50. data/spec/financial/locale_spec.rb +37 -0
  51. data/spec/financial/parcels_spec.rb +179 -0
  52. data/spec/financial/per_cent_spec.rb +24 -0
  53. data/spec/financial/portuguese_spec.rb +117 -0
  54. data/spec/financial/print_table_spec.rb +76 -0
  55. data/spec/financial/revenue_spec.rb +89 -0
  56. data/spec/financial/revenues_spec.rb +18 -0
  57. data/spec/financial/tax_spec.rb +42 -0
  58. data/spec/spec_helper.rb +25 -0
  59. metadata +139 -0
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ module Financial
5
+ describe Balance do
6
+ let(:balance) { Balance.new(100, '07/14/2011')}
7
+ describe '#value' do
8
+ it 'should get and set the value' do
9
+ balance.value.should equal 100
10
+ end
11
+
12
+ end
13
+
14
+ describe '#name' do
15
+ after(:each) do
16
+ Financial.locale = :en
17
+ end
18
+
19
+ it 'should return the the name in english' do
20
+ Balance.new(100, '07/14/2011').name.should == 'Balance'
21
+ end
22
+
23
+ it 'should return the name in portuguese' do
24
+ Financial.locale = :pt
25
+ Balance.new(100, '17/1/2011').name.should == "Saldo"
26
+ end
27
+ end
28
+
29
+ describe '#date' do
30
+ after(:each) do
31
+ Financial.locale = :en
32
+ end
33
+
34
+ it 'should get and set the date' do
35
+ balance.date.should == Date.civil(2011, 7, 14)
36
+ end
37
+
38
+ it 'should not need to set date when is a Date object' do
39
+ date = Date.civil(2011, 1, 1)
40
+ Balance.new(100, date).date.should == date
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Cost do
5
+ describe '#name' do
6
+ it 'should assign the name in a string capitalize and with space' do
7
+ Cost.new(:a_cost, 400).name.should == "A cost"
8
+ end
9
+ end
10
+
11
+ describe '#method_name' do
12
+ it 'should assign the method name in string' do
13
+ Cost.new(:other_cost, 599).method_name.should equal :other_cost
14
+ end
15
+ end
16
+
17
+ describe '#date' do
18
+ it 'should return the date of today' do
19
+ Cost.new(:other_cost, 400).date.should == Date.today
20
+ end
21
+ end
22
+
23
+ describe '#value' do
24
+ it 'should assign the value passing an Array' do
25
+ Cost.new(:cost, [400]).value.should equal 400
26
+ end
27
+
28
+ it 'should assign the value when not passing an array' do
29
+ Cost.new(:super_cost, 1_000).value.should equal 1_000
30
+ end
31
+ end
32
+
33
+ describe '#format_value' do
34
+ it 'should format the value passing an - sign' do
35
+ Cost.new(:cost, [400]).format_value.should == "- 400,00"
36
+ end
37
+
38
+ it 'should pass the cents value' do
39
+ Cost.new(:super_cost, 1_000.99).format_value.should == "- 1000,99"
40
+ end
41
+ end
42
+
43
+ describe '#+' do
44
+ it 'should sum all the values for cost' do
45
+ (Cost.new(:credit, 100) + Cost.new(:card, 400)).should be 500
46
+ end
47
+ end
48
+
49
+ describe '#in_date' do
50
+ let(:credit_card) { Cost.new(:credit_card, 199) }
51
+ context 'when is in portuguese' do
52
+ it 'should get and set the day/month/year format' do
53
+ Financial.locale = :pt
54
+ credit_card.na_data('20/12/2011').date.should == Date.civil(2011, 12, 20)
55
+ end
56
+ end
57
+
58
+ context 'dont pass any args or nil arguments' do
59
+ it 'should return the default date' do
60
+ Financial.locale = :en
61
+ credit_card.in_date.date.should == Date.today
62
+ end
63
+
64
+ it 'should return the today date if a date is nil' do
65
+ Financial.locale = :en
66
+ credit_card.in_date(nil).date.should == Date.today
67
+ end
68
+ end
69
+
70
+ context 'when is in english' do
71
+ it 'should get and set the month/day/year format' do
72
+ Financial.locale = :en
73
+ credit_card.in_date('12/20/2011').date.should == Date.civil(2011, 12, 20)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Costs do
5
+ let(:costs) { Costs.new }
6
+ describe '#method_missing' do
7
+ it 'should create a cost when passing an argument' do
8
+ costs.super_cost(100).should be_instance_of(Cost)
9
+ end
10
+
11
+ it 'should raise error when pass without argument' do
12
+ expect {
13
+ costs.credit_card
14
+ }.to raise_error Financial::CostWithoutValue, "Cost: credit_card don't have a value. Pass a value!"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Deposit do
5
+ let(:deposit) { Deposit.new(1000, :a_account_name) }
6
+
7
+ describe '#name' do
8
+ after(:each) do
9
+ ensure_locale :en
10
+ end
11
+
12
+ it 'should return the the name in english' do
13
+ deposit.name.should == 'Deposit: anonymous'
14
+ end
15
+
16
+ it 'should return the name in portuguese' do
17
+ Financial.locale = :pt
18
+ deposit.in_account(:super).name.should == "Deposito: super"
19
+ end
20
+ end
21
+
22
+ describe '#value' do
23
+ it 'should return the value of deposit' do
24
+ deposit.value.should be 1000
25
+ end
26
+ end
27
+
28
+ describe '#date' do
29
+ it 'should return the current date for defaults' do
30
+ deposit.date.should == Date.today
31
+ end
32
+ end
33
+
34
+ describe '#account_name' do
35
+ it 'should return the account name' do
36
+ deposit.account_name.should equal :a_account_name
37
+ end
38
+ end
39
+
40
+ describe '#in_account' do
41
+ it 'should set the name of account' do
42
+ deposit.in_account(:bank_account).account_to_deposit.should be :bank_account
43
+ end
44
+
45
+ it 'should set anonymous account if dont pass a account to deposit' do
46
+ deposit.account_to_deposit.should be :anonymous
47
+ end
48
+ end
49
+
50
+ describe '#is_a_received_deposit?' do
51
+ let(:state_bank) { Account.new(:state_bank) }
52
+ let(:country_bank) { Account.new(:country_bank) }
53
+ let(:city_bank) { Account.new(:city_bank) }
54
+
55
+ it 'should return false if is a deposit for that same account' do
56
+ deposits = state_bank.deposits do
57
+ deposit(400).in_account(:country_bank)
58
+ end
59
+ event = deposits.first
60
+ event.is_a_received_deposit?(state_bank).should equal false
61
+ end
62
+
63
+ it 'should return false if is a received deposit but for other account' do
64
+ deposits = city_bank.deposits do
65
+ deposit(600).in_account(:country_bank)
66
+ end
67
+ deposits.first.is_a_received_deposit?(state_bank).should equal false
68
+ end
69
+
70
+ it 'should return true if is a return deposit to the account' do
71
+ deposits = city_bank.deposits do
72
+ deposit(600).in_account(:state_bank)
73
+ end
74
+ deposits.first.is_a_received_deposit?(state_bank).should be_true
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Deposits do
5
+ describe '#deposit' do
6
+ let(:deposits) { Deposits.new(:super_account) }
7
+
8
+ it 'should create a cost when passing an argument' do
9
+ deposits.deposit(100).should be_instance_of(Deposit)
10
+ end
11
+
12
+ it 'should get the account name' do
13
+ deposits.deposit(200).account_name.should be :super_account
14
+ end
15
+
16
+ it 'should raise error when pass without argument' do
17
+ expect {
18
+ deposits.deposit
19
+ }.to raise_error ArgumentError
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ Financial.locale = :en
4
+ include Financial::PerCent
5
+
6
+ account :my_account do
7
+ total -2000
8
+ end
9
+
10
+ account :mega_bank_account, :as => 'Bank Account' do
11
+ total 1099
12
+
13
+ deposits do
14
+ deposit(2000).in_account(:my_account)
15
+ end
16
+
17
+ costs do
18
+ a_cost(400).in_date('7/30/2011')
19
+ other_cost(500).in_date('7/31/2011')
20
+ other_other_cost(100) # will use current date if dont pass a date option
21
+ parcels(2).of(500).every_day(6).beginning(:september)
22
+ end
23
+
24
+ revenues do # or billing do
25
+ a_revenue(300).tax(:decreases => 200).in_date('09/20/2011') # The profit here will be 100
26
+ other_revenue(500).in_date('07/15/2011').tax(6.per_cent).in_date('08/20/2011')
27
+ bill(2000).tax(12.per_cent)
28
+ end
29
+
30
+ end
31
+
32
+ describe 'account :my_account' do
33
+ let(:bank_account) { Financial::Account.find(:name => :mega_bank_account) }
34
+
35
+ describe '#name' do
36
+ it 'should have :bank_account as a name' do
37
+ bank_account.name.should equal :mega_bank_account
38
+ end
39
+ end
40
+
41
+ describe '#total' do
42
+ it 'should have a total' do
43
+ bank_account.total.should equal 1099
44
+ end
45
+ end
46
+
47
+ describe '#costs' do
48
+ it 'should have many costs' do
49
+ bank_account.costs.should have(5).items
50
+ end
51
+ end
52
+
53
+ describe '#total_costs' do
54
+ it 'should calculate the total costs' do
55
+ bank_account.total_costs.should == 4470.0
56
+ end
57
+ end
58
+
59
+ describe '#revenues' do
60
+ it 'should have some revenues' do
61
+ bank_account.revenues.should have(3).items
62
+ end
63
+ end
64
+
65
+ describe '#total_revenues' do
66
+ it 'should calculate the total revenues' do
67
+ bank_account.total_revenues.should equal 2800
68
+ end
69
+ end
70
+
71
+ describe '#taxes' do
72
+ it 'should have some taxes' do
73
+ bank_account.taxes.should have(3).items
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe FinancialDate do
5
+ describe '#date' do
6
+ it 'should return today date when pass nil argument' do
7
+ FinancialDate.new(nil).date.should == Date.today
8
+ end
9
+
10
+ context 'in portuguese date' do
11
+ let(:date) {
12
+ Financial.locale = :pt
13
+ FinancialDate.new('14/09/2011')
14
+ }
15
+
16
+ it 'should parse and return the day' do
17
+ date.day.should equal 14
18
+ end
19
+
20
+ it 'should parse and return the month' do
21
+ date.month.should equal 9
22
+ end
23
+
24
+ it 'should parse and return year' do
25
+ date.year.should equal 2011
26
+ end
27
+ end
28
+
29
+ context 'in english date' do
30
+ let(:date) {
31
+ Financial.locale = :en
32
+ FinancialDate.new('08/19/2011')
33
+ }
34
+
35
+ it 'should parse and return the day' do
36
+ date.day.should equal 19
37
+ end
38
+
39
+ it 'should parse and return the month' do
40
+ date.month.should equal 8
41
+ end
42
+
43
+ it 'should parse and return year' do
44
+ date.year.should equal 2011
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Financial do
4
+ describe '.locale' do
5
+ it 'should return the default locale if dont have a locale' do
6
+ Financial.locale.should == Financial::Locale.new(:en)
7
+ end
8
+
9
+ it 'should assign and get the locale name' do
10
+ Financial.locale = :pt
11
+ Financial.locale.should == Financial::Locale.new(:pt)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Financial
6
+ describe FinancialTable do
7
+ let(:account) { Account.new(:federal_bank) }
8
+ let(:print_table) { PrintTable.new(:federal_bank) }
9
+ let(:financial_table) { FinancialTable.new(account, print_table)}
10
+
11
+ before do
12
+ ensure_locale :en
13
+ print_table.from('3/20/2011').to('4/20/2011')
14
+ end
15
+
16
+ describe '#header' do
17
+ after :each do
18
+ ensure_locale :en
19
+ end
20
+
21
+ it 'should add a header the account name and the periods' do
22
+ financial_table.header.should == "Account name: Federal bank (from: 2011-03-20, to: 2011-04-20)"
23
+ end
24
+
25
+ it 'should add a header for portuguese dates' do
26
+ ensure_locale :pt
27
+ financial_table.header.should == "Nome da Conta: Federal bank (de: 20/03/2011, até: 20/04/2011)"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Locale do
5
+ describe '#initialize' do
6
+ it 'should raise error if pass a locale that dont exist' do
7
+ expect {
8
+ Locale.new(:dont_exist)
9
+ }.to raise_error LocaleDontAvailable
10
+ end
11
+ end
12
+
13
+ describe '#name' do
14
+ it 'should keep the name of the locale' do
15
+ Locale.new(:pt).name.should equal :pt
16
+ end
17
+ end
18
+
19
+ describe '#to_coin' do
20
+ it 'should return a dollar for english locale' do
21
+ Locale.new(:en).to_coin(10).should == "$ 10,00"
22
+ end
23
+
24
+ it 'should return a dollar for english locale with float' do
25
+ Locale.new(:en).to_coin(10.10).should == "$ 10,10"
26
+ end
27
+
28
+ it 'should return a reais for portuguese locale' do
29
+ Locale.new(:pt).to_coin(0.99).should == "R$ 0,99"
30
+ end
31
+
32
+ it 'should return a dollar for english locale with negative numbers' do
33
+ Locale.new(:en).to_coin(-100.10).should == "$ -100,10"
34
+ end
35
+ end
36
+ end
37
+ end