bulk_loader 0.2.0 → 0.3.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: 2f597dd144f901e0dfdd7118b2c4b3195f9de24a42344efebfd338bb79b2eb7f
4
- data.tar.gz: '094b05be80e70f378067597d5197ec849828416513a7a5012b9932f741bca36d'
3
+ metadata.gz: b6dece1077abb8076b7b22b30d4db5565f175417fe29e17043e6e2e1f226684c
4
+ data.tar.gz: d6af0bf31f863df6a00bd90a057e74becbc3e67bc140f54a2dc07297914ef0e6
5
5
  SHA512:
6
- metadata.gz: c934949c8af3328af455fd1c0964e594f422346f68efae6a4b5dd58eb0d9cb4ee85b5fb0f0e2d994567d6e2d6ea74760d9dd7186de6ce722bc7d539592ddde2b
7
- data.tar.gz: 35971ca21b9685d44755697520373484b60bdb68f9f5edbdbca45bec77e30dc5d3a88504526c6beb14ebd1ef55c2d9319fb5ae13db314f09e353d2655162530e
6
+ metadata.gz: c49b93dbcd1cd1d09cf50085022bcb2198a6771cd4db44c42e607eb0eca42937c26c6727133d3b8556d5022871f48c6a16997ecd81eae2d9c6041b61c0990804
7
+ data.tar.gz: b899710ae8b53ab5fdeb399c122f35d50c0db73994fcffc3132d37c5794fc6ef97eb5d02522216daef2efc7fc84d0e069a985655b3423aeb89a7c43cf5ac403c
data/.rubocop.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # See default settings https://github.com/bbatsov/rubocop/blob/master/config/enabled.yml
2
2
  AllCops:
3
- TargetRubyVersion: 2.3
3
+ TargetRubyVersion: 2.4
4
4
  DisplayCopNames: true
5
5
  Exclude:
6
6
  - '*.gemspec'
@@ -10,6 +10,12 @@ AllCops:
10
10
  - 'Rakefile'
11
11
  - 'example/**/*'
12
12
 
13
+ Metrics/AbcSize:
14
+ Max: 17
15
+
16
+ Metrics/MethodLength:
17
+ Max: 14
18
+
13
19
  Metrics/BlockLength:
14
20
  Exclude:
15
21
  - 'spec/**/*'
@@ -16,6 +16,7 @@ module BulkLoader
16
16
 
17
17
  def method_missing(name, *args)
18
18
  return super unless @class_attribute.include?(name)
19
+
19
20
  names = [name].freeze
20
21
  define_singleton_method(name) do
21
22
  attr = lazy(name)
@@ -27,6 +28,7 @@ module BulkLoader
27
28
 
28
29
  def respond_to_missing?(name, include_private)
29
30
  return true if @class_attribute.include?(name)
31
+
30
32
  super
31
33
  end
32
34
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulkLoader
4
+ class LoaderNotFoundError < StandardError; end
5
+
4
6
  class ClassAttribute
5
7
  def initialize
6
8
  @loader_of = {}
@@ -19,6 +21,8 @@ module BulkLoader
19
21
 
20
22
  loader_names = [loader_names] unless loader_names.is_a?(Array)
21
23
  loader_names.each do |name|
24
+ raise LoaderNotFoundError, "bulk_loader :#{name} is not defined" unless @loader_of[name]
25
+
22
26
  @loader_of[name].load(attrs.map { |attr| attr.lazy(name) }, *args)
23
27
  end
24
28
  end
@@ -32,6 +36,14 @@ module BulkLoader
32
36
  "#<BulkLoader::ClassAttribute:#{object_id} #{loader_inspect}>"
33
37
  end
34
38
 
39
+ def dup
40
+ new_object = self.class.new
41
+ each do |key, loader|
42
+ new_object.define_loader(key, loader)
43
+ end
44
+ new_object
45
+ end
46
+
35
47
  private
36
48
 
37
49
  def loader_inspect
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'weakref'
4
-
5
3
  module BulkLoader
6
4
  module DSL
7
5
  module ClassMethods
@@ -9,7 +7,12 @@ module BulkLoader
9
7
  # If you pass name, mapping, options argument, you can define loader
10
8
  # if you does not want to export name to object, pass export: false to options.
11
9
  def bulk_loader(*args, &block)
12
- @bulk_loader ||= BulkLoader::ClassAttribute.new
10
+ @bulk_loader ||= if superclass.respond_to?(:bulk_loader)
11
+ superclass.bulk_loader.dup
12
+ else
13
+ BulkLoader::ClassAttribute.new
14
+ end
15
+
13
16
  return @bulk_loader if args.empty?
14
17
 
15
18
  name, mapping, options = *args
@@ -34,7 +37,7 @@ module BulkLoader
34
37
  return @bulk_loader if @bulk_loader
35
38
 
36
39
  class_attribute = self.class.bulk_loader
37
- @bulk_loader = BulkLoader::Attribute.new(class_attribute, WeakRef.new(self))
40
+ @bulk_loader = BulkLoader::Attribute.new(class_attribute, self)
38
41
  end
39
42
  end
40
43
  end
@@ -13,6 +13,7 @@ module BulkLoader
13
13
 
14
14
  def get
15
15
  raise 'data is not loaded!!' unless @loaded
16
+
16
17
  @value
17
18
  end
18
19
 
@@ -35,6 +35,7 @@ module BulkLoader
35
35
  end
36
36
  result_of = @block.call(mapping_of.keys, *args)
37
37
  raise 'block shuold return Hash' unless result_of.is_a?(Hash)
38
+
38
39
  result_of
39
40
  end
40
41
 
@@ -52,6 +53,7 @@ module BulkLoader
52
53
  def set_result_to_lazy_objs(result_of, lazy_obj_of, mapping_of)
53
54
  result_of.each do |mapped_target, value|
54
55
  next unless mapping_of[mapped_target]
56
+
55
57
  mapping_of[mapped_target].each do |target|
56
58
  lazy_obj_of[target]&.set(value)
57
59
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulkLoader
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk_loader
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
  - Keiji Yoshimi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-06 00:00:00.000000000 Z
11
+ date: 2019-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -152,8 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0'
154
154
  requirements: []
155
- rubyforge_project:
156
- rubygems_version: 2.6.14
155
+ rubygems_version: 3.0.3
157
156
  signing_key:
158
157
  specification_version: 4
159
158
  summary: utility to avoid N+1 queries