grape-entity-preloader 0.2.0 → 0.3.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: e1706cb12b11070d46f62c3b3a4e9aacb797e1aff8659ca4c41d2ee68306b5f1
4
- data.tar.gz: 6a060769e40134c573d14b235def6800b931c1fa8b87e84e5d370ce735384e2b
3
+ metadata.gz: 4435d853f0ac2438cbd9def7d195a78e4ce66dcfc0db4015b29f20c5102846b6
4
+ data.tar.gz: 2bde5dbb73a91201040bc0e21da30507638c1b61e164e494a6187dacc1ab328d
5
5
  SHA512:
6
- metadata.gz: f17b3f692873737c4e9a55e9a971324b77d8263ff08e62cb3d71bee414487e949fe8e76a6d9b088bf2442c4ce266f11041ae7bd3d0394fb236a635f6a8bbb951
7
- data.tar.gz: 0e7ba460164dcfad7736f3beb8947c4636d54f954227de6968db5836111171eb8b6b4126fa8348d303f29e15b840e9309c096da04c204a692a434bb419c88cec
6
+ metadata.gz: 36b9c7316b3cb852024039a2db9ffe0c55215f49c60614b49c95a23a1bf307432b8e564c8c09cd3b56b71a4ed5f212e5fbd756e87675901407acb8acd5a8b642
7
+ data.tar.gz: 8f9c9b1ed336765001069d34ab27e7b80e5a94d6a829ffd276ff501785ed15d4a8f43d9cdf762c6a79603487e16ddff00669e0a2222833c091e8ff8ebd7a6532
@@ -3,7 +3,7 @@
3
3
  module Grape
4
4
  class Entity
5
5
  class Preloader
6
- VERSION = '0.2.0'
6
+ VERSION = '0.3.0'
7
7
  end
8
8
  end
9
9
  end
@@ -26,18 +26,20 @@ module Grape
26
26
  end
27
27
  end
28
28
 
29
- def self.with_enable
29
+ def self.with_enable # rubocop:disable Metrics/MethodLength
30
30
  return yield if enabled?
31
31
 
32
- old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
33
- ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = true
32
+ begin
33
+ old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
34
+ ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = true
34
35
 
35
- yield
36
-
37
- if old_value.nil?
38
- ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
39
- else
40
- ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
36
+ yield
37
+ ensure
38
+ if old_value.nil?
39
+ ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
40
+ else
41
+ ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
42
+ end
41
43
  end
42
44
  end
43
45
 
@@ -49,18 +51,20 @@ module Grape
49
51
  !enabled?
50
52
  end
51
53
 
52
- def self.with_disable
54
+ def self.with_disable # rubocop:disable Metrics/MethodLength
53
55
  return yield if disabled?
54
56
 
55
- old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
56
- ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = false
57
+ begin
58
+ old_value = ActiveSupport::IsolatedExecutionState[:grape_entity_preloader]
59
+ ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = false
57
60
 
58
- yield
59
-
60
- if old_value.nil?
61
- ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
62
- else
63
- ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
61
+ yield
62
+ ensure
63
+ if old_value.nil?
64
+ ActiveSupport::IsolatedExecutionState.delete(:grape_entity_preloader)
65
+ else
66
+ ActiveSupport::IsolatedExecutionState[:grape_entity_preloader] = old_value
67
+ end
64
68
  end
65
69
  end
66
70
 
@@ -4,4 +4,32 @@ RSpec.describe Grape::Entity::Preloader do
4
4
  it 'has a version number' do
5
5
  expect(Grape::Entity::Preloader::VERSION).not_to be_nil
6
6
  end
7
+
8
+ describe '.with_enable' do
9
+ it 'returns the value of the block' do
10
+ result = described_class.with_enable { 42 }
11
+ expect(result).to eq(42)
12
+ end
13
+
14
+ it 'returns the value of the block when already enabled' do
15
+ described_class.enabled!
16
+ result = described_class.with_enable { 'hello' }
17
+ expect(result).to eq('hello')
18
+ ensure
19
+ described_class.disabled!
20
+ end
21
+ end
22
+
23
+ describe '.with_disable' do
24
+ it 'returns the value of the block' do
25
+ result = described_class.with_disable { 42 }
26
+ expect(result).to eq(42)
27
+ end
28
+
29
+ it 'returns the value of the block when already disabled' do
30
+ described_class.disabled!
31
+ result = described_class.with_disable { 'hello' }
32
+ expect(result).to eq('hello')
33
+ end
34
+ end
7
35
  end
@@ -105,7 +105,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
105
105
  User::Entity.represent(
106
106
  users,
107
107
  serializable: true,
108
- only: [books_by_association: %i[tags_by_association tags_by_callback]]
108
+ only: [{ books_by_association: %i[tags_by_association tags_by_callback] }]
109
109
  )
110
110
  end.to make_database_queries(count: 3)
111
111
  .and make_database_queries(count: 1, matching: /user_books_by_association/)
@@ -149,7 +149,7 @@ RSpec.describe 'N+1 operation' do # rubocop:disable RSpec/DescribeClass
149
149
  User::Entity.represent(
150
150
  users,
151
151
  serializable: true,
152
- only: [books_by_callback: %i[tags_by_association tags_by_callback]]
152
+ only: [{ books_by_callback: %i[tags_by_association tags_by_callback] }]
153
153
  )
154
154
  end.to make_database_queries(count: 3)
155
155
  .and make_database_queries(count: 1, matching: /books_by_callback/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-entity-preloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OuYangJinTing