command_extension 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.rubocop.yml +6 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +113 -0
- data/Rakefile +8 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/command_extension.gemspec +33 -0
- data/lib/command_extension.rb +8 -0
- data/lib/command_extension/after_commit.rb +79 -0
- data/lib/command_extension/base.rb +15 -0
- data/lib/command_extension/version.rb +5 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b336b279c2e6e19e9e04149abc4ba63b82ab60f253aea23026fa2f4ab86d02d1
|
4
|
+
data.tar.gz: 4165a879da0fb89326db030b811d38b6d782edd07bb1f1e3af3cedd38ae048a9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 00e3a9c43047cb206071cc6170eb6834a62b4d6679461bbbc26a7ec0608351125419eb126a02c92620b263618b70e1d76d7b18ff6675328e0f63ef54df239a13
|
7
|
+
data.tar.gz: 454ec55547de9ad7163dcd6b95bfa24e1216a4b9b3bd344eec4c46c154bdaf375b65dafa002d1a437cd0b2c37b23eedd0ab43cca6c4d02706f3a5e21d11bca57
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in command_extension.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem 'rubocop'
|
12
|
+
gem 'simplecov'
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Meyvn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
# CommandExtension
|
2
|
+
|
3
|
+
Gem will help if you have a lot of commands with delayed calls, for example delayed jobs or sidekiq, and you want them to be executed only after the AR transaction is completed.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'command_extension'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install command_extension
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Here's a basic example of a command
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class YourCommand
|
27
|
+
include CommandExtension::Base
|
28
|
+
include CommandExtension::AfterCommit
|
29
|
+
|
30
|
+
after_commit :update_something_by_sidekiq
|
31
|
+
|
32
|
+
def execute
|
33
|
+
ActiveRecord::Base.transaction do
|
34
|
+
...
|
35
|
+
after_commit do
|
36
|
+
...
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
super
|
41
|
+
end
|
42
|
+
|
43
|
+
def update_something_by_sidekiq
|
44
|
+
...
|
45
|
+
end
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Another example with subcommand, you are sure that emails will be sent only if the transaction is completed
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class BaseCommand
|
53
|
+
include CommandExtension::Base
|
54
|
+
include CommandExtension::AfterCommit
|
55
|
+
end
|
56
|
+
|
57
|
+
class CreateOrderCommand < BaseCommand
|
58
|
+
def execute
|
59
|
+
ActiveRecord::Base.transaction do
|
60
|
+
order = Order.create()
|
61
|
+
|
62
|
+
after_commit do
|
63
|
+
SendEmailToBuyer.call_async(order.user_id)
|
64
|
+
end
|
65
|
+
|
66
|
+
subcommand(UpdateUserProfit.new(order)).execute
|
67
|
+
|
68
|
+
# if an error occurs then nothing will be sent
|
69
|
+
# raise
|
70
|
+
end
|
71
|
+
|
72
|
+
super
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class UpdateUserProfitCommand < BaseCommand
|
77
|
+
|
78
|
+
attr_reader :order
|
79
|
+
|
80
|
+
def initialize(order)
|
81
|
+
@order = order
|
82
|
+
|
83
|
+
super()
|
84
|
+
end
|
85
|
+
|
86
|
+
def execute
|
87
|
+
ActiveRecord::Base.transaction do
|
88
|
+
profit = Profit.find_by(user_id: order.seller_id)
|
89
|
+
profit.update_something
|
90
|
+
|
91
|
+
after_commit do
|
92
|
+
SendEmailToSeller.call_async(order.seller_id)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
super
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
## Development
|
102
|
+
|
103
|
+
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.
|
104
|
+
|
105
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
106
|
+
|
107
|
+
## Contributing
|
108
|
+
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/meyvn/command_extension. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'command_extension'
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
# require "pry"
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
require 'irb'
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'command_extension/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'command_extension'
|
9
|
+
spec.version = CommandExtension::VERSION
|
10
|
+
spec.authors = ['Alexander Fedulin', 'Alexander Strelnikov']
|
11
|
+
spec.email = ['aristat37@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Command pattern with after commit logic to avoid AR model callbacks'
|
14
|
+
spec.description = 'Command pattern with after commit logic to avoid AR model callbacks'
|
15
|
+
spec.homepage = 'https://github.com/meyvn/command_extension'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
30
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.10'
|
32
|
+
spec.add_development_dependency 'singleton', '~> 0.1'
|
33
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CommandExtension
|
4
|
+
module AfterCommit
|
5
|
+
def self.included(base)
|
6
|
+
base.send :include, InstanceMethods
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
attr_writer :after_commit_queue_enabled
|
12
|
+
|
13
|
+
def after_commit_queue
|
14
|
+
@after_commit_queue ||= []
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset
|
18
|
+
@after_commit_queue = nil
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
22
|
+
def after_commit(method = nil, &block)
|
23
|
+
after_commit_queue << proc { send(method) } if method
|
24
|
+
after_commit_queue << block if block
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def after_commit_queue_enabled
|
29
|
+
@after_commit_queue_enabled = true if @after_commit_queue_enabled.nil?
|
30
|
+
@after_commit_queue_enabled
|
31
|
+
end
|
32
|
+
|
33
|
+
def after_commit_queue_enabled?
|
34
|
+
after_commit_queue_enabled
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute
|
38
|
+
run_after_commit_queue if after_commit_queue_enabled?
|
39
|
+
super
|
40
|
+
end
|
41
|
+
|
42
|
+
def subcommand(command)
|
43
|
+
command.after_commit_queue_enabled = false
|
44
|
+
after_commit do
|
45
|
+
command.run_after_commit_queue
|
46
|
+
end
|
47
|
+
command
|
48
|
+
end
|
49
|
+
|
50
|
+
def run_after_commit_queue
|
51
|
+
self.class.run_after_commit_queue(self)
|
52
|
+
after_commit_queue.each(&:call)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
module ClassMethods
|
57
|
+
def after_commit_queue
|
58
|
+
@after_commit_queue ||= []
|
59
|
+
end
|
60
|
+
|
61
|
+
def after_commit(method = nil, &block)
|
62
|
+
after_commit_queue << method if method
|
63
|
+
after_commit_queue << block if block
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
67
|
+
def run_after_commit_queue(instance)
|
68
|
+
after_commit_queue.each do |m|
|
69
|
+
case m
|
70
|
+
when Symbol, String
|
71
|
+
instance.__send__(m)
|
72
|
+
else
|
73
|
+
instance.instance_eval(m)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: command_extension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Fedulin
|
8
|
+
- Alexander Strelnikov
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-05-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '2.0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '2.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '13.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '13.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.10'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.10'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: singleton
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0.1'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.1'
|
70
|
+
description: Command pattern with after commit logic to avoid AR model callbacks
|
71
|
+
email:
|
72
|
+
- aristat37@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- command_extension.gemspec
|
87
|
+
- lib/command_extension.rb
|
88
|
+
- lib/command_extension/after_commit.rb
|
89
|
+
- lib/command_extension/base.rb
|
90
|
+
- lib/command_extension/version.rb
|
91
|
+
homepage: https://github.com/meyvn/command_extension
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubygems_version: 3.0.4
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Command pattern with after commit logic to avoid AR model callbacks
|
114
|
+
test_files: []
|