roney 0.2.4
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/README.md +39 -0
- data/Rakefile +91 -0
- data/bin/console +11 -0
- data/bin/roney +4 -0
- data/bin/setup +8 -0
- data/lib/roney/cli.rb +26 -0
- data/lib/roney/command.rb +8 -0
- data/lib/roney/commands/in.rb +41 -0
- data/lib/roney/commands/init.rb +9 -0
- data/lib/roney/commands/out.rb +39 -0
- data/lib/roney/commands/remove.rb +43 -0
- data/lib/roney/commands/rollback.rb +13 -0
- data/lib/roney/commands/summary.rb +73 -0
- data/lib/roney/commands/summary_group.rb +83 -0
- data/lib/roney/database_adapter.rb +67 -0
- data/lib/roney/models/movement.rb +14 -0
- data/lib/roney/operations/base.rb +43 -0
- data/lib/roney/operations/movements/create.rb +28 -0
- data/lib/roney/operations/response.rb +62 -0
- data/lib/roney/version.rb +5 -0
- data/lib/roney.rb +26 -0
- metadata +68 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 47aaeb028c9dfd9de01f7e4cc4d7a184b5dca23aa659dcc0c50edb2e4f4dac9a
|
|
4
|
+
data.tar.gz: 8bda4900515f2fb42588680aa2b409621267f1673123125e0e0dfad111296897
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e62478bcc551faddb24317d4f24efb28db629345fc0541d7f7608e57975eb6e8397f6a4bc5825a0f408c8823a3b3315771da36ff5f6cbc7981cd1a48434e007e
|
|
7
|
+
data.tar.gz: 4c5f1cf5370b5b8fcdf1bcfcc029fda1790f5fb87cde409c5e73c5dfa19f33603f995f266f8a7a00ebe084e35ffd3005d8dabacc6eb8a34dd49452d1b619a696
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Roney
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/roney`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
14
|
+
|
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
16
|
+
|
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
TODO: Write usage instructions here
|
|
22
|
+
|
|
23
|
+
## Development
|
|
24
|
+
|
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
26
|
+
|
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
28
|
+
|
|
29
|
+
## Contributing
|
|
30
|
+
|
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/roney. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/roney/blob/master/CODE_OF_CONDUCT.md).
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
36
|
+
|
|
37
|
+
## Code of Conduct
|
|
38
|
+
|
|
39
|
+
Everyone interacting in the Roney project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/roney/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "yaml"
|
|
5
|
+
require "debug"
|
|
6
|
+
|
|
7
|
+
require "bundler/gem_tasks"
|
|
8
|
+
require "rspec/core/rake_task"
|
|
9
|
+
|
|
10
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
11
|
+
|
|
12
|
+
require "rubocop/rake_task"
|
|
13
|
+
|
|
14
|
+
RuboCop::RakeTask.new
|
|
15
|
+
|
|
16
|
+
task default: %i[spec rubocop]
|
|
17
|
+
|
|
18
|
+
require_relative "lib/roney/database_adapter"
|
|
19
|
+
|
|
20
|
+
namespace :db do
|
|
21
|
+
desc "Create the database"
|
|
22
|
+
task :create do
|
|
23
|
+
Roney::DatabaseAdapter.establish_connection
|
|
24
|
+
|
|
25
|
+
ActiveRecord::Base.connection.create_table("main")
|
|
26
|
+
ActiveRecord::Base.connection.drop_table("main")
|
|
27
|
+
puts "Database created."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
desc "Migrate the database"
|
|
31
|
+
task :migrate do
|
|
32
|
+
Roney::DatabaseAdapter.establish_connection
|
|
33
|
+
ActiveRecord::Base.connection_pool.migration_context.migrate
|
|
34
|
+
|
|
35
|
+
Rake::Task["db:schema"].invoke
|
|
36
|
+
puts "Database migrated."
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
desc "Rollback the last migration"
|
|
40
|
+
task :rollback do
|
|
41
|
+
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
|
42
|
+
|
|
43
|
+
Roney::DatabaseAdapter.establish_connection
|
|
44
|
+
ActiveRecord::Base.connection_pool.migration_context.rollback(step)
|
|
45
|
+
|
|
46
|
+
Rake::Task["db:schema"].invoke
|
|
47
|
+
puts "Database rollback."
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
desc "Drop the database"
|
|
51
|
+
task :drop do
|
|
52
|
+
Dir["storage/*"].each { |b| File.delete(b) }
|
|
53
|
+
puts "Database deleted."
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
desc "Reset the database"
|
|
57
|
+
task reset: %i[drop create migrate]
|
|
58
|
+
|
|
59
|
+
desc "Setup the database"
|
|
60
|
+
task setup: %i[create migrate]
|
|
61
|
+
|
|
62
|
+
desc "Create a db/schema.rb file that is portable against any DB supported by AR"
|
|
63
|
+
task :schema do
|
|
64
|
+
Roney::DatabaseAdapter.establish_connection
|
|
65
|
+
require "active_record/schema_dumper"
|
|
66
|
+
filename = "db/schema.rb"
|
|
67
|
+
File.open(filename, "w:utf-8") do |file|
|
|
68
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, file)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
namespace :g do
|
|
74
|
+
desc "Generate migration"
|
|
75
|
+
task :migration do
|
|
76
|
+
name = ARGV[1] || raise("Specify name: rake g:migration your_migration")
|
|
77
|
+
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
|
|
78
|
+
path = File.expand_path("../db/migrate/#{timestamp}_#{name}.rb", __FILE__)
|
|
79
|
+
migration_class = name.split("_").map(&:capitalize).join
|
|
80
|
+
|
|
81
|
+
File.write(path, <<~EOF)
|
|
82
|
+
class #{migration_class} < ActiveRecord::Migration[8.0]
|
|
83
|
+
def change
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
EOF
|
|
87
|
+
|
|
88
|
+
puts "Migration #{path} created"
|
|
89
|
+
abort # needed stop other tasks
|
|
90
|
+
end
|
|
91
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "roney"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
require "irb"
|
|
11
|
+
IRB.start(__FILE__)
|
data/bin/roney
ADDED
data/bin/setup
ADDED
data/lib/roney/cli.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "thor/rake_compat"
|
|
4
|
+
|
|
5
|
+
module Roney
|
|
6
|
+
class Cli < Thor
|
|
7
|
+
def self.exit_on_failure?
|
|
8
|
+
true
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.register(klass, command)
|
|
12
|
+
super(klass, command, [command, klass.arguments_text].join(' '), klass.desc || '')
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
register Roney::Commands::Init, "init"
|
|
16
|
+
|
|
17
|
+
register Roney::Commands::In, "in"
|
|
18
|
+
register Roney::Commands::Out, "out"
|
|
19
|
+
register Roney::Commands::Summary, "summary"
|
|
20
|
+
register Roney::Commands::Rollback, "rollback"
|
|
21
|
+
register Roney::Commands::Remove, "remove"
|
|
22
|
+
register Roney::Commands::SummaryGroup, "summarygroup"
|
|
23
|
+
|
|
24
|
+
map "trace" => "summary"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
module Roney
|
|
2
|
+
module Commands
|
|
3
|
+
class In < Roney::Command
|
|
4
|
+
argument :amount, type: :numeric, banner: 'AMOUNT'
|
|
5
|
+
argument :description
|
|
6
|
+
|
|
7
|
+
def create_movement
|
|
8
|
+
@created = ::Roney::Movement.create(
|
|
9
|
+
amount: amount,
|
|
10
|
+
description: description
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def setup_table
|
|
15
|
+
return unless @created.valid?
|
|
16
|
+
|
|
17
|
+
@table = ::Terminal::Table.new(
|
|
18
|
+
title: set_color("Movement Created Sucessfully", :green),
|
|
19
|
+
headings: %w[Id Amount Description],
|
|
20
|
+
style: {
|
|
21
|
+
border_x: "=",
|
|
22
|
+
border_i: "x"
|
|
23
|
+
},
|
|
24
|
+
rows: [[
|
|
25
|
+
@created.id,
|
|
26
|
+
set_color(@created.amount.to_fs(:currency, unit: "R$ "), @created.amount.positive? ? :green : :red),
|
|
27
|
+
@created.description
|
|
28
|
+
]]
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def handle_result
|
|
33
|
+
if @created.valid?
|
|
34
|
+
say @table.render
|
|
35
|
+
else
|
|
36
|
+
say "Error: #{@created.errors.full_messages.to_sentence}", :red
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Roney
|
|
2
|
+
module Commands
|
|
3
|
+
class Out < Roney::Command
|
|
4
|
+
argument :amount, type: :numeric, banner: "AMOUNT"
|
|
5
|
+
argument :description
|
|
6
|
+
|
|
7
|
+
def create_movement
|
|
8
|
+
@created = ::Roney::Movement.create(
|
|
9
|
+
amount: amount.to_d * -1,
|
|
10
|
+
description: description
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def setup_table
|
|
15
|
+
@table = ::Terminal::Table.new(
|
|
16
|
+
title: set_color("Movement Created Sucessfully", :green),
|
|
17
|
+
headings: %w[Id Amount Description],
|
|
18
|
+
style: {
|
|
19
|
+
border_x: "=",
|
|
20
|
+
border_i: "x"
|
|
21
|
+
},
|
|
22
|
+
rows: [[
|
|
23
|
+
@created.id,
|
|
24
|
+
set_color(@created.amount.to_fs(:currency, unit: "R$ "), @created.amount.positive? ? :green : :red),
|
|
25
|
+
@created.description
|
|
26
|
+
]]
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def handle_result
|
|
31
|
+
if @created.valid?
|
|
32
|
+
say @table.render
|
|
33
|
+
else
|
|
34
|
+
say "Error: #{@created.errors.full_messages.to_sentence}", :red
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Roney
|
|
2
|
+
module Commands
|
|
3
|
+
class Remove < Roney::Command
|
|
4
|
+
argument :id, type: :string
|
|
5
|
+
|
|
6
|
+
def find_movement
|
|
7
|
+
@movement = ::Roney::Movement.find_by(id: id)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def delete_movement
|
|
11
|
+
return unless @movement
|
|
12
|
+
|
|
13
|
+
@deleted = @movement.destroy
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def setup_table
|
|
17
|
+
return unless @deleted
|
|
18
|
+
|
|
19
|
+
@table = ::Terminal::Table.new(
|
|
20
|
+
title: set_color("Movement Deleted Sucessfully", :yellow),
|
|
21
|
+
headings: %w[Id Amount Description],
|
|
22
|
+
style: {
|
|
23
|
+
border_x: "=",
|
|
24
|
+
border_i: "x"
|
|
25
|
+
},
|
|
26
|
+
rows: [[
|
|
27
|
+
@deleted.id,
|
|
28
|
+
set_color(@deleted.amount.to_fs(:currency, unit: "R$ "), @deleted.amount.positive? ? :green : :red),
|
|
29
|
+
@deleted.description
|
|
30
|
+
]]
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def handle_result
|
|
35
|
+
if @deleted&.destroyed?
|
|
36
|
+
say @table.render
|
|
37
|
+
else
|
|
38
|
+
say "Error: #{@deleted&.errors&.full_messages&.to_sentence || 'Movement not found'}", :red
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
module Roney
|
|
2
|
+
module Commands
|
|
3
|
+
class Summary < Roney::Command
|
|
4
|
+
argument :start_text, required: false
|
|
5
|
+
argument :end_text, required: false
|
|
6
|
+
|
|
7
|
+
def define_start_time
|
|
8
|
+
@start_time = if start_text.present?
|
|
9
|
+
value = Chronic.parse(start_text)
|
|
10
|
+
|
|
11
|
+
raise "Invalid Start Date" unless value
|
|
12
|
+
|
|
13
|
+
value
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def define_end_time
|
|
18
|
+
@end_time = if end_text.present?
|
|
19
|
+
value = Chronic.parse(end_text)
|
|
20
|
+
|
|
21
|
+
raise "Invalid End Date" unless value
|
|
22
|
+
|
|
23
|
+
value
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def define_default_times
|
|
28
|
+
@start_time ||= Time.now.beginning_of_month
|
|
29
|
+
@end_time ||= Time.now
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def define_query
|
|
33
|
+
@query = ::Roney::Movement.all
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def filter_dates
|
|
37
|
+
@query = @query.where("created_at >= ?", @start_time) if @start_time
|
|
38
|
+
@query = @query.where("created_at <= ?", @end_time) if @end_time
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def setup_table
|
|
42
|
+
@table = ::Terminal::Table.new(
|
|
43
|
+
title: "Movements Summary",
|
|
44
|
+
headings: %w[Id Amount Description Inserted],
|
|
45
|
+
style: {
|
|
46
|
+
border_x: "=",
|
|
47
|
+
border_i: "x"
|
|
48
|
+
},
|
|
49
|
+
rows: @query.map do |m|
|
|
50
|
+
[
|
|
51
|
+
m.id,
|
|
52
|
+
set_color(m.amount.to_fs(:currency, unit: "R$ "), m.amount.positive? ? :green : :red),
|
|
53
|
+
m.description,
|
|
54
|
+
ChronicDuration.output((Time.now - m.created_at).to_i, format: :short)
|
|
55
|
+
]
|
|
56
|
+
end
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def load_footer
|
|
61
|
+
total = @query.sum(:amount)
|
|
62
|
+
|
|
63
|
+
@table.add_separator
|
|
64
|
+
@table.add_row [@query.count, nil, "Total",
|
|
65
|
+
set_color(total.to_fs(:currency, unit: "R$ "), total.positive? ? :green : :red)]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def render_table
|
|
69
|
+
say @table.render
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module Roney
|
|
2
|
+
module Commands
|
|
3
|
+
class SummaryGroup < Roney::Command
|
|
4
|
+
argument :start_text, required: false
|
|
5
|
+
argument :end_text, required: false
|
|
6
|
+
|
|
7
|
+
def define_start_time
|
|
8
|
+
@start_time = if start_text.present?
|
|
9
|
+
value = Chronic.parse(start_text)
|
|
10
|
+
|
|
11
|
+
raise "Invalid Start Date" unless value
|
|
12
|
+
|
|
13
|
+
value
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def define_end_time
|
|
18
|
+
@end_time = if end_text.present?
|
|
19
|
+
value = Chronic.parse(end_text)
|
|
20
|
+
|
|
21
|
+
raise "Invalid End Date" unless value
|
|
22
|
+
|
|
23
|
+
value
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def define_default_times
|
|
28
|
+
@start_time ||= Time.now.beginning_of_month
|
|
29
|
+
@end_time ||= Time.now
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def define_query
|
|
33
|
+
@query = ::Roney::Movement
|
|
34
|
+
.select('
|
|
35
|
+
parameterized_description,
|
|
36
|
+
SUM(amount) AS total_amount,
|
|
37
|
+
COUNT(*) AS count
|
|
38
|
+
')
|
|
39
|
+
.group('parameterized_description')
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def filter_dates
|
|
43
|
+
@query = @query.where("created_at >= ?", @start_time) if @start_time
|
|
44
|
+
@query = @query.where("created_at <= ?", @end_time) if @end_time
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def setup_table
|
|
48
|
+
@table = ::Terminal::Table.new(
|
|
49
|
+
title: "Movements Summary Group",
|
|
50
|
+
headings: %w[Description TotalAmount Count],
|
|
51
|
+
style: {
|
|
52
|
+
border_x: "=",
|
|
53
|
+
border_i: "x"
|
|
54
|
+
},
|
|
55
|
+
rows: @query.sort_by {|a| a[:total_amount]}.map do |m|
|
|
56
|
+
[
|
|
57
|
+
m[:parameterized_description].titlecase,
|
|
58
|
+
set_color(m[:total_amount].to_fs(:currency, unit: "R$ "), m[:total_amount].positive? ? :green : :red),
|
|
59
|
+
m[:count]
|
|
60
|
+
]
|
|
61
|
+
end
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def load_footer
|
|
66
|
+
total = @query
|
|
67
|
+
.unscope(:group, :select)
|
|
68
|
+
.pick(Arel.sql('SUM(amount), COUNT(*)'))
|
|
69
|
+
|
|
70
|
+
@table.add_separator
|
|
71
|
+
@table.add_row [
|
|
72
|
+
'Total',
|
|
73
|
+
set_color(total.first.to_d.to_fs(:currency, unit: "R$ "), total.first.to_d.positive? ? :green : :red),
|
|
74
|
+
total.last
|
|
75
|
+
]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def render_table
|
|
79
|
+
say @table.render
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Roney
|
|
4
|
+
class DatabaseAdapter
|
|
5
|
+
class << self
|
|
6
|
+
def config_file_path
|
|
7
|
+
@config_file_path ||= Dir[File.join(Dir.home, ".config/roney/config.yml")].first
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def database_config
|
|
11
|
+
unless @database_config
|
|
12
|
+
@database_config = YAML.safe_load(File.open(config_file_path))
|
|
13
|
+
|
|
14
|
+
if @database_config["database"]["relative_location"]
|
|
15
|
+
@database_config["database"].merge!("database" => File.expand_path(
|
|
16
|
+
"../#{@database_config["database"]["database"]}", config_file_path
|
|
17
|
+
))
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
@database_config
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def establish_connection
|
|
25
|
+
ActiveRecord::Base.establish_connection(database_config["database"])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def connection
|
|
29
|
+
ActiveRecord::Base.connection
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def create_database
|
|
33
|
+
establish_connection
|
|
34
|
+
|
|
35
|
+
connection.create_table("main")
|
|
36
|
+
connection.drop_table("main")
|
|
37
|
+
|
|
38
|
+
puts "Database created."
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def migrate_database
|
|
42
|
+
establish_connection
|
|
43
|
+
|
|
44
|
+
ActiveRecord::Base.connection_pool.migration_context.migrate
|
|
45
|
+
|
|
46
|
+
schema_load
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def schema_load
|
|
50
|
+
establish_connection
|
|
51
|
+
|
|
52
|
+
require "active_record/schema_dumper"
|
|
53
|
+
|
|
54
|
+
filename = "db/schema.rb"
|
|
55
|
+
File.open(filename, "w:utf-8") do |file|
|
|
56
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, file)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def setup_database
|
|
61
|
+
create_database
|
|
62
|
+
migrate_database
|
|
63
|
+
schema_load
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Roney
|
|
4
|
+
class Movement < ActiveRecord::Base
|
|
5
|
+
validates :amount, presence: true
|
|
6
|
+
validates :description, presence: true
|
|
7
|
+
|
|
8
|
+
before_validation :assign_parameterized_description
|
|
9
|
+
|
|
10
|
+
def assign_parameterized_description
|
|
11
|
+
self.parameterized_description = description.parameterize
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Roney
|
|
4
|
+
module Operations
|
|
5
|
+
class Base
|
|
6
|
+
def self.call(params:)
|
|
7
|
+
operation_instance = new
|
|
8
|
+
|
|
9
|
+
operation_instance.call(params)
|
|
10
|
+
rescue StandardError => e
|
|
11
|
+
operation_instance.failure(e)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def success(payload = nil)
|
|
15
|
+
Response.new(true, payload, self.class)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def failure(exception, status = :unprocessable_entity, _options = {})
|
|
19
|
+
Response.new(false, nil, self.class, exception, status)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Overriding the method missing to create a new variable in a
|
|
23
|
+
# operation when call set_<variable_name>(value)
|
|
24
|
+
def method_missing(method_name, *args, &block)
|
|
25
|
+
if method_name.start_with?("set_")
|
|
26
|
+
new_method_name = method_name.to_s.gsub("set_", "")
|
|
27
|
+
new_instace_variable_name = "@#{new_method_name}"
|
|
28
|
+
return_value = args.first
|
|
29
|
+
|
|
30
|
+
instance_variable_set(new_instace_variable_name, return_value)
|
|
31
|
+
else
|
|
32
|
+
super
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def respond_to_missing?(name, _include_private)
|
|
37
|
+
name.starts_with?("set_")
|
|
38
|
+
|
|
39
|
+
super
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Roney
|
|
4
|
+
module Operations
|
|
5
|
+
module Movements
|
|
6
|
+
class Create < Base
|
|
7
|
+
def call(params)
|
|
8
|
+
set_params params
|
|
9
|
+
|
|
10
|
+
define_create_params
|
|
11
|
+
create_movement
|
|
12
|
+
|
|
13
|
+
success @movement
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def define_create_params
|
|
19
|
+
@create_params = @params.slice(:amount)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def create_movement
|
|
23
|
+
@movement = Movement.create!(@create_params)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Roney
|
|
4
|
+
module Operations
|
|
5
|
+
class Response
|
|
6
|
+
attr_accessor :payload, :error, :status, :operation_class
|
|
7
|
+
|
|
8
|
+
def initialize(was_success, payload, operation_class, error = nil, status = nil)
|
|
9
|
+
@was_success = was_success
|
|
10
|
+
@payload = payload
|
|
11
|
+
@error = error
|
|
12
|
+
@status = status
|
|
13
|
+
@operation_class = operation_class
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def success?
|
|
17
|
+
@was_success
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def failure?
|
|
21
|
+
!@was_success
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def failure_data
|
|
25
|
+
return unless error
|
|
26
|
+
|
|
27
|
+
{
|
|
28
|
+
failed_step: find_last_executed_method_from_backtrace,
|
|
29
|
+
failure_message: error.message,
|
|
30
|
+
error_class: error.class.name,
|
|
31
|
+
operation_class: operation_class
|
|
32
|
+
}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def find_last_executed_method_from_backtrace
|
|
38
|
+
return unless error&.backtrace
|
|
39
|
+
|
|
40
|
+
main_regex = map_steps_to_regex
|
|
41
|
+
|
|
42
|
+
filtered_backtrace = error.backtrace.select { |line| line.match(main_regex) }
|
|
43
|
+
|
|
44
|
+
filtered_backtrace.first[/`([^']*)'/, 1]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def map_steps_to_regex
|
|
48
|
+
regex_map = operation_steps.map(&method(:map_step_to_regex))
|
|
49
|
+
|
|
50
|
+
Regexp.union(regex_map)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def map_step_to_regex(step_name)
|
|
54
|
+
/`(#{step_name})'/
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def operation_steps
|
|
58
|
+
operation_class.instance_methods(false) + operation_class.private_instance_methods(false)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
data/lib/roney.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_record"
|
|
4
|
+
require "thor"
|
|
5
|
+
require "yaml"
|
|
6
|
+
require "chronic"
|
|
7
|
+
require "chronic_duration"
|
|
8
|
+
require "terminal-table"
|
|
9
|
+
require "debug"
|
|
10
|
+
|
|
11
|
+
module Roney
|
|
12
|
+
class Error < StandardError; end
|
|
13
|
+
|
|
14
|
+
require_relative "roney/command"
|
|
15
|
+
|
|
16
|
+
Dir[File.expand_path("../lib/roney/commands/*", __dir__)].each { |b| require_relative(b) }
|
|
17
|
+
Dir[File.expand_path("../lib/roney/models/*", __dir__)].each { |b| require_relative(b) }
|
|
18
|
+
|
|
19
|
+
require_relative "roney/version"
|
|
20
|
+
require_relative "roney/cli"
|
|
21
|
+
require_relative "roney/database_adapter"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
Roney::DatabaseAdapter.establish_connection
|
|
25
|
+
|
|
26
|
+
Roney::Cli.start(ARGV)
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: roney
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.4
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- albertalef
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: With Roney!
|
|
13
|
+
email:
|
|
14
|
+
- albertalef@protonmail.com
|
|
15
|
+
executables:
|
|
16
|
+
- roney
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- CHANGELOG.md
|
|
21
|
+
- README.md
|
|
22
|
+
- Rakefile
|
|
23
|
+
- bin/console
|
|
24
|
+
- bin/roney
|
|
25
|
+
- bin/setup
|
|
26
|
+
- lib/roney.rb
|
|
27
|
+
- lib/roney/cli.rb
|
|
28
|
+
- lib/roney/command.rb
|
|
29
|
+
- lib/roney/commands/in.rb
|
|
30
|
+
- lib/roney/commands/init.rb
|
|
31
|
+
- lib/roney/commands/out.rb
|
|
32
|
+
- lib/roney/commands/remove.rb
|
|
33
|
+
- lib/roney/commands/rollback.rb
|
|
34
|
+
- lib/roney/commands/summary.rb
|
|
35
|
+
- lib/roney/commands/summary_group.rb
|
|
36
|
+
- lib/roney/database_adapter.rb
|
|
37
|
+
- lib/roney/models/movement.rb
|
|
38
|
+
- lib/roney/operations/base.rb
|
|
39
|
+
- lib/roney/operations/movements/create.rb
|
|
40
|
+
- lib/roney/operations/response.rb
|
|
41
|
+
- lib/roney/version.rb
|
|
42
|
+
homepage: https://github.com/albertalef/roney
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata:
|
|
46
|
+
allowed_push_host: https://rubygems.org
|
|
47
|
+
homepage_uri: https://github.com/albertalef/roney
|
|
48
|
+
source_code_uri: https://github.com/albertalef/roney
|
|
49
|
+
changelog_uri: https://github.com/albertalef/roney
|
|
50
|
+
rubygems_mfa_required: 'true'
|
|
51
|
+
rdoc_options: []
|
|
52
|
+
require_paths:
|
|
53
|
+
- lib
|
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: 2.6.0
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
requirements:
|
|
61
|
+
- - ">="
|
|
62
|
+
- !ruby/object:Gem::Version
|
|
63
|
+
version: '0'
|
|
64
|
+
requirements: []
|
|
65
|
+
rubygems_version: 3.7.2
|
|
66
|
+
specification_version: 4
|
|
67
|
+
summary: Controll you money
|
|
68
|
+
test_files: []
|