ii_finder 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0ae2fd0943ca59c70d7f10c8f0c7c48d9cac20fddd65af6c0f255a9352c78a2
4
- data.tar.gz: 433dddd946b33c7677d3a65ce123c7cc78a5bd1f4defb67b8b2a8b5dc773e6e1
3
+ metadata.gz: 29ddace819669cddc6f56eb2f7e2f61975e6a71e5e2478eeddde806a6d9aba30
4
+ data.tar.gz: 744b5c28c6064a4e3459ef4296f5256c02d5f0ceaaaffb1ddc50645609a76ccb
5
5
  SHA512:
6
- metadata.gz: 549fe65cf139efb23f0ea3cf127091e835f4cdfe1f7e48d6700d4ceae0b436d0375ad60b906075d8e79f8a56439a143c294af012706e631d292e442d2572bc35
7
- data.tar.gz: '009952b67ce09720ed1d250fb1337c9fb053661c7e45427c30e144dcab0d875673ad3a2eed933560b7dfebc70929a162ad075b35e751c3d55138dfd146498877'
6
+ metadata.gz: 0b708694fb6d3084d7b56dae4da34367a6f4e6ff0cb69c59061de3e652d28f8b1941bc4c321298609e633b7f59a054225d7852a56148ebe5797e1955ad1a6925
7
+ data.tar.gz: db7addc2d731b77733fa05b6c3879f7b4bd69671ebb0b8e85cd7b9412ea4455b6a1e1388fc2607503015ffe5f019ddffd3a16812713a93d21cd6b8551e328cd5
@@ -4,27 +4,41 @@ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  test:
7
- runs-on: ubuntu-18.04
7
+ runs-on: ubuntu-20.04
8
8
  strategy:
9
9
  fail-fast: false
10
10
  matrix:
11
- ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
12
- gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
11
+ ruby: [2.3, 2.4, 2.5, 2.6, 2.7, '3.0', 3.1]
12
+ gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61', 'rails70']
13
13
  exclude:
14
14
  - ruby: 2.3
15
15
  gemfile: rails60
16
16
  - ruby: 2.3
17
17
  gemfile: rails61
18
+ - ruby: 2.3
19
+ gemfile: rails70
18
20
  - ruby: 2.4
19
21
  gemfile: rails60
20
22
  - ruby: 2.4
21
23
  gemfile: rails61
24
+ - ruby: 2.4
25
+ gemfile: rails70
26
+ - ruby: 2.5
27
+ gemfile: rails70
28
+ - ruby: 2.6
29
+ gemfile: rails70
22
30
  - ruby: 3.0
23
31
  gemfile: rails50
24
32
  - ruby: 3.0
25
33
  gemfile: rails51
26
34
  - ruby: 3.0
27
35
  gemfile: rails52
36
+ - ruby: 3.1
37
+ gemfile: rails50
38
+ - ruby: 3.1
39
+ gemfile: rails51
40
+ - ruby: 3.1
41
+ gemfile: rails52
28
42
 
29
43
  name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
30
44
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.2.0
4
+
5
+ * Use coactive 0.3.
6
+
7
+ ## 2.1.0
8
+
9
+ * Add traversal config.
10
+ * Add callbacks for `call_all`.
11
+ * Add calling instrumentation.
12
+ * Clear class cache when reloaded.
13
+ * Bump coactive version to 0.2.
14
+
15
+ ## 2.0.1
16
+
17
+ * Return relation after callbacks called.
18
+
3
19
  ## 2.0.0
4
20
 
5
21
  * Replace chain feature with coactive.
data/README.md CHANGED
@@ -230,7 +230,9 @@ IIFinder::LogSubscriber.attach_to :ii_finder
230
230
  This subscriber will write logs in debug mode as the following example:
231
231
 
232
232
  ```
233
- Called ItemsFinder with {:id=>1} (Duration: 9.9ms, Allocations: 915)
233
+ Calling ItemsFinder with {:id=>1}
234
+ ...
235
+ Called ItemsFinder (Duration: 9.9ms, Allocations: 915)
234
236
  ```
235
237
 
236
238
  ## Contributing
data/bin/console CHANGED
@@ -10,5 +10,5 @@ require "ii_finder"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- # require "irb"
14
- # IRB.start
13
+ require "irb"
14
+ IRB.start
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem "rails", "~> 6.0.0"
4
+ gem "psych", "~> 3.3.0"
4
5
 
5
6
  gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "rails", "~> 7.0.1"
4
+
5
+ gemspec path: "../"
data/ii_finder.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_dependency "activesupport", ">= 5.0"
21
- spec.add_dependency "coactive", ">= 0.1"
21
+ spec.add_dependency "coactive", ">= 0.3"
22
22
 
23
23
  spec.add_development_dependency "rails", ">= 5.0"
24
24
  spec.add_development_dependency "sqlite3"
@@ -5,6 +5,8 @@ require_relative 'parameters'
5
5
  require_relative 'callbacks'
6
6
  require_relative 'instrumentation'
7
7
  require_relative 'lookup'
8
+ require_relative 'context'
9
+ require_relative 'contextualizer'
8
10
  require_relative 'coactors'
9
11
 
10
12
  module IIFinder
@@ -14,6 +16,7 @@ module IIFinder
14
16
  include Callbacks
15
17
  include Instrumentation
16
18
  include Lookup
19
+ include Contextualizer
17
20
  include Coactors
18
21
  end
19
22
  end
@@ -6,9 +6,16 @@ module IIFinder
6
6
  include ActiveSupport::Callbacks
7
7
 
8
8
  included do
9
+ define_callbacks :all
9
10
  define_callbacks :call
10
11
  end
11
12
 
13
+ def call_all
14
+ run_callbacks :all do
15
+ super
16
+ end
17
+ end
18
+
12
19
  def call
13
20
  run_callbacks :call do
14
21
  super
@@ -16,6 +23,18 @@ module IIFinder
16
23
  end
17
24
 
18
25
  class_methods do
26
+ def before_all(*args, &block)
27
+ set_callback(:all, :before, *args, &block)
28
+ end
29
+
30
+ def after_all(*args, &block)
31
+ set_callback(:all, :after, *args, &block)
32
+ end
33
+
34
+ def around_all(*args, &block)
35
+ set_callback(:all, :around, *args, &block)
36
+ end
37
+
19
38
  def before_call(*args, &block)
20
39
  set_callback(:call, :before, *args, &block)
21
40
  end
@@ -18,12 +18,5 @@ module IIFinder
18
18
  alias_method :chain, :coact
19
19
  end
20
20
  end
21
-
22
- def call
23
- coactors.each do |finder|
24
- merge_relation!(finder.call(*@_args))
25
- end
26
- super
27
- end
28
21
  end
29
22
  end
@@ -6,7 +6,8 @@ module IIFinder
6
6
 
7
7
  self.data = {
8
8
  lookup_cache: true,
9
- merge_relation: true
9
+ merge_relation: true,
10
+ traversal: :postorder,
10
11
  }
11
12
 
12
13
  data.keys.each do |key|
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIFinder
4
+ class Context < Coactive::Context
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIFinder
4
+ module Contextualizer
5
+ extend ActiveSupport::Concern
6
+ include Coactive::Contextualizer
7
+
8
+ def call
9
+ contextualize do
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
@@ -3,24 +3,40 @@
3
3
  module IIFinder
4
4
  module Core
5
5
  extend ActiveSupport::Concern
6
+ include Coactive::Initializer
6
7
 
7
8
  included do
8
- attr_reader :relation, :criteria, :model, :table
9
+ self.context_class = IIFinder::Context
10
+ context :relation, output: true
11
+ context :criteria, :model, :table
9
12
  end
10
13
 
11
14
  def initialize(*args)
12
- @_args = args;
13
- if args.size == 0 || args.size == 1
14
- @model = self.class.lookup
15
- raise IIFinder::Error.new("could not find model for #{self.class}") unless @model
16
- @relation = @model.all
17
- @criteria = args[0] || {}
15
+ if args[0].is_a?(self.class.context_class)
16
+ super(args[0])
18
17
  else
19
- @relation = args[0]
20
- @criteria = args[1]
21
- @model = @relation.klass
18
+ relation, criteria = Core.resolve_args(self, *args)
19
+ model = relation.klass
20
+ table = model.arel_table if model.respond_to?(:arel_table)
21
+ super(relation: relation, criteria: criteria, model: model, table: table)
22
+ end
23
+ end
24
+
25
+ def call_all
26
+ planned = case IIFinder.config.traversal
27
+ when :preorder
28
+ [self] + coactors
29
+ when :postorder
30
+ coactors + [self]
31
+ when :inorder
32
+ planned = coactors.in_groups(2, false)
33
+ planned[0] + [self] + planned[1]
34
+ end
35
+
36
+ planned.each do |finder|
37
+ relation = finder == self ? call : finder.call(@context)
38
+ @context.relation = @context.relation.merge(relation) if relation.respond_to?(:merge)
22
39
  end
23
- @table = @model.arel_table if @model.respond_to?(:arel_table)
24
40
  end
25
41
 
26
42
  def call
@@ -30,8 +46,6 @@ module IIFinder
30
46
  merge_relation!(send(param.name, value))
31
47
  end
32
48
  end
33
-
34
- @relation
35
49
  end
36
50
 
37
51
  def fetch_criteria(name)
@@ -50,7 +64,20 @@ module IIFinder
50
64
 
51
65
  class_methods do
52
66
  def call(*args)
53
- new(*args).call
67
+ finder = new(*args).tap(&:call_all)
68
+ finder.context.relation
69
+ end
70
+ end
71
+
72
+ class << self
73
+ def resolve_args(finder, *args)
74
+ if args.size == 0 || args.size == 1
75
+ model = finder.class.lookup
76
+ raise IIFinder::Error.new("could not find model for #{finder.class}") unless model
77
+ return model.all, args[0] || {}
78
+ else
79
+ return args[0], args[1]
80
+ end
54
81
  end
55
82
  end
56
83
  end
@@ -4,6 +4,11 @@ module IIFinder
4
4
  module Instrumentation
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ def call_all
8
+ ActiveSupport::Notifications.instrument 'calling.ii_finder', finder: self
9
+ super
10
+ end
11
+
7
12
  def call
8
13
  ActiveSupport::Notifications.instrument 'call.ii_finder', finder: self do
9
14
  super
@@ -2,13 +2,22 @@
2
2
 
3
3
  module IIFinder
4
4
  class LogSubscriber < ActiveSupport::LogSubscriber
5
+ def calling(event)
6
+ debug do
7
+ finder = event.payload[:finder]
8
+ "Calling #{finder.class} with #{finder.context}"
9
+ end
10
+ end
11
+
5
12
  def call(event)
6
13
  debug do
7
14
  finder = event.payload[:finder]
8
- "Called #{finder.class} with #{finder.criteria} (#{additional_log(event)})"
15
+ "Called #{finder.class} (#{additional_log(event)})"
9
16
  end
10
17
  end
11
18
 
19
+ private
20
+
12
21
  def additional_log(event)
13
22
  additions = ["Duration: %.1fms" % event.duration]
14
23
  additions << "Allocations: %d" % event.allocations if event.respond_to?(:allocations)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIFinder
4
+ class Railtie < Rails::Railtie
5
+ ActiveSupport::Reloader.to_prepare do
6
+ IIFinder::Lookup.cache.clear
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIFinder
4
- VERSION = '2.0.0'
4
+ VERSION = '2.2.0'
5
5
  end
data/lib/ii_finder.rb CHANGED
@@ -7,6 +7,7 @@ require 'ii_finder/errors'
7
7
  require 'ii_finder/base'
8
8
  require 'ii_finder/scope'
9
9
  require 'ii_finder/log_subscriber'
10
+ require 'ii_finder/railtie' if defined?(Rails)
10
11
 
11
12
  module IIFinder
12
13
  class << self
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: 2.0.0
4
+ version: 2.2.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-11-29 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.1'
33
+ version: '0.3'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.1'
40
+ version: '0.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rails
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -130,12 +130,15 @@ files:
130
130
  - gemfiles/rails52.gemfile
131
131
  - gemfiles/rails60.gemfile
132
132
  - gemfiles/rails61.gemfile
133
+ - gemfiles/rails70.gemfile
133
134
  - ii_finder.gemspec
134
135
  - lib/ii_finder.rb
135
136
  - lib/ii_finder/base.rb
136
137
  - lib/ii_finder/callbacks.rb
137
138
  - lib/ii_finder/coactors.rb
138
139
  - lib/ii_finder/config.rb
140
+ - lib/ii_finder/context.rb
141
+ - lib/ii_finder/contextualizer.rb
139
142
  - lib/ii_finder/core.rb
140
143
  - lib/ii_finder/errors.rb
141
144
  - lib/ii_finder/instrumentation.rb
@@ -143,6 +146,7 @@ files:
143
146
  - lib/ii_finder/lookup.rb
144
147
  - lib/ii_finder/parameter.rb
145
148
  - lib/ii_finder/parameters.rb
149
+ - lib/ii_finder/railtie.rb
146
150
  - lib/ii_finder/scope.rb
147
151
  - lib/ii_finder/version.rb
148
152
  homepage: https://github.com/kanety/ii_finder
@@ -163,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
167
  - !ruby/object:Gem::Version
164
168
  version: '0'
165
169
  requirements: []
166
- rubygems_version: 3.0.3
170
+ rubygems_version: 3.3.3
167
171
  signing_key:
168
172
  specification_version: 4
169
173
  summary: A base finder to support building relations from parameters