pluck_global_id 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72cada23bff291533081e6643c2f54d9e6d4ba58
4
+ data.tar.gz: 115f480b4603b6ebd77d61b6c7c4985e36512def
5
+ SHA512:
6
+ metadata.gz: 59a307dd49229af8a95896efb5bb3cc060b2e104daa95679f6b25629c2455ebc59fc71ee8fc36e952bfe0f22e39f8b8e2ca96d486939fde0f4ef3f0168091ad4
7
+ data.tar.gz: d02395478789cca654c5aa721d912608238a48bd7fcebed0b1922af792e83a8f256a121a6684fd545defc7171e166132dfaca92f7e69eaeee43a0492cd1f60cd
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_record-pluck_globalid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Lukas Fittl
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # activerecord-pluck-globalid
2
+
3
+ Helper method to easily get GlobalIDs from an ActiveRecord scope without instantiating AR objects.
4
+
5
+ Useful for mass-creating ActiveJob jobs from a specific subset of your records.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'activerecord-pluck-globalid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install activerecord-pluck-globalid
22
+
23
+ ## Usage
24
+
25
+ Use ```pluck_globalid``` when you want to instantiate ActiveJob from a scope:
26
+
27
+ ```ruby
28
+ user_gids = User.where(active: true).pluck_globalid
29
+ user_gids.each do |user_gid|
30
+ UserWorker.perform_later(user_gid)
31
+ end
32
+ ```
33
+
34
+ This avoids costly AR objects which can consume significant memory in such cases.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/lfittl/activerecord-pluck-globalid/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,4 @@
1
+ require 'pluck_global_id/version'
2
+ require 'pluck_global_id/record'
3
+ require 'pluck_global_id/relation_methods'
4
+ require 'pluck_global_id/railtie' if defined?(Rails)
@@ -0,0 +1,13 @@
1
+ require 'rails/railtie'
2
+
3
+ module PluckGlobalID
4
+ class Railtie < Rails::Railtie
5
+ initializer 'pluck_global_id' do |app|
6
+ ActiveSupport.on_load :active_record do
7
+ ::ActiveRecord::Base.extend PluckGlobalID::RelationMethods
8
+ ::ActiveRecord::Relation.send(:include, PluckGlobalID::RelationMethods)
9
+ ::ActiveRecord::Associations::CollectionProxy.send(:include, PluckGlobalID::RelationMethods)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'globalid'
2
+
3
+ module PluckGlobalID
4
+ class Record
5
+ # Necessary for ActiveJob to detect this as a serialize-able object
6
+ include ::GlobalID::Identification
7
+
8
+ def initialize(model_name, model_id, options = {})
9
+ app = options.fetch :app, GlobalID.app
10
+ @global_id = GlobalID.new URI("gid://#{app}/#{model_name}/#{model_id}")
11
+ end
12
+
13
+ def to_global_id
14
+ @global_id
15
+ end
16
+
17
+ def to_signed_global_id
18
+ fail ArgumentError, 'Not implemented'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module PluckGlobalID
2
+ module RelationMethods
3
+ def pluck_global_id(options = {})
4
+ pluck(:id).map do |id|
5
+ PluckGlobalID::Record.new self.name, id
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PluckGlobalID
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pluck_global_id/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pluck_global_id"
8
+ spec.version = PluckGlobalID::VERSION
9
+ spec.authors = ["Lukas Fittl"]
10
+ spec.email = ["lukas@fittl.com"]
11
+ spec.summary = %q{Get GlobalIDs from an ActiveRecord scope without instantiating AR objects}
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/lfittl/pluck_global_id'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activerecord", ">= 4.0"
22
+ spec.add_runtime_dependency "railties", ">= 4.0"
23
+ spec.add_runtime_dependency "globalid", "~> 0.3.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3"
28
+ spec.add_development_dependency "rspec-rails"
29
+ spec.add_development_dependency "sqlite3"
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe PluckGlobalID do
4
+ it 'returns GlobalID records' do
5
+ sample = Sample.create! name: 'Something'
6
+
7
+ gid_record = Sample.pluck_global_id.first
8
+ expect(gid_record).to be_present
9
+ expect(gid_record).to be_a(PluckGlobalID::Record)
10
+ expect(gid_record.to_global_id).to eq sample.to_global_id
11
+ end
12
+
13
+ it 'respects where clauses on the scope' do
14
+ s1 = Sample.create! name: 'sample 1'
15
+ s2 = Sample.create! name: 'sample 2'
16
+ expect(Sample.where(name: 'sample 1').pluck_global_id.map(&:to_global_id)).to eq [s1.to_global_id]
17
+ end
18
+
19
+ it 'works with limit' do
20
+ 10.times { Sample.create! name: 'Something' }
21
+ expect(Sample.limit(1).pluck_global_id.size).to eq 1
22
+ end
23
+
24
+ it 'works with empty scopes' do
25
+ expect(Sample.pluck_global_id).to eq []
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require 'bundler/setup'
2
+ require 'rails/all'
3
+ require 'pluck_global_id'
4
+
5
+ class Sample < ActiveRecord::Base
6
+ include GlobalID::Identification
7
+ end
8
+
9
+ RSpec.configure do |config|
10
+ config.before(:suite) do
11
+ PluckGlobalID::Railtie.run_initializers
12
+ GlobalID.app = 'sample'
13
+
14
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
15
+
16
+ ActiveRecord::Migration.create_table :samples do |t|
17
+ t.string :name
18
+ end
19
+ end
20
+
21
+ config.before(:each) do
22
+ Sample.delete_all
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pluck_global_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Fittl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: globalid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: ''
126
+ email:
127
+ - lukas@fittl.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - Gemfile
134
+ - LICENSE.txt
135
+ - README.md
136
+ - Rakefile
137
+ - lib/pluck_global_id.rb
138
+ - lib/pluck_global_id/railtie.rb
139
+ - lib/pluck_global_id/record.rb
140
+ - lib/pluck_global_id/relation_methods.rb
141
+ - lib/pluck_global_id/version.rb
142
+ - pluck_global_id.gemspec
143
+ - spec/lib/pluck_globalid_spec.rb
144
+ - spec/spec_helper.rb
145
+ homepage: https://github.com/lfittl/pluck_global_id
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.4.5
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Get GlobalIDs from an ActiveRecord scope without instantiating AR objects
169
+ test_files:
170
+ - spec/lib/pluck_globalid_spec.rb
171
+ - spec/spec_helper.rb