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,179 @@
1
+ # coding: utf-8
2
+ require 'spec_helper'
3
+
4
+ module Financial
5
+ describe Parcels do
6
+ describe '#number' do
7
+ it 'should set and get the number of the parcels' do
8
+ Parcels.new(6).number.should be 6
9
+ end
10
+ end
11
+
12
+ describe '#name' do
13
+ it 'should set and get the name of the parcels in english' do
14
+ Financial.locale = :en
15
+ Parcels.new(3).name.should == "Parcel"
16
+ end
17
+
18
+ it 'should set and get the name of the parcels in portuguese' do
19
+ Financial.locale = :pt
20
+ Parcels.new(3).name.should == "Parcela"
21
+ end
22
+ end
23
+
24
+ describe 'default instances' do
25
+ it 'should return zero as default value of the parcels' do
26
+ Parcels.new(1).value_of_the_parcels.should be 0
27
+ end
28
+
29
+ it 'should return the first day as default day of the parcels' do
30
+ Parcels.new(2).day_of_the_parcels.should be 1
31
+ end
32
+
33
+ it 'should return the current month of the beginning month' do
34
+ Date.should_receive(:today).and_return(Date.civil(2011, 7, 3))
35
+ Parcels.new(3).beginning_month.should equal 7
36
+ end
37
+ end
38
+
39
+ describe '#of' do
40
+ it 'should set the value of the parcels' do
41
+ Parcels.new(5).of(500).value_of_the_parcels.should equal 500
42
+ end
43
+ end
44
+
45
+ describe '#every_day' do
46
+ it 'should set the day of the parcels' do
47
+ Parcels.new(3).every_day(14).day_of_the_parcels.should equal 14
48
+ end
49
+ end
50
+
51
+ describe '#beginning' do
52
+ it 'should set the beginning month' do
53
+ Financial.locale = :en
54
+ Parcels.new(2).beginning(:september).beginning_month.should equal 9
55
+ end
56
+
57
+ it 'should be possible to set a portuguese month name' do
58
+ Financial.locale = :pt
59
+ Parcels.new(4).beginning(:janeiro).beginning_month.should equal 1
60
+ Financial.locale = :en
61
+ end
62
+
63
+ it 'should return the current month if dont pass a beginning month' do
64
+ Date.should_receive(:today).and_return(Date.civil(2011, 6, 1))
65
+ Parcels.new(4).beginning_month.should equal 6
66
+ end
67
+
68
+ it 'should raise if dont have the month specified' do
69
+ expect {
70
+ Parcels.new(2).beginning(:dont_exist)
71
+ }.to raise_error Financial::MonthDontExist
72
+ end
73
+ end
74
+
75
+ describe '#choose_month_for' do
76
+ context 'for parcels with less then twelve numbers' do
77
+ let(:parcels) { Parcels.new(9) }
78
+ before do
79
+ mock_date(20, 1, 2011)
80
+ end
81
+
82
+ it 'should choose the exactly months' do
83
+ parcels.choose_month_for(0).should be 1
84
+ parcels.choose_month_for(7).should be 8
85
+ parcels.choose_month_for(8).should be 9
86
+ end
87
+ end
88
+
89
+ context 'for parcels with greater then twelve numbers' do
90
+ let(:parcels) { Parcels.new(13) }
91
+
92
+ before do
93
+ mock_date(20, 6, 2011)
94
+ end
95
+
96
+ it 'should choose the exactly months' do
97
+ parcels.choose_month_for(0).should be 6
98
+ parcels.choose_month_for(6).should be 12
99
+ parcels.choose_month_for(7).should be 1
100
+ parcels.choose_month_for(9).should be 3
101
+ parcels.choose_month_for(12).should be 6
102
+ end
103
+ end
104
+ end
105
+
106
+ describe '#choose_year_for' do
107
+ it 'should choose the current year' do
108
+ mock_date(4, 7, 2011)
109
+ Parcels.new(2).choose_year_for(:parcel_number => 3).should be 2011
110
+ end
111
+
112
+ it 'should choose the next year for parcel that pass this current year' do
113
+ mock_date(20, 6, 2011)
114
+ Parcels.new(8).choose_year_for(:parcel_number => 7).should be 2012
115
+ end
116
+
117
+ it 'should choose this next years from now too' do
118
+ mock_date(20, 8, 2011)
119
+ parcels = Parcels.new(30)
120
+ parcels.choose_year_for(:parcel_number => 5).should be 2012
121
+ parcels.choose_year_for(:parcel_number => 13).should be 2012
122
+ parcels.choose_year_for(:parcel_number => 17).should be 2013
123
+ parcels.choose_year_for(:parcel_number => 18).should be 2013
124
+ parcels.choose_year_for(:parcel_number => 29).should be 2014
125
+ end
126
+ end
127
+
128
+ describe '#+' do
129
+ it 'should sum a parcel with a cost' do
130
+ (Parcels.new(2).of(500) + Cost.new(:credit_card, 300)).should be 1300
131
+ end
132
+ end
133
+
134
+ describe '#to_cost' do
135
+ let(:parcels) { Parcels.new(4).of(200) }
136
+ let(:costs) { parcels.to_cost }
137
+ before(:each) do
138
+ costs.should_not be_empty
139
+ end
140
+
141
+ it 'should converting to many costs' do
142
+ parcels.to_cost.should have(4).items
143
+ end
144
+
145
+ it 'should converting to cost with the correct value' do
146
+ costs.all? {|cost| cost.value == 200 }.should be_true
147
+ end
148
+
149
+ context "with many dates" do
150
+ let(:parcels) { Parcels.new(12).of(200).every_day(4).beginning(:july) }
151
+
152
+ before(:each) do
153
+ Date.should_receive(:today).at_least(:once).and_return(Date.civil(2011, 7, 2))
154
+ Financial.locale = :en
155
+ @costs = parcels.to_cost
156
+ end
157
+
158
+ it "should return the date for the first parcel" do
159
+ @costs[0].date.should == Date.civil(2011, 7, 4)
160
+ end
161
+
162
+ it "should return the date for the second parcel" do
163
+ Financial.locale = :en
164
+ @costs[1].date.should == Date.civil(2011, 8, 4)
165
+ end
166
+
167
+ it "should return the date for the third parcel" do
168
+ Financial.locale = :en
169
+ @costs[2].date.should == Date.civil(2011, 9, 4)
170
+ end
171
+
172
+ it "should return the date for the fourth parcel" do
173
+ Financial.locale = :en
174
+ @costs[3].date.should == Date.civil(2011, 10, 4)
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe PerCent do
5
+ include Financial::PerCent
6
+
7
+ describe '#per_cent' do
8
+ it 'should return the porcentage of Fixnum value' do
9
+ 6.per_cent.should == 0.06
10
+ end
11
+
12
+ it 'should return the porcentage of Float Value' do
13
+ 64.4.per_cent.should == 0.644
14
+ end
15
+
16
+ it 'should return the porcentage in portuguese too' do
17
+ original_locale = Financial.locale
18
+ Financial.locale = :pt
19
+ 99.por_cento.should == 0.99
20
+ Financial.locale = original_locale.name
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ Financial.locale = :pt
4
+ include Financial::PerCent
5
+
6
+ conta :uma_conta_de_banco do
7
+ total -500
8
+ end
9
+
10
+ conta :banco_com_dinheiro do
11
+ total 4000
12
+
13
+ depositos do
14
+ deposite(500).na_conta(:uma_conta_de_banco).na_data('27/07/2011')
15
+ end
16
+
17
+ custos do
18
+ proxima_fatura(1000).na_data('26/07/2011')
19
+ parcelas(6).de(316).todo_dia(19)
20
+ parcelas(12).de(67).todo_dia(05).comecando_no_mes_de(:setembro)
21
+ end
22
+
23
+ faturamento do
24
+ nota_fiscal(2500).na_data('02/07/2011').imposto(6.por_cento).na_data('17/07/2011')
25
+ end
26
+ end
27
+
28
+ describe 'account :banco_com_dinheiro' do
29
+ let(:banco_com_dinheiro) { Financial::Account.find(:name => :banco_com_dinheiro) }
30
+ let(:uma_conta_de_banco) { Financial::Account.find(:name => :uma_conta_de_banco)}
31
+
32
+ before do
33
+ stub_date(1, 7, 2011)
34
+ end
35
+
36
+ describe '#name' do
37
+ it 'should have :banco_com_dinheiro as a name' do
38
+ banco_com_dinheiro.name.should equal :banco_com_dinheiro
39
+ end
40
+ end
41
+
42
+ describe '#total' do
43
+ it 'should have a total' do
44
+ banco_com_dinheiro.total.should equal 4000
45
+ end
46
+
47
+ it 'should store negative totals too' do
48
+ uma_conta_de_banco.total.should be -500
49
+ end
50
+ end
51
+
52
+ describe '#costs' do
53
+ it 'should have many costs counting taxes' do
54
+ banco_com_dinheiro.costs.should have(19).items
55
+ end
56
+
57
+ it 'all costs should be instance of Costs' do
58
+ banco_com_dinheiro.costs.all? { |cost|
59
+ cost.instance_of?(Financial::Cost)
60
+ }.should be_true
61
+ end
62
+ end
63
+
64
+ describe '#total_costs' do
65
+ it 'should calculate the total costs' do
66
+ banco_com_dinheiro.total_costs.should == 4350 # costs + deposits + taxes
67
+ end
68
+ end
69
+
70
+ describe '#revenues' do
71
+ it 'should have some revenues' do
72
+ banco_com_dinheiro.revenues.should have(1).item
73
+ end
74
+ end
75
+
76
+ describe '#deposits' do
77
+ it 'should have some deposits' do
78
+ banco_com_dinheiro.deposits.should have(1).item
79
+ end
80
+ end
81
+
82
+ describe '#total_revenues' do
83
+ it 'should calculate the total revenues' do
84
+ banco_com_dinheiro.total_revenues.should equal 2500
85
+ end
86
+ end
87
+
88
+ describe '#taxes' do
89
+ it 'should have some taxes' do
90
+ banco_com_dinheiro.taxes.should have(1).item
91
+ end
92
+ end
93
+
94
+ describe '#calculate_balances' do
95
+ before do
96
+ banco_com_dinheiro.calculate_balances
97
+ end
98
+
99
+ it 'should include balance in the date 02/07/2011' do
100
+ Financial.locale = :pt
101
+ Financial.locale.name.should be :pt
102
+ banco_com_dinheiro.balances.should include_balance(6500).in_date('02/07/2011')
103
+ end
104
+
105
+ it 'should include one balance for :uma_conta_de_banco' do
106
+ uma_conta_de_banco.balances.should have(1).items
107
+ end
108
+
109
+ it 'should include the recevied deposit from :banco_com_dinheiro' do
110
+ uma_conta_de_banco.balances.should include_balance(0).in_date('27/07/2011')
111
+ end
112
+
113
+ it 'should have many balances' do
114
+ banco_com_dinheiro.balances.should have(22).items
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe PrintTable do
5
+ let(:print_table) { PrintTable.new(:my_account) }
6
+ let(:print_all) { PrintTable.new(:all) }
7
+
8
+ describe '#account_name' do
9
+ it 'should return the name of the account to run' do
10
+ print_table.account_name.should equal :my_account
11
+ end
12
+
13
+ it 'should return all accounts to run' do
14
+ print_all.account_name.should equal :all
15
+ end
16
+ end
17
+
18
+ describe '#account' do
19
+ let(:my_bank_account) { Financial.account_manager.new_account(:my_bank_account) }
20
+
21
+ it 'should return an array of accounts instances' do
22
+ my_bank_account
23
+ PrintTable.new(:my_bank_account).account.should == [my_bank_account]
24
+ end
25
+
26
+ it 'should return all accounts when pass :all option' do
27
+ my_bank_account
28
+ print_all.account.should equal AccountManager.instance.accounts
29
+ end
30
+ end
31
+
32
+ describe '#initial_date' do
33
+ it 'should return the current date for defaults' do
34
+ print_table.initial_date.should == Date.today
35
+ end
36
+ end
37
+
38
+ describe '#final_date' do
39
+ it 'should return the current date for defaults' do
40
+ print_table.final_date.should == Date.today
41
+ end
42
+ end
43
+
44
+ describe '#from' do
45
+
46
+ it 'should be possible to use portuguese dates' do
47
+ Financial.locale = :pt
48
+ print_table.from('20/12/2011')
49
+ print_table.initial_date.should == Date.civil(2011, 12, 20)
50
+ Financial.locale = :en
51
+ Financial.locale
52
+ end
53
+
54
+ it 'should assign the @from accessor' do
55
+ print_table.from('02/02/2002')
56
+ print_table.initial_date.should == Date.civil(2002, 2, 2)
57
+ end
58
+ end
59
+
60
+ describe '#to' do
61
+
62
+ it 'should be possible to use portuguese dates' do
63
+ Financial.locale = :pt
64
+ print_table.to('20/12/2011')
65
+ print_table.final_date.should == Date.civil(2011, 12, 20)
66
+ Financial.locale = :en
67
+ Financial.locale
68
+ end
69
+
70
+ it 'should assign the @from accessor' do
71
+ print_table.to('02/02/2002')
72
+ print_table.final_date.should == Date.civil(2002, 2, 2)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ module Financial
4
+ describe Revenue do
5
+ let(:revenue) { Revenue.new(:salary, 500) }
6
+
7
+ describe '#name' do
8
+ it 'should return the name capitalized' do
9
+ revenue.name.should == "Salary"
10
+ end
11
+
12
+ it 'should return the name capitalized with spaces' do
13
+ Revenue.new(:my_precious_salary, 400).name.should == "My precious salary"
14
+ end
15
+ end
16
+
17
+ describe '#method_name' do
18
+ it 'should set the method name' do
19
+ revenue.method_name.should equal :salary
20
+ end
21
+ end
22
+
23
+ describe '#value' do
24
+ it 'should return the value' do
25
+ revenue.value.should be 500
26
+ end
27
+ end
28
+
29
+ describe '#format_value' do
30
+ it 'should format the value passing an - sign' do
31
+ Revenue.new(:cost, 400).format_value.should == "+ 400,00"
32
+ end
33
+
34
+ it 'should pass the cents value' do
35
+ Revenue.new(:super_cost, 1_000.99).format_value.should == "+ 1000,99"
36
+ end
37
+ end
38
+
39
+ describe '#date' do
40
+ it 'should return the today date' do
41
+ revenue.date.should == Date.today
42
+ end
43
+ end
44
+
45
+ describe '#in_date' do
46
+ it 'should return the date in english format' do
47
+ Financial.locale = :en
48
+ revenue.in_date('6/19/2011').date.should == Date.civil(2011, 6, 19)
49
+ end
50
+
51
+ it 'should return the date in portuguese format' do
52
+ Financial.locale = :pt
53
+ revenue.in_date('20/06/2011').date.should == Date.civil(2011, 6, 20)
54
+ end
55
+ end
56
+
57
+ describe '#tax' do
58
+ it 'should return a tax' do
59
+ revenue.tax(0.06).should be_instance_of(Financial::Tax)
60
+ end
61
+
62
+ it 'should set the method name' do
63
+ revenue.tax(1.08).method_name.should == :salary
64
+ end
65
+
66
+ it 'should set the name' do
67
+ revenue.tax(1.08).name.should == "Salary"
68
+ end
69
+
70
+ it 'should set calculate the value multiplying the revenue value by the tax' do
71
+ revenue.tax(0.12).value.should == 60.0 # 500 * 0.12
72
+ end
73
+
74
+ it 'should calculate the value decreasing the tax' do
75
+ revenue.tax(:decreases => 10).value.should == 10
76
+ end
77
+
78
+ it 'should persist the value if dont have args' do
79
+ revenue.tax(1)
80
+ revenue.tax.value.should be 500
81
+ end
82
+
83
+ it 'should set the instance variable name' do
84
+ revenue.tax(0.10)
85
+ revenue.instance_variable_get(:@tax).should be_instance_of(Financial::Tax)
86
+ end
87
+ end
88
+ end
89
+ end