ii_interactor 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca26ce09ce20cc37d4e35337c05a5f49abf260a648752efeb1ac0e887f85dd8c
4
- data.tar.gz: 0b6f575ef339301f4501b5abd0104dc8a4ce083fba0fe9e0fae4f8cc09b0d023
3
+ metadata.gz: 3d08217ca0d49ae98a2c034ed00bb785aca82359dd90a2d06285a6b6e850b5ac
4
+ data.tar.gz: 85d61cde0ae2f9e828637fcf5cd7144aabf9ba42d9842c582f9a86d06e7328d2
5
5
  SHA512:
6
- metadata.gz: cc4b8a3cfa9b100299f91c17e04efa110c8c482dd9a2d1365003f962bf27fc3682822b5970453bbf9e6d19b6a4267639052aa7acdf23a9c38a24245cdabeb323
7
- data.tar.gz: a882894b2f36b99e1aed993ae7bf888c8fc9261ec8f1c9f1a8f15ce39a67e374476dff2baa6c4a643e3d946a0fa7d8956aa91dcfcf26a1af4395cde0da466fd1
6
+ metadata.gz: 148a1e202624098aef8cbe7fcbededf1cd86a03d33c070768e1d75f0aa848363b45e4a9b349e6b614e1f753710d8256a4ac3aed9a39d2bf91a7444a9eb0140fd
7
+ data.tar.gz: daf5475ab67cae1eb42043c5b0cc20334683f94e4fdf908e222bcb67e323f81606b8b888a5e14c07b3d31c4a7ee1f68c285fa692f784fe575098f51ed6f20c89
@@ -39,4 +39,4 @@ jobs:
39
39
  bundler-cache: true
40
40
  - name: Run test
41
41
  run: |
42
- bundle exec rspec
42
+ DEBUG=1 bundle exec rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.1.0
4
+
5
+ * Add log subscriber.
6
+
7
+ ## 1.0.0
8
+
9
+ * First release.
data/README.md CHANGED
@@ -359,6 +359,21 @@ end
359
359
  # BInteractor: called B
360
360
  ```
361
361
 
362
+ ### Logging
363
+
364
+ Interactor supports instrumentation hook supplied by `ActiveSupport::Notifications`.
365
+ You can enable log subscriber as follows:
366
+
367
+ ```ruby
368
+ IIInteractor::LogSubscriber.attach_to :ii_interactor
369
+ ```
370
+
371
+ This subscriber will write logs in debug mode as the following example:
372
+
373
+ ```
374
+ Called SimpleInteractor (Duration: 0.3ms, Allocations: 42)
375
+ ```
376
+
362
377
  ## Contributing
363
378
 
364
379
  Bug reports and pull requests are welcome at https://github.com/kanety/ii_interactor.
data/lib/ii_interactor.rb CHANGED
@@ -5,6 +5,7 @@ require 'ii_interactor/errors'
5
5
  require 'ii_interactor/config'
6
6
  require 'ii_interactor/base'
7
7
  require 'ii_interactor/loader'
8
+ require 'ii_interactor/log_subscriber'
8
9
 
9
10
  module IIInteractor
10
11
  class << self
@@ -1,76 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'context'
4
+ require_relative 'core'
4
5
  require_relative 'callbacks'
6
+ require_relative 'instrumentation'
5
7
  require_relative 'interaction'
6
8
  require_relative 'lookup'
7
9
 
8
10
  module IIInteractor
9
11
  class Base
12
+ include Core
10
13
  include Callbacks
14
+ include Instrumentation
11
15
  include Interaction
12
16
  include Lookup
13
-
14
- attr_reader :context
15
-
16
- def initialize(context = {}, &block)
17
- @context = if context.is_a?(IIInteractor::Context)
18
- context
19
- else
20
- IIInteractor::Context.new(context, &block)
21
- end
22
- end
23
-
24
- def call_all
25
- planned = lookup.map { |interactor| interactor.new(@context) } + [self]
26
- @context._planned += planned
27
- planned.each_with_index do |interactor, i|
28
- if i == planned.size - 1
29
- interactor.call_self
30
- else
31
- interactor.call_all
32
- end
33
- break if @context.stopped?
34
- end
35
- end
36
-
37
- def call_self
38
- run_callbacks :call do
39
- call
40
- end
41
- @context._called << self
42
- end
43
-
44
- def call
45
- end
46
-
47
- def rollback
48
- end
49
-
50
- def inform(*args)
51
- @context._block.call(*([self] + args)) if @context._block
52
- end
53
-
54
- def fail!(data = {})
55
- @context.fail!(data)
56
- raise UnprogressableError.new(@context)
57
- end
58
-
59
- def stop!(data = {})
60
- @context.stop!(data)
61
- end
62
-
63
- class << self
64
- def call(context = {}, &block)
65
- interactor = new(context, &block)
66
- interactor.call_all
67
- interactor.context
68
- rescue UnprogressableError
69
- interactor.context._called.reverse.each do |called|
70
- called.rollback
71
- end
72
- interactor.context
73
- end
74
- end
75
17
  end
76
18
  end
@@ -9,6 +9,12 @@ module IIInteractor
9
9
  define_callbacks :call
10
10
  end
11
11
 
12
+ def call_self
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,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIInteractor
4
+ module Core
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ attr_reader :context
9
+ end
10
+
11
+ def initialize(context = {}, &block)
12
+ @context = if context.is_a?(IIInteractor::Context)
13
+ context
14
+ else
15
+ IIInteractor::Context.new(context, &block)
16
+ end
17
+ end
18
+
19
+ def call_all
20
+ planned = lookup.map { |interactor| interactor.new(@context) } + [self]
21
+ @context._planned += planned
22
+ planned.each_with_index do |interactor, i|
23
+ if i == planned.size - 1
24
+ interactor.call_self
25
+ else
26
+ interactor.call_all
27
+ end
28
+ break if @context.stopped?
29
+ end
30
+ end
31
+
32
+ def call_self
33
+ call
34
+ @context._called << self
35
+ end
36
+
37
+ def call
38
+ end
39
+
40
+ def rollback
41
+ end
42
+
43
+ def inform(*args)
44
+ @context._block.call(*([self] + args)) if @context._block
45
+ end
46
+
47
+ def fail!(data = {})
48
+ @context.fail!(data)
49
+ raise UnprogressableError.new(@context)
50
+ end
51
+
52
+ def stop!(data = {})
53
+ @context.stop!(data)
54
+ end
55
+
56
+ class_methods do
57
+ def call(context = {}, &block)
58
+ interactor = new(context, &block)
59
+ interactor.call_all
60
+ interactor.context
61
+ rescue UnprogressableError
62
+ interactor.context._called.reverse.each do |called|
63
+ called.rollback
64
+ end
65
+ interactor.context
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIInteractor
4
+ module Instrumentation
5
+ extend ActiveSupport::Concern
6
+
7
+ def call_self
8
+ ActiveSupport::Notifications.instrument 'call.ii_interactor', interactor: self do
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IIInteractor
4
+ class LogSubscriber < ActiveSupport::LogSubscriber
5
+ def call(event)
6
+ debug do
7
+ interactor = event.payload[:interactor]
8
+ "Called #{interactor.class} (#{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
@@ -41,11 +41,11 @@ module IIInteractor
41
41
  class_attribute :lookups
42
42
  self.lookups = [Lookups::Name, Lookups::Object]
43
43
 
44
- class_attribute :_cache
45
- self._cache = {}
44
+ class_attribute :cache
45
+ self.cache = {}
46
46
 
47
47
  def call(klass, interaction)
48
- cache(klass, interaction) do
48
+ with_cache(klass, interaction) do
49
49
  lookup = lookups.detect { |lookup| lookup.call?(interaction) }
50
50
  lookup.new(klass, interaction).call if lookup
51
51
  end
@@ -53,10 +53,10 @@ module IIInteractor
53
53
 
54
54
  private
55
55
 
56
- def cache(klass, interaction)
56
+ def with_cache(klass, interaction)
57
57
  if Config.lookup_cache
58
- self._cache[klass] ||= {}
59
- self._cache[klass][interaction] ||= yield
58
+ self.cache[klass] ||= {}
59
+ self.cache[klass][interaction] ||= yield
60
60
  else
61
61
  yield
62
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IIInteractor
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_interactor
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-13 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
@@ -104,6 +104,7 @@ files:
104
104
  - ".github/workflows/ci.yml"
105
105
  - ".gitignore"
106
106
  - ".rspec"
107
+ - CHANGELOG.md
107
108
  - Gemfile
108
109
  - LICENSE
109
110
  - README.md
@@ -121,9 +122,12 @@ files:
121
122
  - lib/ii_interactor/callbacks.rb
122
123
  - lib/ii_interactor/config.rb
123
124
  - lib/ii_interactor/context.rb
125
+ - lib/ii_interactor/core.rb
124
126
  - lib/ii_interactor/errors.rb
127
+ - lib/ii_interactor/instrumentation.rb
125
128
  - lib/ii_interactor/interaction.rb
126
129
  - lib/ii_interactor/loader.rb
130
+ - lib/ii_interactor/log_subscriber.rb
127
131
  - lib/ii_interactor/lookup.rb
128
132
  - lib/ii_interactor/lookups/base.rb
129
133
  - lib/ii_interactor/lookups/name.rb