tantot 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tantot/changes.rb +65 -0
- data/lib/tantot/collector/base.rb +6 -0
- data/lib/tantot/collector/block.rb +78 -0
- data/lib/tantot/collector/watcher.rb +87 -0
- data/lib/tantot/collector.rb +76 -0
- data/lib/tantot/config.rb +17 -0
- data/lib/tantot/errors.rb +4 -0
- data/lib/tantot/extensions/chewy.rb +88 -0
- data/lib/tantot/extensions/grape/middleware.rb +17 -0
- data/lib/tantot/formatter/compact.rb +9 -0
- data/lib/tantot/formatter/detailed.rb +9 -0
- data/lib/tantot/formatter.rb +10 -0
- data/lib/tantot/observe.rb +84 -0
- data/lib/tantot/performer/bypass.rb +9 -0
- data/lib/tantot/performer/inline.rb +9 -0
- data/lib/tantot/performer/sidekiq.rb +19 -0
- data/lib/tantot/performer.rb +17 -0
- data/lib/tantot/railtie.rb +23 -0
- data/lib/tantot/registry.rb +11 -0
- data/lib/tantot/version.rb +3 -0
- data/lib/tantot/watcher.rb +16 -0
- data/lib/tantot.rb +62 -0
- data/spec/changes_spec.rb +60 -0
- data/spec/extensions/chewy_spec.rb +127 -0
- data/spec/sidekiq_spec.rb +100 -0
- data/spec/spec_helper.rb +47 -0
- data/spec/tantot_spec.rb +299 -0
- data/tantot.gemspec +31 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 856c9b2b2373579c05f52dd5ab10b2728bf516ec
|
4
|
+
data.tar.gz: 527e2a10f265490b948a73a108a6ac42b6d02d09
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 882fa4b710f08cad5c074fc73f3a9eef94ee3d84fe499347a607e7683826587488aa82b3660f9cf1e877e73c4908dc3158debb5cb1889f69c80ed680677755a2
|
7
|
+
data.tar.gz: c2a4008e85d2b887c6c08889b09145883450e1e31cfd63129d68a94ac1a19738fb440935113ad2b91cba005e9ad74313acde0a89b3d8e32b873c0374562a8299
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at fpbouchard@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 François-Pierre Bouchard
|
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,45 @@
|
|
1
|
+
# Tantot
|
2
|
+
|
3
|
+
[](https://travis-ci.org/petalmd/tantot)
|
4
|
+
|
5
|
+
Tantot (french for _shortly_/_soon_)
|
6
|
+
|
7
|
+
Centralize and delay changes to multiple ActiveRecord models to offload processing of complex calculations caused by model mutations.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'tantot'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install tantot
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Tantot introduces new ActiveRecord callbacks that abstracts most of the usual boilerplate when managing changes, and also allows running callbacks in Sidekiq.
|
28
|
+
|
29
|
+
_More documentation to come!_
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
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.
|
34
|
+
|
35
|
+
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).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/petalmd/tantot. 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.
|
40
|
+
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
45
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tantot"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
module Tantot
|
2
|
+
module Changes
|
3
|
+
|
4
|
+
class ById
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :changes_by_id
|
8
|
+
|
9
|
+
def initialize(changes_by_id)
|
10
|
+
@changes_by_id = changes_by_id
|
11
|
+
end
|
12
|
+
|
13
|
+
delegate :[], :keys, :each, :count, :size, to: :changes_by_id
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
other.changes_by_id == @changes_by_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def for_attribute(attribute, compact = true)
|
20
|
+
changes = @changes_by_id.values.collect {|changes_by_attribute| changes_by_attribute[attribute.to_s]}.flatten.uniq
|
21
|
+
compact ? changes.compact : changes
|
22
|
+
end
|
23
|
+
|
24
|
+
def ids
|
25
|
+
@changes_by_id.keys
|
26
|
+
end
|
27
|
+
|
28
|
+
def attributes
|
29
|
+
@changes_by_id.values.collect(&:keys).flatten.uniq.collect(&:to_sym)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ByModel
|
34
|
+
include Enumerable
|
35
|
+
|
36
|
+
attr_reader :changes_by_model
|
37
|
+
|
38
|
+
def initialize(changes_by_model)
|
39
|
+
@changes_by_model = changes_by_model
|
40
|
+
end
|
41
|
+
|
42
|
+
delegate :==, :keys, :count, :size, to: :changes_by_model
|
43
|
+
alias_method :models, :keys
|
44
|
+
|
45
|
+
def ==(other)
|
46
|
+
other.changes_by_model == @changes_by_model
|
47
|
+
end
|
48
|
+
|
49
|
+
def [](model)
|
50
|
+
for_model(model)
|
51
|
+
end
|
52
|
+
|
53
|
+
def each(&block)
|
54
|
+
@changes_by_model.each do |model, changes|
|
55
|
+
block.call(model, Tantot::Changes::ById.new(changes))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def for_model(model)
|
60
|
+
Tantot::Changes::ById.new(@changes_by_model[model])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Tantot
|
2
|
+
module Collector
|
3
|
+
class Block < Base
|
4
|
+
def self.manages?(context)
|
5
|
+
context.key?(:block_id)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@stash = Hash.new do |block_id_hash, block_id|
|
10
|
+
block_id_hash[block_id] = Hash.new do |id_hash, id|
|
11
|
+
id_hash[id] = {}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def register_watch(context, block)
|
17
|
+
Tantot.registry.watch_config[context[:block_id]] = {context: context, block: block}
|
18
|
+
end
|
19
|
+
|
20
|
+
def push(context, instance, mutations)
|
21
|
+
options = context.fetch(:options, {})
|
22
|
+
formatter = Tantot::Formatter.resolve(options[:format] || Tantot.config.default_watcher_options[:format]).new
|
23
|
+
attribute_hash = @stash[context[:block_id]][instance.id]
|
24
|
+
mutations.each do |attr, changes|
|
25
|
+
attribute_hash[attr] = formatter.push(attribute_hash[attr], context, changes)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def sweep(performer, context = {})
|
30
|
+
@stash.each do |block_id, changes|
|
31
|
+
performer.run({block_id: block_id}, changes)
|
32
|
+
end
|
33
|
+
@stash.clear
|
34
|
+
end
|
35
|
+
|
36
|
+
def perform(context, changes_by_id)
|
37
|
+
watch_config = Tantot.registry.watch_config[context[:block_id]]
|
38
|
+
watch_config[:context][:model].instance_exec(Tantot::Changes::ById.new(changes_by_id), &watch_config[:block])
|
39
|
+
end
|
40
|
+
|
41
|
+
def marshal(context, changes_by_id)
|
42
|
+
[context, changes_by_id]
|
43
|
+
end
|
44
|
+
|
45
|
+
def unmarshal(context, changes_by_id)
|
46
|
+
changes_by_id = changes_by_id.each.with_object({}) do |(id, changes), change_hash|
|
47
|
+
change_hash[id.to_i] = changes
|
48
|
+
end
|
49
|
+
[context, changes_by_id]
|
50
|
+
end
|
51
|
+
|
52
|
+
def debug_block(block)
|
53
|
+
location, line = block.source_location
|
54
|
+
short_path = defined?(Rails) ? Pathname.new(location).relative_path_from(Rails.root).to_s : location
|
55
|
+
"block @ #{short_path}##{line}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def debug_context(context)
|
59
|
+
block = Tantot.registry.watch_config[context[:block_id]][:block]
|
60
|
+
debug_block(block)
|
61
|
+
end
|
62
|
+
|
63
|
+
def debug_state(context)
|
64
|
+
return false if @stash.empty?
|
65
|
+
@stash.collect do |block_id, changes_by_id|
|
66
|
+
watch_config = Tantot.registry.watch_config[block_id]
|
67
|
+
"#{watch_config[:context][:model].name}*#{changes_by_id.size} for #{debug_block(watch_config[:block])}"
|
68
|
+
end.join(" / ")
|
69
|
+
end
|
70
|
+
|
71
|
+
def debug_perform(context, changes_by_id)
|
72
|
+
watch_config = Tantot.registry.watch_config[context[:block_id]]
|
73
|
+
"#{watch_config[:context][:model].name}*#{changes_by_id.size} for #{debug_block(watch_config[:block])}"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Tantot
|
2
|
+
module Collector
|
3
|
+
class Watcher < Base
|
4
|
+
def self.manages?(context)
|
5
|
+
context.key?(:watcher)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@stash = Hash.new do |watcher_hash, watcher|
|
10
|
+
watcher_hash[watcher] = Hash.new do |model_hash, model|
|
11
|
+
model_hash[model] = Hash.new do |id_hash, id|
|
12
|
+
id_hash[id] = {}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def register_watch(context, block)
|
19
|
+
# nop
|
20
|
+
end
|
21
|
+
|
22
|
+
def push(context, instance, mutations)
|
23
|
+
watcher = context[:watcher]
|
24
|
+
model = context[:model]
|
25
|
+
formatter = Tantot::Formatter.resolve(watcher.watcher_options[:format]).new
|
26
|
+
attribute_hash = @stash[watcher][model][instance.id]
|
27
|
+
mutations.each do |attr, changes|
|
28
|
+
attribute_hash[attr] = formatter.push(attribute_hash[attr], context, changes)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def sweep(performer, context = {})
|
33
|
+
watcher = context[:watcher]
|
34
|
+
filtered_stash = watcher ? @stash.select {|w, _c| w == watcher} : @stash
|
35
|
+
filtered_stash.each do |w, changes_by_model|
|
36
|
+
performer.run({watcher: w}, changes_by_model)
|
37
|
+
end
|
38
|
+
if watcher
|
39
|
+
@stash.delete(watcher)
|
40
|
+
else
|
41
|
+
@stash.clear
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def perform(context, changes_by_model)
|
46
|
+
context[:watcher].new.perform(Tantot::Changes::ByModel.new(changes_by_model))
|
47
|
+
end
|
48
|
+
|
49
|
+
def marshal(context, changes_by_model)
|
50
|
+
changes_by_model = changes_by_model.each.with_object({}) do |(model_class, changes), hash|
|
51
|
+
hash[model_class.name] = changes
|
52
|
+
end
|
53
|
+
[context, changes_by_model]
|
54
|
+
end
|
55
|
+
|
56
|
+
def unmarshal(context, changes_by_model)
|
57
|
+
context[:watcher] = context[:watcher].constantize
|
58
|
+
changes_by_model = changes_by_model.each.with_object({}) do |(model_class_name, changes_by_id), model_hash|
|
59
|
+
model_hash[model_class_name.constantize] = changes_by_id.each.with_object({}) do |(id, changes), change_hash|
|
60
|
+
change_hash[id.to_i] = changes
|
61
|
+
end
|
62
|
+
end
|
63
|
+
[context, changes_by_model]
|
64
|
+
end
|
65
|
+
|
66
|
+
def debug_context(context)
|
67
|
+
context[:watcher].name.demodulize
|
68
|
+
end
|
69
|
+
|
70
|
+
def debug_state(context)
|
71
|
+
return false if @stash.empty?
|
72
|
+
@stash.collect do |watcher, changes_by_model|
|
73
|
+
"#{watcher.name}[#{changes_by_model.collect do |model, changes_by_id|
|
74
|
+
"#{model.name}*#{changes_by_id.size}"
|
75
|
+
end.join("&")}]"
|
76
|
+
end.flatten.join(" / ")
|
77
|
+
end
|
78
|
+
|
79
|
+
def debug_perform(context, changes_by_model)
|
80
|
+
"#{context[:watcher].name}[#{changes_by_model.collect do |model, changes_by_id|
|
81
|
+
"#{model.name}*#{changes_by_id.size}"
|
82
|
+
end.join("&")}]"
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'tantot/collector/base'
|
2
|
+
require 'tantot/collector/watcher'
|
3
|
+
require 'tantot/collector/block'
|
4
|
+
|
5
|
+
module Tantot
|
6
|
+
module Collector
|
7
|
+
class Manager
|
8
|
+
def initialize
|
9
|
+
@collectors = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def register_watch(context, block)
|
13
|
+
resolve!(context).register_watch(context, block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def run(&block)
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
sweep
|
20
|
+
end
|
21
|
+
|
22
|
+
def push(context, instance, mutations)
|
23
|
+
collector = resolve!(context)
|
24
|
+
Tantot.logger.debug do
|
25
|
+
mutate = mutations.size.zero? ? 'destroy' : "#{mutations.size} mutations(s)"
|
26
|
+
"[Tantot] [Collecting] [#{collector.class.name.demodulize}] #{mutate} on <#{instance.class.name}:#{instance.id}> for <#{collector.debug_context(context)}>"
|
27
|
+
end
|
28
|
+
collector.push(context, instance, mutations)
|
29
|
+
sweep(context.merge(performer: :inline)) if Tantot.config.console_mode
|
30
|
+
end
|
31
|
+
|
32
|
+
def sweep(context = {})
|
33
|
+
performer = Tantot::Performer.resolve(context[:performer] || Tantot.config.performer).new
|
34
|
+
specific_collector = resolve(context)
|
35
|
+
collectors = specific_collector ? [specific_collector] : @collectors.values
|
36
|
+
collectors.each do |collector|
|
37
|
+
if Tantot.logger.debug? && (debug_state = collector.debug_state(context))
|
38
|
+
Tantot.logger.debug { "[Tantot] [Sweeping] [#{collector.class.name.demodulize}] [#{performer.class.name.demodulize}] #{debug_state}" }
|
39
|
+
end
|
40
|
+
collector.sweep(performer, context)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def perform(context, changes)
|
45
|
+
collector = resolve!(context)
|
46
|
+
Tantot.logger.debug { "[Tantot] [Performing] [#{collector.class.name.demodulize}] #{collector.debug_perform(context, changes)}" }
|
47
|
+
collector.perform(context, changes)
|
48
|
+
end
|
49
|
+
|
50
|
+
def marshal(context, changes)
|
51
|
+
collector = resolve!(context)
|
52
|
+
context, changes = collector.marshal(context, changes)
|
53
|
+
context[:collector_class] = collector.class
|
54
|
+
[context, changes]
|
55
|
+
end
|
56
|
+
|
57
|
+
def unmarshal(context, changes)
|
58
|
+
context.deep_symbolize_keys!
|
59
|
+
collector_class = context[:collector_class].constantize
|
60
|
+
collector = @collectors[collector_class] || @collectors[collector_class] = collector_class.new
|
61
|
+
collector.unmarshal(context, changes)
|
62
|
+
end
|
63
|
+
|
64
|
+
def resolve(context)
|
65
|
+
collector_class = Tantot::Collector::Base.descendants.find {|c| c.manages?(context)}
|
66
|
+
return nil unless collector_class
|
67
|
+
@collectors[collector_class] || @collectors[collector_class] = collector_class.new
|
68
|
+
end
|
69
|
+
|
70
|
+
def resolve!(context)
|
71
|
+
resolve(context) || (raise "No collector manages current context: #{context.inspect}")
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Tantot
|
2
|
+
class Config
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
attr_accessor :performer, :use_after_commit_callbacks, :default_watcher_options, :console_mode, :chewy_strategy
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@performer = :inline
|
9
|
+
@use_after_commit_callbacks = true
|
10
|
+
@default_watcher_options = {
|
11
|
+
format: :compact
|
12
|
+
}
|
13
|
+
@console_mode = false
|
14
|
+
@chewy_strategy = :atomic
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Tantot
|
2
|
+
module Extensions
|
3
|
+
module Chewy
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
class_attribute :_tantot_chewy_callbacks
|
8
|
+
end
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
# watch_index 'index#type', attribute, attribute, {method: [:self | :method | ignore and pass a block | ignore and don't pass a block, equivalent of :self]} [block]
|
12
|
+
def watch_index(type_name, *args, &block)
|
13
|
+
options = args.extract_options!
|
14
|
+
watch('tantot/extensions/chewy/chewy', *args)
|
15
|
+
Tantot::Extensions::Chewy.register_watch(self, type_name, options, block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.register_watch(model, type_name, options, block)
|
20
|
+
method = options.delete(:method)
|
21
|
+
model._tantot_chewy_callbacks ||= {}
|
22
|
+
model._tantot_chewy_callbacks[type_name] ||= []
|
23
|
+
model._tantot_chewy_callbacks[type_name].push({method: method, options: options, block: block})
|
24
|
+
end
|
25
|
+
|
26
|
+
class ChewyWatcher
|
27
|
+
include Tantot::Watcher
|
28
|
+
|
29
|
+
def perform(changes_by_model)
|
30
|
+
changes_by_model.each do |model, changes_by_id|
|
31
|
+
model_watches = model._tantot_chewy_callbacks
|
32
|
+
model_watches.each do |type_name, watch_args_array|
|
33
|
+
# Find type
|
34
|
+
reference =
|
35
|
+
if type_name.is_a?(Proc)
|
36
|
+
if type_name.arity.zero?
|
37
|
+
instance_exec(&type_name)
|
38
|
+
else
|
39
|
+
type_name.call(self)
|
40
|
+
end
|
41
|
+
else
|
42
|
+
type_name
|
43
|
+
end
|
44
|
+
|
45
|
+
watch_args_array.each do |watch_args|
|
46
|
+
method = watch_args[:method]
|
47
|
+
options = watch_args[:options]
|
48
|
+
block = watch_args[:block]
|
49
|
+
|
50
|
+
# Find ids to update
|
51
|
+
backreference =
|
52
|
+
if (method && method.to_sym == :self) || (!method && !block)
|
53
|
+
# Simply extract keys from changes
|
54
|
+
changes_by_id.keys
|
55
|
+
elsif method
|
56
|
+
# We need to call `method`.
|
57
|
+
# Try to find it on the class. If so, call it once with all changes.
|
58
|
+
# There is no API to call per-instance since objects can be already destroyed
|
59
|
+
# when using the sidekiq performer
|
60
|
+
model.send(method, changes_by_id)
|
61
|
+
elsif block
|
62
|
+
# Since we can be post-destruction of the model, we can't load models here
|
63
|
+
# Thus, the signature of the block callback is |changes| which are all
|
64
|
+
# the changes to all the models
|
65
|
+
model.instance_exec(changes_by_id, &block)
|
66
|
+
end
|
67
|
+
|
68
|
+
if backreference
|
69
|
+
backreference.compact!
|
70
|
+
|
71
|
+
# Make sure there are any backreferences
|
72
|
+
if backreference.any?
|
73
|
+
# Call update_index, will follow normal chewy strategy
|
74
|
+
::Chewy.strategy Tantot.config.chewy_strategy do
|
75
|
+
Tantot.logger.debug "[Tantot] [Chewy] [update_index] #{reference} (#{backreference.count} objects): #{backreference.inspect}"
|
76
|
+
::Chewy.derive_type(reference).update_index(backreference, options)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|