ookkee 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fb4a26de7169c37a1386813145c12defcfd04e2df815eeab1cc6dcd9d37d2ddc
4
+ data.tar.gz: da78b32b53e588c51d45c55262541dc6cc78867876cb5d2a108d962b1731de52
5
+ SHA512:
6
+ metadata.gz: 5affb76e53ba7076f4a8ae955e4482703e25e0755b55e7a1c7398a2958fdfc27baa164a95dbbbfdbad24e17ec9ca8ee218af4263a475b55762fdf13f7d205686
7
+ data.tar.gz: 39062201fc1c2b071820f79fba949a679758db9d085a2208b463c9bc7ab3d3db4e099a8cc63aac916ee8dafccf03bea997dc50403beac394ac20e5ae8da0d6e8
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Sen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Ookkee
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ookkee'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install ookkee
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Ookkee'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,15 @@
1
+ require "ookkee/railtie"
2
+
3
+ require 'ookkee/model/entry'
4
+ require 'ookkee/model/sheet'
5
+ require 'ookkee/model/account'
6
+ require 'ookkee/builder'
7
+ require 'ookkee/builder_proxy'
8
+ require 'ookkee/factory'
9
+ # require 'ookkee/stack'
10
+ require 'ookkee/repos/entry_repo'
11
+ require 'ookkee/usecase'
12
+
13
+ module Ookkee
14
+ # Your code goes here...
15
+ end
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Asset
3
+ end
4
+ end
@@ -0,0 +1,58 @@
1
+ module Ookkee
2
+ class Builder
3
+ attr_accessor :registry
4
+
5
+ def initialize
6
+ @registry = []
7
+ @attributes = {}
8
+ end
9
+
10
+ def self.build
11
+ instance = self.new
12
+ yield instance
13
+
14
+ instance.build_activerecord_objects
15
+ end
16
+
17
+ def title(value)
18
+ @attributes[:title] = value
19
+ end
20
+
21
+ def transaction_number(value)
22
+ @attributes[:transaction_number] = value
23
+ end
24
+
25
+ def user(value)
26
+ @attributes[:user] = value
27
+ end
28
+
29
+ def credit(account, &block)
30
+ builder_proxy = BuilderProxy.new({builder: self})
31
+ builder_proxy.credit(account, &block)
32
+ end
33
+
34
+ def debit(account, &block)
35
+ builder_proxy = BuilderProxy.new({builder: self})
36
+ builder_proxy.debit(account, &block)
37
+ end
38
+
39
+ def build_activerecord_objects
40
+ sheet = Sheet.new(
41
+ title: @attributes[:title],
42
+ transaction_number: @attributes[:transaction_number],
43
+ user: @attributes[:user]
44
+ )
45
+ registry.each do |element|
46
+ entry_repo.build_from_factory(sheet, element)
47
+ end
48
+
49
+ sheet
50
+ end
51
+
52
+ private
53
+
54
+ def entry_repo
55
+ @entry_repo ||= EntryRepo.new
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ module Ookkee
2
+ class BuilderProxy
3
+ def initialize(options)
4
+ @builder = options[:builder]
5
+ end
6
+
7
+ def credit(account)
8
+ factory = Factory.new
9
+ yield factory
10
+ factory.entry_type 'credit'
11
+ factory.account account
12
+ factory.sheet_name account.sheet_name
13
+
14
+ @builder.registry << factory
15
+ end
16
+
17
+ def debit(account)
18
+ factory = Factory.new
19
+ yield factory
20
+ factory.entry_type 'debit'
21
+ factory.account account
22
+ factory.sheet_name account.sheet_name
23
+
24
+ @builder.registry << factory
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Equity
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Expense < Sheet
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Ookkee
2
+ class Factory < BasicObject
3
+ def initialize
4
+ @attributes = {}
5
+ end
6
+
7
+ attr_reader :attributes
8
+
9
+ def method_missing(name, *args, &block)
10
+ @attributes[name] = args[0]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Liability < Sheet
3
+ end
4
+ end
@@ -0,0 +1,12 @@
1
+ module Ookkee
2
+ class Account < ActiveRecord::Base
3
+ self.table_name = "ookkee_accounts"
4
+
5
+ validates :name, presence: true
6
+ validates :sheet_name, presence: true, \
7
+ inclusion: {
8
+ in: Entry::SHEET_NAMES,
9
+ message: "%{value} is not a valid account name"
10
+ }
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ module Ookkee
2
+ class Entry < ActiveRecord::Base
3
+ self.table_name = "ookkee_entries"
4
+
5
+ ENTRY_TYPES = %w[credit debit]
6
+ SHEET_NAMES = %w[assets liabilities equity revenue expenses]
7
+
8
+ belongs_to :sheet, class_name: 'Ookkee::Sheet', foreign_key: :sheet_id
9
+ belongs_to :account, class_name: 'Ookkee::Account', foreign_key: :account_id
10
+ belongs_to :trackable, polymorphic: true
11
+
12
+ validates :entry_type, presence: true, \
13
+ inclusion: {
14
+ in: ENTRY_TYPES,
15
+ message: "%{value} is not a valid entry type"
16
+ }
17
+ validates :sheet_name, presence: true, \
18
+ inclusion: {
19
+ in: SHEET_NAMES,
20
+ message: "%{value} is not a valid account name"
21
+ }
22
+ end
23
+ end
@@ -0,0 +1,54 @@
1
+ module Ookkee
2
+ class Sheet < ActiveRecord::Base
3
+ self.table_name = "ookkee_sheets"
4
+
5
+ has_many :entries, class_name: 'Ookkee::Entry', foreign_key: :sheet_id
6
+ belongs_to :user, polymorphic: true
7
+
8
+ validates :title, presence: true
9
+ validate :credit_and_debit_must_be_exists
10
+ validate :ledger_equalities
11
+
12
+ Entry::ENTRY_TYPES.each do |type|
13
+ define_method "#{type}_filter" do |*args|
14
+ args[0].select { |entry| entry.entry_type == type }
15
+ end
16
+ end
17
+
18
+ Entry::SHEET_NAMES.each do |name|
19
+ define_method "#{name}_filter" do |*args|
20
+ args[0].select { |entry| entry.sheet_name == name }
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def credit_and_debit_must_be_exists
27
+ if credit_filter(entries).count == 0 || debit_filter(entries).count == 0
28
+ errors.add(:base, "credit and debut must be exists")
29
+ end
30
+ end
31
+
32
+ def ledger_equalities
33
+ assets = ledger_left(assets_filter(entries))
34
+ expenses = ledger_left(expenses_filter(entries))
35
+ liabilities = ledger_right(liabilities_filter(entries))
36
+ equity = ledger_right(equity_filter(entries))
37
+ revenue = ledger_right(revenue_filter(entries))
38
+
39
+ if assets + expenses != liabilities + equity + revenue
40
+ errors.add(:base, "credits amount must equal to debits amount")
41
+ end
42
+ end
43
+
44
+ def ledger_left(collection)
45
+ debits = debit_filter(collection).inject(0) { |sum, entry| sum + entry.amount }
46
+ credits = credit_filter(collection).inject(0) { |sum, entry| sum + entry.amount }
47
+ debits - credits
48
+ end
49
+
50
+ def ledger_right(collection)
51
+ -1 * ledger_left(collection)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
File without changes
@@ -0,0 +1,28 @@
1
+ require 'bigdecimal'
2
+
3
+ module Ookkee
4
+ class EntryRepo
5
+ def build_from_factory(sheet, factory)
6
+ entry = sheet.entries.build
7
+ entry.account = factory.attributes[:account]
8
+ entry.trackable = factory.attributes[:trackable]
9
+ entry.sheet_name = factory.attributes[:sheet_name]
10
+ entry.entry_type = factory.attributes[:entry_type]
11
+ entry.amount = BigDecimal(factory.attributes[:amount] || 0)
12
+
13
+ entry
14
+ end
15
+
16
+ def account_entries_with_user(account, user)
17
+ Ookkee::Entry.joins(:account, :sheet)
18
+ .where(sheet_name: account.sheet_name)
19
+ .where('ookkee_accounts.name = ?', account.name)
20
+ .where('ookkee_sheets.user_id = ? and ookkee_sheets.user_type = ?', user.id, user.class.name)
21
+ .order('ookkee_entries.created_at DESC')
22
+ end
23
+
24
+ def calculate_account_balance(account, user)
25
+ account_entries_with_user(account, user).where('entry_type = ?', 'debit').sum(:amount) - entries.where('entry_type = ?', 'credit').sum(:amount)
26
+ end
27
+ end
28
+ end
File without changes
@@ -0,0 +1,4 @@
1
+ module Ookkee
2
+ class Revenue < Sheet
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Ookkee
2
+ class Usecase
3
+ def self.account_balance_with_user(account, user)
4
+ repo = EntryRepo.new
5
+ repo.calculate_account_balance_with_user(account, user)
6
+ end
7
+
8
+ def self.account_entries(account, user)
9
+ repo = EntryRepo.new
10
+ repo.account_entries_with_user(account, user)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module Ookkee
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,6 @@
1
+ # desc "Explaining what the task does"
2
+ namespace :ookkee do
3
+ task :install do
4
+
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ookkee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Double entry bookkeeping.
42
+ email:
43
+ - sen9ob@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/ookkee.rb
52
+ - lib/ookkee/asset.rb
53
+ - lib/ookkee/builder.rb
54
+ - lib/ookkee/builder_proxy.rb
55
+ - lib/ookkee/equity.rb
56
+ - lib/ookkee/expense.rb
57
+ - lib/ookkee/factory.rb
58
+ - lib/ookkee/liability.rb
59
+ - lib/ookkee/model/account.rb
60
+ - lib/ookkee/model/entry.rb
61
+ - lib/ookkee/model/sheet.rb
62
+ - lib/ookkee/railtie.rb
63
+ - lib/ookkee/repos/account_repo.rb
64
+ - lib/ookkee/repos/entry_repo.rb
65
+ - lib/ookkee/repos/sheet_repo.rb
66
+ - lib/ookkee/revenue.rb
67
+ - lib/ookkee/usecase.rb
68
+ - lib/ookkee/validators/account_validator.rb
69
+ - lib/ookkee/validators/sheet_validator.rb
70
+ - lib/ookkee/version.rb
71
+ - lib/tasks/ookkee_tasks.rake
72
+ homepage: https://github.com/Sen/ookkee
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ allowed_push_host: https://RubyGems.org
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubygems_version: 3.0.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Bookeeping
96
+ test_files: []