bulk_loader 1.0.0 → 2.0.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: 905e932ce017b63197cd743a2c856e50def0c6d52083d5c287a6d1463124f0e3
4
- data.tar.gz: a6481e11d19e6eb455d5f54c5a2f9e736561cdd9c6dbe7ff3053ac06e9e7a711
3
+ metadata.gz: 7f1d67abfc96cf07c2567f79aea5d9aa72a788a4eb1177cfb82f54a895645ba7
4
+ data.tar.gz: 7d35e81f819371a5cf2c2962f16b3b0be51c1ea6dd56bd209bcf4a548f9ea092
5
5
  SHA512:
6
- metadata.gz: 8a2ef5b610d4635c521f0a04fef831e19b1b330473054cd2b99d8da340f6433bced83c2683f8b9125d5a186ce5c0a7fca31c8aa54a99501c85d1e629f42a81af
7
- data.tar.gz: 49747836ca7331d9134deaad2ac9840a51c92a7ec0a5e48fc845e862a69300ce1cd1c035ec84be39258fe80d86caf448dd71019b6912b90260be788ad0bf4b2c
6
+ metadata.gz: 7c6c1a2de3185b089168f03046fc115f523c80b4319b83c366cd58f40bb43c6719e7471c9890618c915b2a4be5983b3397f97e148b50114d997d6bfe0069d3c1
7
+ data.tar.gz: 4d1f5ee95f42cfc48967261c5af00214da6716f7a5b7602481de83ff740aa86c9e443af5f0924423bb2dda77ed19fbdbe115d4f7cebee7fb87a5008e0e58cf84
@@ -0,0 +1,23 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ ruby: ['3.0', '2.7', '2.6', '2.5']
12
+
13
+ steps:
14
+ - uses: actions/checkout@v1
15
+ - name: Set up Ruby ${{ matrix.ruby }}
16
+ uses: ruby/setup-ruby@v1
17
+ with:
18
+ ruby-version: ${{ matrix.ruby }}
19
+ - name: Build and test with Rake
20
+ run: |
21
+ gem install bundler
22
+ bundle install --jobs 4 --retry 3
23
+ bundle exec rake
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
+ --warnings
2
3
  --color
data/.rubocop.yml CHANGED
@@ -8,20 +8,25 @@ AllCops:
8
8
  - 'Guardfile'
9
9
  - 'bin/*'
10
10
  - 'Rakefile'
11
+ - 'Steepfile'
11
12
  - 'example/**/*'
13
+ NewCops: enable
12
14
 
13
15
  Metrics/AbcSize:
14
- Max: 17
16
+ Max: 22
15
17
 
16
18
  Metrics/MethodLength:
17
- Max: 14
19
+ Max: 20
18
20
 
19
21
  Metrics/BlockLength:
20
22
  Exclude:
21
23
  - 'spec/**/*'
22
24
 
23
- Metrics/LineLength:
25
+ Layout/LineLength:
24
26
  Max: 100
25
27
 
26
28
  Style/Documentation:
27
29
  Enabled: false
30
+
31
+ Lint/ConstantDefinitionInBlock:
32
+ Enabled: false
data/README.md CHANGED
@@ -80,7 +80,7 @@ class YourModel
80
80
  end
81
81
  ```
82
82
 
83
- #### export: false
83
+ #### export: false option ( default: true )
84
84
 
85
85
  :name method is just shorthand for bulk\_loader.public\_send(:name). So if you not want to create :name method, you can pass export: false to bulk\_loader definition.
86
86
 
@@ -100,6 +100,32 @@ then you can use this like followings.
100
100
  YourModel.new.bulk_loader.name
101
101
  ```
102
102
 
103
+ #### autoload: false option ( default: true )
104
+
105
+ If you set this option to false, +BulkLoader::UnloadAccessError+ occured when you does not call +YourModel.bulk\_loader.load explicitly on :name method.
106
+
107
+ ```ruby
108
+ class YourModel
109
+ include BulkLoader::DSL
110
+
111
+ bulk_loader :name, :mapped_key, default: nil, autoload: false do |mapped_keys|
112
+ # something with mapped_keys
113
+ end
114
+ end
115
+ ```
116
+
117
+ ```ruby
118
+ YourModel.new.name #=> raise error BulkLoader::UnloadAccessError
119
+ ```
120
+
121
+ You can pass this by calling load explicitly.
122
+
123
+ ```ruby
124
+ model = YourModel.new
125
+ YourModel.bulk_loader.load(:name, [model])
126
+ model.name #=> it does not raise BulkLoader::UnloadAccessError
127
+ ```
128
+
103
129
  ## Installation
104
130
 
105
131
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
5
7
 
6
- task :default => :spec
8
+ task :default => [:spec, :rubocop]
data/bulk_loader.gemspec CHANGED
@@ -20,10 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.15"
24
- spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "bundler", ">= 1.15"
24
+ spec.add_development_dependency "rake", ">= 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_development_dependency "rubocop", "~> 0.49"
26
+ spec.add_development_dependency "rubocop", ">= 0.74"
27
27
  spec.add_development_dependency "guard", "~> 2.14"
28
28
  spec.add_development_dependency "guard-rspec", "~> 4.7.3"
29
29
  spec.add_development_dependency "guard-rubocop", "~> 1.3.0"
@@ -8,7 +8,11 @@ module BulkLoader
8
8
  end
9
9
 
10
10
  def lazy(name)
11
- @lazy_of[name] ||= BulkLoader::Lazy.new(@target)
11
+ @lazy_of[name] ||= BulkLoader::Lazy.new(@target, name: name)
12
+ end
13
+
14
+ def loaded?(name)
15
+ lazy(name).loaded?
12
16
  end
13
17
 
14
18
  def marshal_dump
@@ -35,7 +39,7 @@ module BulkLoader
35
39
  names = [name].freeze
36
40
  define_singleton_method(name) do
37
41
  attr = lazy(name)
38
- class_attribute.load(names, [self]) unless attr.loaded?
42
+ class_attribute.load(names, [self]) if !attr.loaded? && class_attribute.autoload?(name)
39
43
  attr.get
40
44
  end
41
45
  public_send(name)
@@ -6,12 +6,18 @@ module BulkLoader
6
6
  class ClassAttribute
7
7
  def initialize
8
8
  @loader_of = {}
9
+
10
+ @no_autoload_of = {}
9
11
  end
10
12
 
11
13
  def include?(name)
12
14
  @loader_of.include?(name)
13
15
  end
14
16
 
17
+ def autoload?(name)
18
+ !@no_autoload_of[name]
19
+ end
20
+
15
21
  def each(&block)
16
22
  @loader_of.each(&block)
17
23
  end
@@ -27,8 +33,9 @@ module BulkLoader
27
33
  end
28
34
  end
29
35
 
30
- def define_loader(name, loader)
36
+ def define_loader(name, loader, autoload: true)
31
37
  @loader_of[name] = loader
38
+ @no_autoload_of[name] = true unless autoload
32
39
  define_singleton_method(name) { loader }
33
40
  end
34
41
 
@@ -6,7 +6,9 @@ module BulkLoader
6
6
  # getter for +BulkLoader::ClassAttribute+
7
7
  # If you pass name, mapping, options argument, you can define loader
8
8
  # if you does not want to export name to object, pass export: false to options.
9
- def bulk_loader(*args, &block)
9
+ # if you would like to raise error when implicitly calling loader, pass autoload: false
10
+ # to options.
11
+ def bulk_loader(*args, export: nil, autoload: true, default: nil, &block)
10
12
  @bulk_loader ||= if superclass.respond_to?(:bulk_loader)
11
13
  superclass.bulk_loader.dup
12
14
  else
@@ -15,13 +17,12 @@ module BulkLoader
15
17
 
16
18
  return @bulk_loader if args.empty?
17
19
 
18
- name, mapping, options = *args
19
- options ||= {}
20
- does_export = options.delete(:export)
20
+ name, mapping = *args
21
21
 
22
- @bulk_loader.define_loader(name, BulkLoader::Loader.new(mapping, options, &block))
22
+ loader = BulkLoader::Loader.new(mapping, default: default, &block)
23
+ @bulk_loader.define_loader(name, loader, autoload: autoload)
23
24
 
24
- return if does_export == false
25
+ return if export == false
25
26
 
26
27
  define_method name do
27
28
  bulk_loader.public_send(name)
@@ -34,7 +35,7 @@ module BulkLoader
34
35
  end
35
36
 
36
37
  def bulk_loader
37
- return @bulk_loader if @bulk_loader
38
+ return @bulk_loader if defined?(@bulk_loader) && @bulk_loader
38
39
 
39
40
  @bulk_loader = BulkLoader::Attribute.new(self)
40
41
  end
@@ -1,18 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulkLoader
4
+ class UnloadAccessError < StandardError; end
5
+
4
6
  # lazy class
5
7
  class Lazy
6
8
  attr_reader :target
7
9
 
8
- def initialize(target)
10
+ def initialize(target, name: nil)
9
11
  @loaded = false
10
12
  @value = nil
13
+ @name = name
11
14
  @target = target
12
15
  end
13
16
 
14
17
  def get
15
- raise 'data is not loaded!!' unless @loaded
18
+ raise UnloadAccessError, "#{@name} has not been loaded!!" unless @loaded
16
19
 
17
20
  @value
18
21
  end
@@ -13,7 +13,7 @@ module BulkLoader
13
13
  end
14
14
 
15
15
  def load(lazy_objs, *args)
16
- lazy_obj_of = lazy_objs.each_with_object({}) { |e, h| h[e.target] = e }
16
+ return if lazy_objs.empty?
17
17
 
18
18
  mapping_of = get_mapping(lazy_objs)
19
19
 
@@ -21,7 +21,7 @@ module BulkLoader
21
21
 
22
22
  lazy_objs.each(&:clear)
23
23
 
24
- set_result_to_lazy_objs(result_of, lazy_obj_of, mapping_of)
24
+ set_result_to_lazy_objs(result_of, mapping_of)
25
25
 
26
26
  fill_default_to_unloaded_obj(lazy_objs)
27
27
  end
@@ -30,32 +30,33 @@ module BulkLoader
30
30
 
31
31
  def call_block(mapping_of, *args)
32
32
  if args.size < @block.arity - 1
33
- message = "block should take #{@block.arity} parameters, but given #{arity.size + 1}"
33
+ message = "load should take #{@block.arity} parameters, but given #{args.size + 1}"
34
34
  raise ArgumentError, message
35
35
  end
36
36
  result_of = @block.call(mapping_of.keys, *args)
37
- raise 'block shuold return Hash' unless result_of.is_a?(Hash)
37
+
38
+ raise 'block should return Hash' unless result_of.is_a?(Hash)
38
39
 
39
40
  result_of
40
41
  end
41
42
 
42
43
  def get_mapping(lazy_objs)
43
44
  mapping_of = {}
44
- targets = lazy_objs.map(&:target)
45
- targets.each do |target|
45
+ lazy_objs.each do |lazy_obj|
46
+ target = lazy_obj.target
46
47
  mapped_target = @is_mapping_proc ? @mapping.call(target) : target.public_send(@mapping)
47
48
  mapping_of[mapped_target] = [] unless mapping_of[mapped_target]
48
- mapping_of[mapped_target].push(target)
49
+ mapping_of[mapped_target].push(lazy_obj)
49
50
  end
50
51
  mapping_of
51
52
  end
52
53
 
53
- def set_result_to_lazy_objs(result_of, lazy_obj_of, mapping_of)
54
+ def set_result_to_lazy_objs(result_of, mapping_of)
54
55
  result_of.each do |mapped_target, value|
55
56
  next unless mapping_of[mapped_target]
56
57
 
57
- mapping_of[mapped_target].each do |target|
58
- lazy_obj_of[target]&.set(value)
58
+ mapping_of[mapped_target].each do |lazy_obj|
59
+ lazy_obj.set(value)
59
60
  end
60
61
  end
61
62
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BulkLoader
4
- VERSION = '1.0.0'
4
+ VERSION = '2.0.0'
5
5
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.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: 2019-09-11 00:00:00.000000000 Z
11
+ date: 2021-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0.49'
61
+ version: '0.74'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0.49'
68
+ version: '0.74'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".github/workflows/ruby.yml"
118
119
  - ".gitignore"
119
120
  - ".rspec"
120
121
  - ".rubocop.yml"
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  - !ruby/object:Gem::Version
153
154
  version: '0'
154
155
  requirements: []
155
- rubygems_version: 3.0.3
156
+ rubygems_version: 3.2.3
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: utility to avoid N+1 queries