junk_drawer 1.5.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.travis.yml +6 -2
- data/README.md +34 -3
- data/junk_drawer.gemspec +1 -1
- data/lib/junk_drawer/callable.rb +3 -1
- data/lib/junk_drawer/notifier.rb +7 -8
- data/lib/junk_drawer/rails/bulk_updatable.rb +24 -8
- data/lib/junk_drawer/version.rb +1 -1
- metadata +7 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 41bdf84aed0cada468a2754eaeb98650346f35789f2ffaae69b2b79026abe720
|
4
|
+
data.tar.gz: 7e8394ce972f2eeb72d2c28ba5966a67da30f1561575595d329d8533781a3c46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55ae0cc6d20c582a88be1cd28d92bb2942ffc44d671aabff3cc4d6c6dbd3c1ee61d5232f2e86d6cfa44422d98c8daf86563c097db6ef078261d584a80c389c9a
|
7
|
+
data.tar.gz: 07d25dce3efde1f44f6d41382453d2e3b1d9f7c2cf7d5658006acd6bf21ecfe4c62237e64a3a324094d1bf17ea6ca7a023395ae09da531a07830fa7538941308
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.3
|
4
|
+
- 2.3
|
5
|
+
- 2.4
|
6
|
+
- 2.5
|
7
|
+
- 2.6
|
8
|
+
- 2.7
|
5
9
|
addons:
|
6
10
|
postgresql: '9.4'
|
7
|
-
before_install: gem install bundler -v
|
11
|
+
before_install: gem install bundler -v 2.0.2
|
8
12
|
before_script:
|
9
13
|
- createdb junk_drawer_test -U postgres
|
10
14
|
script:
|
data/README.md
CHANGED
@@ -27,9 +27,18 @@ gem 'junk_drawer', require: 'junk_drawer/rails'
|
|
27
27
|
|
28
28
|
### Contents
|
29
29
|
|
30
|
-
- [JunkDrawer
|
31
|
-
- [
|
32
|
-
- [
|
30
|
+
- [JunkDrawer](#junkdrawer)
|
31
|
+
- [Installation](#installation)
|
32
|
+
- [Contents](#contents)
|
33
|
+
- [Usage](#usage)
|
34
|
+
- [JunkDrawer::Callable](#junkdrawercallable)
|
35
|
+
- [JunkDrawer::Notifier](#junkdrawernotifier)
|
36
|
+
- [Rails](#rails)
|
37
|
+
- [JunkDrawer::BulkUpdatable](#junkdrawerbulkupdatable)
|
38
|
+
- [Caveats](#caveats)
|
39
|
+
- [Development](#development)
|
40
|
+
- [Contributing](#contributing)
|
41
|
+
- [License](#license)
|
33
42
|
|
34
43
|
## Usage
|
35
44
|
|
@@ -129,6 +138,28 @@ your selected strategy. The strategies available are as follows:
|
|
129
138
|
3) `:null` is a noop. If you want to disable notifications temporarily, you can
|
130
139
|
configure the strategy to `:null`.
|
131
140
|
|
141
|
+
4) To create your own custom notifier, configure `JunkDrawer::Notifier` with
|
142
|
+
a callable object as the strategy.
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
class MyNotifier
|
146
|
+
include JunkDrawer::Callable
|
147
|
+
|
148
|
+
def call(*args)
|
149
|
+
SomeMonitoringService.notify(*args)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
JunkDrawer::Notifier.strategy = MyNotifier
|
154
|
+
```
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
JunkDrawer::Notifier.strategy = ->(*args) {
|
158
|
+
MonitoringServiceA.notify(*args)
|
159
|
+
MonitoringServiceB.notify(*args)
|
160
|
+
}
|
161
|
+
```
|
162
|
+
|
132
163
|
If you're using Rails, you may want to configure `Notifier` based on the
|
133
164
|
environment, so in your `config/environments/development.rb` you might have:
|
134
165
|
|
data/junk_drawer.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.required_ruby_version = '>= 2.1'
|
23
23
|
|
24
|
-
spec.
|
24
|
+
spec.add_runtime_dependency 'ruby2_keywords', '~> 0.0.2'
|
25
25
|
spec.add_development_dependency 'guard', '~> 2.14'
|
26
26
|
spec.add_development_dependency 'guard-rspec', '~> 4.7'
|
27
27
|
spec.add_development_dependency 'guard-rubocop', '~> 1.2'
|
data/lib/junk_drawer/callable.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'ruby2_keywords'
|
4
|
+
|
3
5
|
module JunkDrawer
|
4
6
|
# error to be thrown by Callable
|
5
7
|
class CallableError < StandardError
|
@@ -19,7 +21,7 @@ module JunkDrawer
|
|
19
21
|
# an instance. It also causes an error to be raised if a public instance
|
20
22
|
# method is defined with a name other than `call`
|
21
23
|
module ClassMethods
|
22
|
-
def call(*args, &block)
|
24
|
+
ruby2_keywords def call(*args, &block)
|
23
25
|
new.(*args, &block)
|
24
26
|
end
|
25
27
|
|
data/lib/junk_drawer/notifier.rb
CHANGED
@@ -12,7 +12,12 @@ module JunkDrawer
|
|
12
12
|
|
13
13
|
class << self
|
14
14
|
|
15
|
-
|
15
|
+
attr_reader :strategy
|
16
|
+
|
17
|
+
def strategy=(strategy)
|
18
|
+
@strategy =
|
19
|
+
strategy.is_a?(Symbol) ? STRATEGIES.fetch(strategy) : strategy
|
20
|
+
end
|
16
21
|
|
17
22
|
end
|
18
23
|
|
@@ -23,13 +28,7 @@ module JunkDrawer
|
|
23
28
|
}.freeze
|
24
29
|
|
25
30
|
def call(*args)
|
26
|
-
strategy.(*args)
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def strategy
|
32
|
-
STRATEGIES.fetch(self.class.strategy)
|
31
|
+
self.class.strategy.(*args)
|
33
32
|
end
|
34
33
|
|
35
34
|
end
|
@@ -11,13 +11,30 @@ module JunkDrawer
|
|
11
11
|
objects = objects.select(&:changed?)
|
12
12
|
return unless objects.any?
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
unique_objects = uniquify_and_merge(objects)
|
15
|
+
changed_attributes = extract_changed_attributes(unique_objects)
|
16
|
+
query = build_query_for(unique_objects, changed_attributes)
|
16
17
|
connection.execute(query)
|
18
|
+
objects.each(&:clear_changes_information)
|
17
19
|
end
|
18
20
|
|
19
21
|
private
|
20
22
|
|
23
|
+
def uniquify_and_merge(objects)
|
24
|
+
grouped_objects = objects.group_by(&:id).values
|
25
|
+
grouped_objects.each do |group|
|
26
|
+
next if group.length == 1
|
27
|
+
|
28
|
+
attrs = group.each_with_object({}) do |object, changes|
|
29
|
+
object.changed.each do |changed_attribute|
|
30
|
+
changes[changed_attribute] = object[changed_attribute]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
group.each { |object| object.attributes = attrs }
|
34
|
+
end
|
35
|
+
grouped_objects.map(&:first)
|
36
|
+
end
|
37
|
+
|
21
38
|
def extract_changed_attributes(objects)
|
22
39
|
now = Time.zone.now
|
23
40
|
objects.each { |object| object.updated_at = now }
|
@@ -41,12 +58,11 @@ module JunkDrawer
|
|
41
58
|
"#{quoted_column_name} = tmp_table.#{quoted_column_name}"
|
42
59
|
end.join(', ')
|
43
60
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
SQL
|
61
|
+
"UPDATE #{table_name} " \
|
62
|
+
"SET #{assignment_query} " \
|
63
|
+
"FROM (VALUES #{object_values}) " \
|
64
|
+
"AS tmp_table(id, #{attributes.join(', ')}) " \
|
65
|
+
"WHERE #{table_name}.id = tmp_table.id"
|
50
66
|
end
|
51
67
|
|
52
68
|
def sanitized_values(object, attributes)
|
data/lib/junk_drawer/version.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: junk_drawer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Fletcher
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: ruby2_keywords
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
type: :
|
19
|
+
version: 0.0.2
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.0.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: guard
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -243,8 +243,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
243
243
|
- !ruby/object:Gem::Version
|
244
244
|
version: '0'
|
245
245
|
requirements: []
|
246
|
-
|
247
|
-
rubygems_version: 2.6.14
|
246
|
+
rubygems_version: 3.1.4
|
248
247
|
signing_key:
|
249
248
|
specification_version: 4
|
250
249
|
summary: random useful Ruby utilities
|