batch-loader 1.3.0 → 1.4.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
  SHA1:
3
- metadata.gz: a7165f52041d179cfe8de386a0d761d35d1cbca2
4
- data.tar.gz: 0d4f594bea1a9d78cb3b94f41c90cf14860dedee
3
+ metadata.gz: e5518ce90e0c03eda9c5717a9fb50ac7c159f551
4
+ data.tar.gz: 3081111f1bcaf9311d4de1769a3d8ce38f5f6552
5
5
  SHA512:
6
- metadata.gz: 157d5e17609ef8dc6df9ee49755441083d5d49f5b482e14087d8fba3d004ecb2e1a721ae28eb77a5b23be399c99383cf8cc29cdc0e0efe7d88d051bd44c80412
7
- data.tar.gz: a2ad4e96c32219022efa84d7d27bfb90244ce8e8c54072370d5e667031bbc2bf37faee159f645d632b72d440b45a6c2e0b5074c216572ff7b3bf9cc334362cc3
6
+ metadata.gz: 658b9c3bb6e606f86aa5caa1b89d75e52445d6d227f0d7dd0ca91373144885c29f5a64ced62c602b4f61f816b65b87770bb82ef5ad8cb9314cb75a8baeab5ace
7
+ data.tar.gz: 394300f8a32614af28f17483485dc09efb1246d6f366c7458f3154bffac65660135811e2dee01e74330c1c0b928328037e48f8dbf674e3bda1b3c6d7e182f400
data/CHANGELOG.md CHANGED
@@ -8,13 +8,17 @@ one of the following labels: `Added`, `Changed`, `Deprecated`,
8
8
  to manage the versions of this gem so
9
9
  that you can set version constraints properly.
10
10
 
11
- #### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v1.3.0...HEAD)
11
+ #### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v1.4.0...HEAD)
12
12
 
13
13
  * WIP
14
14
 
15
+ #### [v1.4.0](https://github.com/exAspArk/batch-loader/compare/v1.3.0...v1.4.0)
16
+
17
+ * `Added`: new `replace_methods` argument to `BatchLoader#batch` to allow control over `define_method` calls. [#45](https://github.com/exAspArk/batch-loader/pull/45)
18
+
15
19
  #### [v1.3.0](https://github.com/exAspArk/batch-loader/compare/v1.2.2...v1.3.0)
16
20
 
17
- * `Added`: `BatchLoader::GraphQL` to make it work with `graphql` gem version `>= 1.8.7`. [#30](https://github.com/exAspArk/batch-loader/issues/30)
21
+ * `Added`: `BatchLoader::GraphQL.for` to make it compatible with `graphql` gem versions `>= 1.8.7`. [#30](https://github.com/exAspArk/batch-loader/issues/30)
18
22
 
19
23
  #### [v1.2.2](https://github.com/exAspArk/batch-loader/compare/v1.2.1...v1.2.2)
20
24
 
data/README.md CHANGED
@@ -20,6 +20,7 @@ This gem provides a generic lazy batching mechanism to avoid N+1 DB queries, HTT
20
20
  * [Loading multiple items](#loading-multiple-items)
21
21
  * [Batch key](#batch-key)
22
22
  * [Caching](#caching)
23
+ * [Replacing methods](#replacing-methods)
23
24
  * [Installation](#installation)
24
25
  * [API](#api)
25
26
  * [Implementation details](#implementation-details)
@@ -374,6 +375,21 @@ puts user_lazy(1) # SELECT * FROM users WHERE id IN (1)
374
375
  puts user_lazy(1) # SELECT * FROM users WHERE id IN (1)
375
376
  ```
376
377
 
378
+ If you set `cache: false`, it's likely you also want `replace_methods: false` (see below section).
379
+
380
+ ### Replacing methods
381
+
382
+ By default, `BatchLoader` replaces methods on its instance by calling `#define_method` after batching to copy methods from the loaded value.
383
+ This consumes some time but allows to speed up any future method calls on the instance.
384
+ In some cases, when there are a lot of instances with a huge number of defined methods, this initial process of replacing the methods can be slow.
385
+ You may consider avoiding the "up front payment" and "pay as you go" with `#method_missing` by disabling the method replacement:
386
+
387
+ ```ruby
388
+ BatchLoader.for(id).batch(replace_methods: false) do |ids, loader|
389
+ # ...
390
+ end
391
+ ```
392
+
377
393
  ## Installation
378
394
 
379
395
  Add this line to your application's Gemfile:
@@ -393,20 +409,26 @@ Or install it yourself as:
393
409
  ## API
394
410
 
395
411
  ```ruby
396
- BatchLoader.for(item).batch(default_value: default_value, cache: cache, key: key) do |items, loader, args|
412
+ BatchLoader.for(item).batch(
413
+ default_value: default_value,
414
+ cache: cache,
415
+ replace_methods: replace_methods,
416
+ key: key
417
+ ) do |items, loader, args|
397
418
  # ...
398
419
  end
399
420
  ```
400
421
 
401
- | Argument Key | Default | Description |
402
- | --------------- | --------------------------------------------- | ------------------------------------------------------------- |
403
- | `item` | - | Item which will be collected and used for batching. |
404
- | `default_value` | `nil` | Value returned by default after batching. |
405
- | `cache` | `true` | Set `false` to disable caching between the same executions. |
406
- | `key` | `nil` | Pass custom key to uniquely identify the batch block. |
407
- | `items` | - | List of collected items for batching. |
408
- | `loader` | - | Lambda which should be called to load values loaded in batch. |
409
- | `args` | `{default_value: nil, cache: true, key: nil}` | Arguments passed to the `batch` method. |
422
+ | Argument Key | Default | Description |
423
+ | --------------- | --------------------------------------------- | ------------------------------------------------------------- |
424
+ | `item` | - | Item which will be collected and used for batching. |
425
+ | `default_value` | `nil` | Value returned by default after batching. |
426
+ | `cache` | `true` | Set `false` to disable caching between the same executions. |
427
+ | `replace_methods` | `true` | Set `false` to use `#method_missing` instead of replacing the methods after batching. |
428
+ | `key` | `nil` | Pass custom key to uniquely identify the batch block. |
429
+ | `items` | - | List of collected items for batching. |
430
+ | `loader` | - | Lambda which should be called to load values loaded in batch. |
431
+ | `args` | `{default_value: nil, cache: true, replace_methods: true, key: nil}` | Arguments passed to the `batch` method. |
410
432
 
411
433
  ## Implementation details
412
434
 
data/lib/batch_loader.rb CHANGED
@@ -23,11 +23,13 @@ class BatchLoader
23
23
  @__executor_proxy = executor_proxy
24
24
  end
25
25
 
26
- def batch(default_value: nil, cache: true, key: nil, &batch_block)
26
+ def batch(default_value: nil, cache: true, replace_methods: nil, key: nil, &batch_block)
27
27
  @default_value = default_value
28
28
  @cache = cache
29
+ @replace_methods = replace_methods.nil? ? cache : replace_methods
29
30
  @key = key
30
31
  @batch_block = batch_block
32
+
31
33
  __executor_proxy.add(item: @item)
32
34
 
33
35
  __singleton_class.class_eval { undef_method(:batch) }
@@ -74,7 +76,7 @@ class BatchLoader
74
76
  def __sync!
75
77
  loaded_value = __sync
76
78
 
77
- if @cache
79
+ if @replace_methods
78
80
  __replace_with!(loaded_value)
79
81
  else
80
82
  loaded_value
@@ -86,7 +88,7 @@ class BatchLoader
86
88
 
87
89
  items = __executor_proxy.list_items
88
90
  loader = __loader
89
- args = {default_value: @default_value, cache: @cache, key: @key}
91
+ args = {default_value: @default_value, cache: @cache, replace_methods: @replace_methods, key: @key}
90
92
  @batch_block.call(items, loader, args)
91
93
  items.each do |item|
92
94
  next if __executor_proxy.value_loaded?(item: item)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class BatchLoader
4
- VERSION = "1.3.0"
4
+ VERSION = "1.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch-loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - exAspArk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-01 00:00:00.000000000 Z
11
+ date: 2019-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler