ii_finder 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e3ba37c83b4bc3c8015d95bf52544ea29e90928948c0f5926946e8cb0c49bb3
4
- data.tar.gz: c785e88d9b5c78e1a1409cedd805a2944d526de46d6b4acb22272c8c97d732a8
3
+ metadata.gz: 5e3dc926cc0da901131f8a10b84ae7a31815606384e5b37ab3caad85ad87cee3
4
+ data.tar.gz: 9217854b0fe479c182404a1137e4c95f1777d8a674fbb3f3b81bc744a1476652
5
5
  SHA512:
6
- metadata.gz: 67e33beb2d96672958062a58301da0ff1cc9bdb8855572c2e41fa41c685e08a7901d52e6c99df97fec2d986b8043cd59726bd1fbaf3d0c5790854d6720fc8c6d
7
- data.tar.gz: 6b70c8f8620928ca1997e79f0178aaba61cd04abfedc5d2ff8e24b7296e9c4e0b7cfaa1be9c4bd05a42ee647e7a5d3c0a532a55e09e285fb362876a914c93470
6
+ metadata.gz: 45ee75341deca673d350dadc0bc1c0be3e24b9a473261f26f5861310fced6d3b2b9237aaf484dac82d08339bbeb1e6a7e5bde7ddcca77d2cd1599ec03b849083
7
+ data.tar.gz: ad252f7de5a14f901aa2278d7a9b69732a198778608c3bdfb816f73e46ec435d18721cac57901890d722e3780bc03bc19735fe4df703612392bf82352053a661
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.1.0
4
+
5
+ * Add log subscriber.
6
+
3
7
  ## 1.0.0
4
8
 
5
9
  * First release.
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
@@ -5,6 +5,7 @@ require 'ii_finder/config'
5
5
  require 'ii_finder/errors'
6
6
  require 'ii_finder/base'
7
7
  require 'ii_finder/scope'
8
+ require 'ii_finder/log_subscriber'
8
9
 
9
10
  module IIFinder
10
11
  class << self
@@ -1,64 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lookup'
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
@@ -9,6 +9,12 @@ module IIFinder
9
9
  define_callbacks :call
10
10
  end
11
11
 
12
+ def call
13
+ run_callbacks :call do
14
+ super
15
+ end
16
+ end
17
+
12
18
  class_methods do
13
19
  def before_call(*args, &block)
14
20
  set_callback(:call, :before, *args, &block)
@@ -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,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIFinder
4
+ module Instrumentation
5
+ extend ActiveSupport::Concern
6
+
7
+ def call
8
+ ActiveSupport::Notifications.instrument 'call.ii_finder', finder: self do
9
+ super
10
+ end
11
+ end
12
+ end
13
+ 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
@@ -19,13 +19,13 @@ module IIFinder
19
19
  end
20
20
 
21
21
  class << self
22
- class_attribute :_cache
23
- self._cache = {}
22
+ class_attribute :cache
23
+ self.cache = {}
24
24
 
25
25
  def call(klass)
26
26
  return if terminate?(klass)
27
27
 
28
- cache(klass) do
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 cache(klass)
39
+ def with_cache(klass)
40
40
  if Config.lookup_cache
41
- self._cache[klass] ||= yield
41
+ self.cache[klass] ||= yield
42
42
  else
43
43
  yield
44
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIFinder
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
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.0.0
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-05 00:00:00.000000000 Z
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