lannister 0.0.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 26b4d2eea11ee88c67e74ddde6f5bb72ae26774e
4
+ data.tar.gz: cde821cd33e2f5b39cee02b226653cb04fe2e6f1
5
+ SHA512:
6
+ metadata.gz: 27338b9a784888d46e516c8fe939d7cb302148ad9b84e6e8d67c00651285a92fa248fe023b21cf7220ac7e3a97a3b084889db2d4640ca5fea56761f293d75e58
7
+ data.tar.gz: 27a66f736be3273a4fe24b8958ff97f495e0bf52a5cca5ad57d7a69ecdcb84aef74e31a57a04f3543b96b45da249d54612f81ac179f3ba842844cc8fbd9c3bbf
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #gem 'rails', '~> 4.2.0'
4
+ #gem 'railties'
5
+ #gem 'accounting', path: '../../engines/accounting'
6
+
7
+ #group :test do
8
+ #gem 'sqlite3'
9
+ #end
10
+
11
+ # Specify your gem's dependencies in lannister.gemspec
12
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Fabiano Beselga
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Lannister
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lannister'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lannister
20
+
21
+ ## Usage
22
+
23
+ Transfer money:
24
+
25
+ ```ruby
26
+ Lannister.transfer_money(source_account_id: 42,
27
+ destination_account_id: 22,
28
+ amount: 100_000)
29
+ ```
30
+
31
+ Get balance:
32
+
33
+ ```ruby
34
+ Lannister.get_balance(account_id: 42)
35
+ ```
36
+
37
+ ## TODO
38
+
39
+ * print money use case (a.k.a credit)
40
+ * explain about the repository
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/lannister/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lannister.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lannister/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lannister"
8
+ spec.version = Lannister::VERSION
9
+ spec.authors = ["Fabiano Beselga"]
10
+ spec.email = ["fabianobeselga@gmail.com"]
11
+ spec.summary = %q{ PoC of an accounting system using Clean Architecture}
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+
26
+ spec.add_dependency "caze"
27
+ end
data/lib/lannister.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'lannister/version'
2
+
3
+ require 'lannister/entities/transaction'
4
+ require 'lannister/repositories/transaction_repo'
5
+
6
+ #require 'lannister/use_cases'
7
+ require 'lannister/use_cases/get_balance'
8
+ require 'lannister/use_cases/transfer_money'
9
+
10
+ # WITH the engine dependency
11
+ #require 'rails/railties'
12
+ #require 'active_record'
13
+ #require 'accounting'
14
+ #require 'accounting/engine'
15
+ #require "#{Accounting::Engine.root}/app/models/accounting/transaction"
16
+ #require "#{Accounting::Engine.root}/app/repositories/accounting/transaction_repo"
17
+
18
+ require 'caze'
19
+
20
+ module Lannister
21
+ include Caze
22
+ define_use_cases transfer_money: UseCases::TransferMoney,
23
+ get_balance: UseCases::GetBalance
24
+
25
+ extend Forwardable
26
+
27
+ class << self
28
+ attr_writer :transaction_repo
29
+
30
+ def transaction_repo
31
+ @transaction_repo ||= Repositories::TransactionRepo.new
32
+ #@transaction_repo ||= Accounting::TransactionRepo
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module Lannister
2
+ module Entities
3
+ class Transaction
4
+ attr_accessor :id, :account_id, :amount
5
+
6
+ def initialize(attributes = {})
7
+ @id, @account_id, @amount = attributes.values_at(:id, :account_id, :amount)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ module Lannister
2
+ module Repositories
3
+ class TransactionRepo
4
+ def initialize
5
+ @rows = {}
6
+ @id = 0
7
+ end
8
+
9
+ def persist(entity)
10
+ @id += 1
11
+ entity.id = @id
12
+ rows[@id] = entity
13
+ entity
14
+ end
15
+
16
+ def count
17
+ rows.length
18
+ end
19
+
20
+ def balance(account_id:)
21
+ rows.select{ |_, row| row.account_id == account_id }.map{ |_, row| row.amount }.reduce(:+) || 0
22
+ end
23
+
24
+ def delete_all
25
+ @rows = {}
26
+ @id = 0
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :rows
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ require 'lannister/use_cases/get_balance'
2
+ require 'lannister/use_cases/transfer_money'
3
+
4
+ module Lannister
5
+ module UseCases
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Lannister
2
+ module UseCases
3
+ class GetBalance
4
+ def self.get_balance(account_id:)
5
+ Lannister.transaction_repo.balance(account_id: account_id)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,42 @@
1
+ require 'caze'
2
+ require 'forwardable'
3
+
4
+ module Lannister
5
+ module UseCases
6
+ class TransferMoney
7
+ include Caze
8
+ define_entry_point :transfer, as: :execute #, use_transaction: true
9
+ define_entry_point :transfer, as: :transfer_money #, use_transaction: true
10
+
11
+ extend Forwardable
12
+ def_delegators :Lannister, :get_balance, :transaction_repo
13
+
14
+ def initialize(source_account_id:, destination_account_id:, amount:)
15
+ @source_account_id = source_account_id
16
+ @destination_account_id = destination_account_id
17
+ @amount = amount
18
+ end
19
+
20
+ def transfer
21
+ if get_balance(account_id: source_account_id) >= amount
22
+ transaction_repo.persist(debit_transaction)
23
+ transaction_repo.persist(credit_transaction)
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def debit_transaction
32
+ Entities::Transaction.new(account_id: source_account_id, amount: - amount)
33
+ end
34
+
35
+ def credit_transaction
36
+ Entities::Transaction.new(account_id: destination_account_id, amount: amount)
37
+ end
38
+
39
+ attr_reader :source_account_id, :destination_account_id, :amount
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Lannister
2
+ VERSION = "0.0.1.alpha"
3
+ end
@@ -0,0 +1,100 @@
1
+ require 'lannister'
2
+ require 'pry'
3
+
4
+ # WITH the engine dependency
5
+ #ActiveRecord::Base.establish_connection(database: ":memory:", adapter: "sqlite3")
6
+ #migrations_path = Accounting::Engine.paths['db/migrate'].existent
7
+ #ActiveRecord::Migrator.migrate(migrations_path)
8
+
9
+ # This file was generated by the `rspec --init` command. Conventionally, all
10
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
11
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
12
+ # file to always be loaded, without a need to explicitly require it in any files.
13
+ #
14
+ # Given that it is always loaded, you are encouraged to keep this file as
15
+ # light-weight as possible. Requiring heavyweight dependencies from this file
16
+ # will add to the boot time of your test suite on EVERY test run, even for an
17
+ # individual file that may not need all of that loaded. Instead, consider making
18
+ # a separate helper file that requires the additional dependencies and performs
19
+ # the additional setup, and require it from the spec files that actually need it.
20
+ #
21
+ # The `.rspec` file also contains a few flags that are not defaults but that
22
+ # users commonly want.
23
+ #
24
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
25
+ RSpec.configure do |config|
26
+ config.before do
27
+ Lannister.transaction_repo.delete_all
28
+ end
29
+ # rspec-expectations config goes here. You can use an alternate
30
+ # assertion/expectation library such as wrong or the stdlib/minitest
31
+ # assertions if you prefer.
32
+ config.expect_with :rspec do |expectations|
33
+ # This option will default to `true` in RSpec 4. It makes the `description`
34
+ # and `failure_message` of custom matchers include text for helper methods
35
+ # defined using `chain`, e.g.:
36
+ # be_bigger_than(2).and_smaller_than(4).description
37
+ # # => "be bigger than 2 and smaller than 4"
38
+ # ...rather than:
39
+ # # => "be bigger than 2"
40
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
41
+ end
42
+
43
+ # rspec-mocks config goes here. You can use an alternate test double
44
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
45
+ config.mock_with :rspec do |mocks|
46
+ # Prevents you from mocking or stubbing a method that does not exist on
47
+ # a real object. This is generally recommended, and will default to
48
+ # `true` in RSpec 4.
49
+ mocks.verify_partial_doubles = true
50
+ end
51
+
52
+ # The settings below are suggested to provide a good initial experience
53
+ # with RSpec, but feel free to customize to your heart's content.
54
+ =begin
55
+ # These two settings work together to allow you to limit a spec run
56
+ # to individual examples or groups you care about by tagging them with
57
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
58
+ # get run.
59
+ config.filter_run :focus
60
+ config.run_all_when_everything_filtered = true
61
+
62
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
63
+ # For more details, see:
64
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
65
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
66
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = 'doc'
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+
88
+ # Run specs in random order to surface order dependencies. If you find an
89
+ # order dependency and want to debug it, you can fix the order by providing
90
+ # the seed, which is printed after each run.
91
+ # --seed 1234
92
+ config.order = :random
93
+
94
+ # Seed global randomization in this process using the `--seed` CLI option.
95
+ # Setting this allows you to use `--seed` to deterministically reproduce
96
+ # test failures related to randomization by passing the same `--seed` value
97
+ # as the one that triggered the failure.
98
+ Kernel.srand config.seed
99
+ =end
100
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module Lannister
4
+ module UseCases
5
+ describe GetBalance do
6
+ describe '.get_balance' do
7
+ subject(:get_balance) { described_class.get_balance(account_id: account_id) }
8
+
9
+ let(:account_id) { 1 }
10
+ let(:amount) { 10_000 }
11
+ let(:transaction) { Entities::Transaction.new(account_id: account_id, amount: amount) }
12
+
13
+ context 'when there is only one credit transaction' do
14
+ before { Lannister.transaction_repo.persist(transaction) }
15
+
16
+ it { expect(get_balance).to eq(amount) }
17
+ end
18
+
19
+ context 'when there are two credit transactions' do
20
+ let(:another_amount) { 2_200 }
21
+ let(:another_transaction) { Entities::Transaction.new(account_id: account_id, amount: another_amount) }
22
+ before { [transaction, another_transaction].each { |t| Lannister.transaction_repo.persist(t) } }
23
+
24
+ it { expect(get_balance).to eq(amount + another_amount) }
25
+ end
26
+
27
+ context 'when there is a credit and a debit' do
28
+ let(:debit_amount) { -500 }
29
+ let(:debit_transaction) { Entities::Transaction.new(account_id: account_id, amount: debit_amount) }
30
+ before { [transaction, debit_transaction].each { |t| Lannister.transaction_repo.persist(t) } }
31
+
32
+ it { expect(get_balance).to eq(amount + debit_amount) }
33
+ end
34
+
35
+ context 'transactions from another accounts' do
36
+ let(:debit_amount) { -500 }
37
+ let(:debit_transaction) { Entities::Transaction.new(account_id: 33, amount: debit_amount) }
38
+ before { [transaction, debit_transaction].each { |t| Lannister.transaction_repo.persist(t) } }
39
+
40
+ it 'ignores them' do
41
+ expect(get_balance).to eq(amount)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ module Lannister
4
+ module UseCases
5
+ describe TransferMoney do
6
+ describe '.transfer' do
7
+ subject(:transfer) { described_class.transfer_money(source_account_id: source_account_id,
8
+ destination_account_id: destination_account_id,
9
+ amount: amount ) }
10
+ let(:source_account_id) { 1 }
11
+ let(:destination_account_id) { 2 }
12
+ let(:amount) { 1_000 }
13
+
14
+ def get_balance(account_id)
15
+ Lannister.get_balance(account_id: account_id)
16
+ end
17
+
18
+ context 'when there is enough money on the source account' do
19
+ let(:previous_transaction) { Entities::Transaction.new(account_id: source_account_id,
20
+ amount: amount) }
21
+
22
+ before { Lannister.transaction_repo.persist(previous_transaction) }
23
+
24
+ it 'debits the specified amount from source account' do
25
+ expect{ transfer }.to change{ get_balance(source_account_id) }.by(- amount)
26
+ end
27
+
28
+ it 'credits the specific amount to destination account' do
29
+ expect{ transfer }.to change{ get_balance(destination_account_id) }.by(amount)
30
+ end
31
+ end
32
+
33
+ context 'there is not enough money on source account' do
34
+ it 'cancels the transfer by responding false' do
35
+ expect(transfer).to eq(false)
36
+ end
37
+
38
+ it 'does not change the accounts balance' do
39
+ expect{ transfer }.to_not change{ get_balance(source_account_id) }
40
+ expect{ transfer }.to_not change{ get_balance(destination_account_id) }
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lannister
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Fabiano Beselga
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: caze
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: PoC of an accounting system using Clean Architecture
84
+ email:
85
+ - fabianobeselga@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lannister.gemspec
97
+ - lib/lannister.rb
98
+ - lib/lannister/entities/transaction.rb
99
+ - lib/lannister/repositories/transaction_repo.rb
100
+ - lib/lannister/use_cases.rb
101
+ - lib/lannister/use_cases/get_balance.rb
102
+ - lib/lannister/use_cases/transfer_money.rb
103
+ - lib/lannister/version.rb
104
+ - spec/spec_helper.rb
105
+ - spec/use_cases/get_balance_spec.rb
106
+ - spec/use_cases/transfer_money_spec.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">"
123
+ - !ruby/object:Gem::Version
124
+ version: 1.3.1
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: PoC of an accounting system using Clean Architecture
131
+ test_files:
132
+ - spec/spec_helper.rb
133
+ - spec/use_cases/get_balance_spec.rb
134
+ - spec/use_cases/transfer_money_spec.rb