ii_finder 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +15 -0
- data/lib/ii_finder.rb +1 -0
- data/lib/ii_finder/base.rb +5 -52
- data/lib/ii_finder/callbacks.rb +6 -0
- data/lib/ii_finder/core.rb +58 -0
- data/lib/ii_finder/instrumentation.rb +13 -0
- data/lib/ii_finder/log_subscriber.rb +18 -0
- data/lib/ii_finder/lookup.rb +5 -5
- data/lib/ii_finder/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e3dc926cc0da901131f8a10b84ae7a31815606384e5b37ab3caad85ad87cee3
|
4
|
+
data.tar.gz: 9217854b0fe479c182404a1137e4c95f1777d8a674fbb3f3b81bc744a1476652
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45ee75341deca673d350dadc0bc1c0be3e24b9a473261f26f5861310fced6d3b2b9237aaf484dac82d08339bbeb1e6a7e5bde7ddcca77d2cd1599ec03b849083
|
7
|
+
data.tar.gz: ad252f7de5a14f901aa2278d7a9b69732a198778608c3bdfb816f73e46ec435d18721cac57901890d722e3780bc03bc19735fe4df703612392bf82352053a661
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -187,6 +187,21 @@ User.finder_scope(name: 'NAME').to_sql
|
|
187
187
|
#=> SELECT "users".* FROM "users" WHERE "users"."name" = 'NAME'
|
188
188
|
```
|
189
189
|
|
190
|
+
### Logging
|
191
|
+
|
192
|
+
Finder supports instrumentation hook supplied by `ActiveSupport::Notifications`.
|
193
|
+
You can enable log subscriber as follows:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
IIFinder::LogSubscriber.attach_to :ii_finder
|
197
|
+
```
|
198
|
+
|
199
|
+
This subscriber will write logs in debug mode as the following example:
|
200
|
+
|
201
|
+
```
|
202
|
+
Called UsersFinder with {:id=>1} (Duration: 9.9ms, Allocations: 915)
|
203
|
+
```
|
204
|
+
|
190
205
|
## Contributing
|
191
206
|
|
192
207
|
Bug reports and pull requests are welcome at https://github.com/kanety/ii_finder.
|
data/lib/ii_finder.rb
CHANGED
data/lib/ii_finder/base.rb
CHANGED
@@ -1,64 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative 'core'
|
4
4
|
require_relative 'parameters'
|
5
5
|
require_relative 'callbacks'
|
6
|
+
require_relative 'instrumentation'
|
7
|
+
require_relative 'lookup'
|
6
8
|
|
7
9
|
module IIFinder
|
8
10
|
class Base
|
11
|
+
include Core
|
9
12
|
include Parameters
|
10
13
|
include Callbacks
|
14
|
+
include Instrumentation
|
11
15
|
include Lookup
|
12
|
-
|
13
|
-
attr_reader :relation, :criteria, :model, :table
|
14
|
-
|
15
|
-
def initialize(*args)
|
16
|
-
if args.size == 0 || args.size == 1
|
17
|
-
@model = self.class.lookup
|
18
|
-
raise IIFinder::Error.new("could not find model for #{self.class}") unless @model
|
19
|
-
@relation = @model.all
|
20
|
-
@criteria = args[0] || {}
|
21
|
-
else
|
22
|
-
@relation = args[0]
|
23
|
-
@criteria = args[1]
|
24
|
-
@model = @relation.klass
|
25
|
-
end
|
26
|
-
@table = @model.arel_table
|
27
|
-
end
|
28
|
-
|
29
|
-
def call
|
30
|
-
run_callbacks :call do
|
31
|
-
self.class._parameters.each do |param|
|
32
|
-
value = fetch_criteria(param.name)
|
33
|
-
if value.present? || param.allow_blank?
|
34
|
-
call_method(param.name, value)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
@relation
|
40
|
-
end
|
41
|
-
|
42
|
-
def fetch_criteria(name)
|
43
|
-
if @criteria.respond_to?(name)
|
44
|
-
@criteria.send(name)
|
45
|
-
elsif @criteria.respond_to?(:fetch)
|
46
|
-
@criteria.fetch(name, nil)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def call_method(name, value)
|
51
|
-
result = send(name, value)
|
52
|
-
|
53
|
-
if Config.merge_relation && result.is_a?(ActiveRecord::Relation)
|
54
|
-
@relation = @relation.merge(result)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class << self
|
59
|
-
def call(*args)
|
60
|
-
new(*args).call
|
61
|
-
end
|
62
|
-
end
|
63
16
|
end
|
64
17
|
end
|
data/lib/ii_finder/callbacks.rb
CHANGED
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IIFinder
|
4
|
+
module Core
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
attr_reader :relation, :criteria, :model, :table
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(*args)
|
12
|
+
if args.size == 0 || args.size == 1
|
13
|
+
@model = self.class.lookup
|
14
|
+
raise IIFinder::Error.new("could not find model for #{self.class}") unless @model
|
15
|
+
@relation = @model.all
|
16
|
+
@criteria = args[0] || {}
|
17
|
+
else
|
18
|
+
@relation = args[0]
|
19
|
+
@criteria = args[1]
|
20
|
+
@model = @relation.klass
|
21
|
+
end
|
22
|
+
@table = @model.arel_table
|
23
|
+
end
|
24
|
+
|
25
|
+
def call
|
26
|
+
self.class._parameters.each do |param|
|
27
|
+
value = fetch_criteria(param.name)
|
28
|
+
if value.present? || param.allow_blank?
|
29
|
+
call_method(param.name, value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
@relation
|
34
|
+
end
|
35
|
+
|
36
|
+
def fetch_criteria(name)
|
37
|
+
if @criteria.respond_to?(name)
|
38
|
+
@criteria.send(name)
|
39
|
+
elsif @criteria.respond_to?(:fetch)
|
40
|
+
@criteria.fetch(name, nil)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def call_method(name, value)
|
45
|
+
result = send(name, value)
|
46
|
+
|
47
|
+
if Config.merge_relation && result.is_a?(ActiveRecord::Relation)
|
48
|
+
@relation = @relation.merge(result)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class_methods do
|
53
|
+
def call(*args)
|
54
|
+
new(*args).call
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module IIFinder
|
4
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
def call(event)
|
6
|
+
debug do
|
7
|
+
finder = event.payload[:finder]
|
8
|
+
"Called #{finder.class} with #{finder.criteria} (#{additional_log(event)})"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def additional_log(event)
|
13
|
+
additions = ["Duration: %.1fms" % event.duration]
|
14
|
+
additions << "Allocations: %d" % event.allocations if event.respond_to?(:allocations)
|
15
|
+
additions.join(', ')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/ii_finder/lookup.rb
CHANGED
@@ -19,13 +19,13 @@ module IIFinder
|
|
19
19
|
end
|
20
20
|
|
21
21
|
class << self
|
22
|
-
class_attribute :
|
23
|
-
self.
|
22
|
+
class_attribute :cache
|
23
|
+
self.cache = {}
|
24
24
|
|
25
25
|
def call(klass)
|
26
26
|
return if terminate?(klass)
|
27
27
|
|
28
|
-
|
28
|
+
with_cache(klass) do
|
29
29
|
if klass.name && (resolved = resolve(klass))
|
30
30
|
resolved
|
31
31
|
elsif klass.superclass
|
@@ -36,9 +36,9 @@ module IIFinder
|
|
36
36
|
|
37
37
|
private
|
38
38
|
|
39
|
-
def
|
39
|
+
def with_cache(klass)
|
40
40
|
if Config.lookup_cache
|
41
|
-
self.
|
41
|
+
self.cache[klass] ||= yield
|
42
42
|
else
|
43
43
|
yield
|
44
44
|
end
|
data/lib/ii_finder/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ii_finder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -121,7 +121,10 @@ files:
|
|
121
121
|
- lib/ii_finder/base.rb
|
122
122
|
- lib/ii_finder/callbacks.rb
|
123
123
|
- lib/ii_finder/config.rb
|
124
|
+
- lib/ii_finder/core.rb
|
124
125
|
- lib/ii_finder/errors.rb
|
126
|
+
- lib/ii_finder/instrumentation.rb
|
127
|
+
- lib/ii_finder/log_subscriber.rb
|
125
128
|
- lib/ii_finder/lookup.rb
|
126
129
|
- lib/ii_finder/parameter.rb
|
127
130
|
- lib/ii_finder/parameters.rb
|