mongoid-bulk-import 0.1.0 → 0.2.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: 937994d0603649456a9366e8a8c5945dd1b0a82985e15e764cf6de25f5a0ec31
4
- data.tar.gz: e6701a448466fc4c4674a07c45bbea58e5944a1433d823f4433c29b7051570c8
3
+ metadata.gz: 51da653af49acd1c5864ac9486c2a5433002253cc76f5e82c916db2d3ba37cf0
4
+ data.tar.gz: 6e91982865e84d44fb831d93fc2b41fc6d5fb32fddfe86ef2476da59435708b5
5
5
  SHA512:
6
- metadata.gz: 43ed6065bcb0d625e410e70f9d121a09fc01d00b6b682842c3076abd9534637e8d56c38a6bb68d58d45fb4c730a6ae166362e3638c392fb8d5a5f39da3639855
7
- data.tar.gz: 5e64d4c5d1e6b4b2e52672194608689b6319808ad3f734c466c6fa533184bf6a74480f8b79b9a7c19ba7df29e0c87898502d1a4d1c4311e5d11adea9433e78fa
6
+ metadata.gz: 9c5b39207d023542927f23ad76ed5a7e5288246bc8035d471d0ab35d0d7d95e99b78a71ac86d029cffe4d5197559b21a6050e8d7769a924f713c5ba5f9a376d9
7
+ data.tar.gz: d9ff9543faccd27263369d175a49ee8f294c2e40de4968ddbf6961d4260cc2d08400f1fbd138c803552b5b40aaa4e2105c27a82e230bf5207f8ec486756763cc
data/.travis.yml CHANGED
@@ -5,3 +5,4 @@ cache: bundler
5
5
  rvm:
6
6
  - 2.6.5
7
7
  before_install: gem install bundler -v 1.17.3
8
+ services: mongodb
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-bulk-import (0.1.0)
4
+ mongoid-bulk-import (0.2.0)
5
5
  mongoid (>= 7.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Mongoid::Bulk::Import
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mongoid/bulk/import`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ This gem adds an `bulk_insert` method to mongoid classes for bulk inserting data with validations support.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,13 +20,29 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Sample a single document
24
+
25
+ ```ruby
26
26
 
27
- ## Development
27
+ class MyModel
28
+ include Mongoid::Document
29
+ field :name
30
+ end
31
+
32
+ MyModel.bulk_insert({ name: "Jack" })
33
+ ```
28
34
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+ ### Sample multiple documents
30
36
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+ ```ruby
38
+
39
+ class MyModel
40
+ include Mongoid::Document
41
+ field :name
42
+ end
43
+
44
+ MyModel.bulk_insert([{ name: "Jack" }, { name: "Matt" }])
45
+ ```
32
46
 
33
47
  ## Contributing
34
48
 
@@ -1,7 +1,7 @@
1
1
  module Mongoid
2
2
  module Bulk
3
3
  module Import
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -1,10 +1,31 @@
1
1
  require "mongoid/bulk/import/version"
2
+ require 'mongoid'
2
3
 
3
4
  module Mongoid
4
5
  module Bulk
5
6
  module Import
6
- class Error < StandardError; end
7
- # Your code goes here...
7
+ extend ActiveSupport::Concern
8
+ Result = Struct.new(:failed_instances, :num_inserts, :ids)
9
+
10
+ def bulk_insert(attributes = nil, options = {}, &block)
11
+ _creating do
12
+ if attributes.is_a?(::Array)
13
+ documents = attributes.map { |attrs| bulk_insert_obj(attrs, options, &block) }
14
+ else
15
+ documents = [bulk_insert_obj(attributes, options, &block)]
16
+ end
17
+ valid_documents, invalid_documents = documents.partition { |doc| doc.errors.none? }
18
+ response = collection.insert_many(valid_documents.map(&:as_document)) unless valid_documents.blank?
19
+ Result.new(invalid_documents, response&.inserted_count || 0, response&.inserted_ids || [])
20
+ end
21
+ end
22
+
23
+ def bulk_insert_obj(attributes = nil, options = {}, &block)
24
+ doc = new(attributes, &block)
25
+ doc.valid? if (options[:validate].nil? ? true : options[:validate])
26
+ doc
27
+ end
8
28
  end
9
29
  end
10
30
  end
31
+ Mongoid::Document::ClassMethods.send(:include, Mongoid::Bulk::Import)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-bulk-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arun Bharati
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-23 00:00:00.000000000 Z
11
+ date: 2021-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -86,7 +86,6 @@ files:
86
86
  - bin/setup
87
87
  - lib/mongoid/bulk/import.rb
88
88
  - lib/mongoid/bulk/import/version.rb
89
- - lib/mongoid/persistable/creatable.rb
90
89
  - mongoid-bulk-import.gemspec
91
90
  homepage: https://github.com/Arunbharati/mongoid-bulk-import
92
91
  licenses:
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mongoid
4
- module Persistable
5
-
6
- # Defines behavior for persistence operations that create new documents.
7
- module Creatable
8
- extend ActiveSupport::Concern
9
- Result = Struct.new(:failed_instances, :num_inserts, :ids)
10
-
11
- module ClassMethods
12
-
13
- def bulk_insert(attributes = nil, options = {}, &block)
14
- _creating do
15
- if attributes.is_a?(::Array)
16
- documents = attributes.map { |attrs| bulk_insert_obj(attrs, options, &block) }
17
- else
18
- documents = [bulk_insert_obj(attributes, options, &block)]
19
- end
20
- valid_documents, invalid_documents = documents.partition { |doc| doc.errors.none? }
21
- response = collection.insert_many(valid_documents.map(&:as_document)) unless valid_documents.blank?
22
- Result.new(invalid_documents, response&.inserted_count || 0, response&.inserted_ids || [])
23
- end
24
- end
25
-
26
- def bulk_insert_obj(attributes = nil, options = {}, &block)
27
- doc = new(attributes, &block)
28
- doc.valid? if (options[:validate].nil? ? true : options[:validate])
29
- doc
30
- end
31
- end
32
- end
33
- end
34
- end