en_masse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dba126de691ed6b3dba03bd09be4742c22bfa473
4
+ data.tar.gz: f30993407f852520cd7cbe85faa8bbd8267ed974
5
+ SHA512:
6
+ metadata.gz: 989a21b476440a983b0f522ab62f88522d3c0b3d2a2b28aa28cff91a2b555e7983354e5512dec7bddd3eb4c5be1ab44c70ab38b189dd04af45304158c700654a
7
+ data.tar.gz: 0b6308ed04ec67ae59a562fc616ae7b266c318d26514f220ee2e56d5002c9bcc18049f35a77642f39b561542e148c755f689315f593363a8914f19e79f19c03a
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ -rspec_helper
2
+ --format documentation
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 de Praktijk Index
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # EnMasse
2
+
3
+ Operations on arrays of active records.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ valid, invalid = EnMasse.validate(records)
9
+ EnMasse.insert(valid)
10
+ invalid.each do |record|
11
+ Rails.logger.error { "Invalid record in mass operation:\n#{record.to_yaml}" }
12
+ end
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem "en_masse"
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install en_masse
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/en_masse/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am "Add some feature"`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task "db:create" do
4
+ sh "createdb en_masse_test"
5
+ end
data/en_masse.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ require_relative "lib/en_masse/version"
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "en_masse"
6
+ spec.version = EnMasse::VERSION
7
+ spec.authors = ["Artem Baguinski"]
8
+ spec.email = ["abaguinski@depraktijkindex.nl"]
9
+
10
+ spec.summary = %q{Operations on arrays of active records.}
11
+ spec.homepage = "https://github.com/praktijkindex/en_masse"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = "~> 2.1"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "yard", "~> 0.8.7"
25
+ spec.add_development_dependency "redcarpet", "~> 3.2"
26
+
27
+ spec.add_runtime_dependency "activesupport", "~> 4.0"
28
+ spec.add_runtime_dependency "activerecord", "~> 4.0"
29
+ end
@@ -0,0 +1,3 @@
1
+ require "active_support/dependencies"
2
+ ActiveSupport::Dependencies.autoload_paths << File.expand_path("../..", __FILE__)
3
+
@@ -0,0 +1,30 @@
1
+ class EnMasse::Inserts
2
+ using EnMasse::Refinements
3
+
4
+ attr_reader :collection
5
+
6
+ def initialize collection
7
+ @collection = collection
8
+ @collection += collect_new_dependents
9
+ end
10
+
11
+ def call
12
+ ActiveRecord::Base.connection.transaction do
13
+ by_model.each do |model, records|
14
+ model.insert_collection(records)
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def by_model
22
+ @by_model ||= collection.group_by(&:class)
23
+ end
24
+
25
+ def collect_new_dependents
26
+ collection.flat_map{|record| record.new_dependents}.uniq
27
+ end
28
+
29
+ end
30
+
@@ -0,0 +1,87 @@
1
+ module EnMasse::Refinements
2
+
3
+ refine ::ActiveRecord::Base.singleton_class do
4
+
5
+ def insert_collection collection
6
+ ids = allocate_ids(collection.count)
7
+ collection.zip(ids).each do |record, id|
8
+ record.id = id
9
+ record.update_foreign_keys
10
+ end
11
+ values = collection.map{|record|
12
+ "(#{ record.values_for_insert(column_names).join(",") })"
13
+ }.join(",")
14
+ ActiveRecord::Base.connection.execute %Q[
15
+ INSERT INTO #{table_name} (#{column_names.join(",")})
16
+ VALUES #{values};
17
+ ]
18
+ end
19
+
20
+ def reflect_on_dependent_associations
21
+ [:has_one, :has_many].flat_map { |association_kind|
22
+ reflect_on_all_associations(association_kind).reject(&:through_reflection)
23
+ }
24
+ end
25
+
26
+ private
27
+
28
+ def primary_key_sequence_update_lock
29
+ 1
30
+ end
31
+
32
+ def allocate_ids amount
33
+ next_value = connection.execute(%Q[
34
+ SELECT pg_advisory_lock(#{primary_key_sequence_update_lock}),
35
+ setval('#{sequence_name}', greatest(
36
+ (select max(id) from #{table_name}),
37
+ nextval('#{sequence_name}')-1)
38
+ + #{amount}),
39
+ currval('#{sequence_name}'),
40
+ pg_advisory_unlock(#{primary_key_sequence_update_lock});
41
+ ]).first["currval"].to_i
42
+ start_value = next_value - amount + 1
43
+ start_value..next_value
44
+ end
45
+
46
+ end
47
+
48
+ refine ::ActiveRecord::Base do
49
+
50
+ def update_foreign_keys
51
+ id = self[self.class.primary_key]
52
+ self.class.reflect_on_dependent_associations.each do |association|
53
+ foreign_key = association.foreign_key
54
+ dependents = [send(association.name)].flatten.compact
55
+ dependents.each do |dependent|
56
+ dependent[foreign_key] = id
57
+ end
58
+ end
59
+ end
60
+
61
+ def values_for_insert column_names
62
+ column_names.map{|column_name|
63
+ column = column_for_attribute(column_name)
64
+ if %w(created_at updated_at).include? column_name
65
+ "now()"
66
+ else
67
+ value = self.send(column_name)
68
+ ActiveRecord::Base.connection.quote(value, column)
69
+ end
70
+ }
71
+ end
72
+
73
+ def new_dependents
74
+ dependents.select(&:new_record?)
75
+ end
76
+
77
+ def dependents
78
+ direct_dependents = self.class
79
+ .reflect_on_dependent_associations
80
+ .flat_map{|association| self.send(association.name)}
81
+ .compact
82
+ .uniq
83
+ direct_dependents + direct_dependents.flat_map{|dependent| dependent.dependents}
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module EnMasse
2
+ VERSION = "0.0.1"
3
+ end
data/lib/en_masse.rb ADDED
@@ -0,0 +1,11 @@
1
+ require_relative "en_masse/autoload"
2
+
3
+ module EnMasse
4
+ def self.insert collection
5
+ Inserts.new(collection).call
6
+ end
7
+
8
+ def self.validate collection
9
+ collection.partition(&:valid?)
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: en_masse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Artem Baguinski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.7
69
+ - !ruby/object:Gem::Dependency
70
+ name: redcarpet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '4.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '4.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ description:
112
+ email:
113
+ - abaguinski@depraktijkindex.nl
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - en_masse.gemspec
126
+ - lib/en_masse.rb
127
+ - lib/en_masse/autoload.rb
128
+ - lib/en_masse/inserts.rb
129
+ - lib/en_masse/refinements.rb
130
+ - lib/en_masse/version.rb
131
+ homepage: https://github.com/praktijkindex/en_masse
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '2.1'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Operations on arrays of active records.
155
+ test_files: []
156
+ has_rdoc: