autoguid 1.0
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.
- checksums.yaml +15 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +16 -0
- data/Rakefile +30 -0
- data/lib/autoguid.rb +30 -0
- data/lib/autoguid/version.rb +3 -0
- data/lib/initializer.rb +37 -0
- data/lib/migrator.rb +45 -0
- data/lib/railtie.rb +10 -0
- data/lib/tasks/autoguid_tasks.rake +32 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NDRlNzVkZjBkZGQ2ODUzNDlhMWVkZjM0ZDk3NzFkZTg1YWU1NzY3ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
Y2E3MDA0NzEzYzM3M2ZjMGI0NThiMWFjMmViZTc4ZGU4OGI4ZDIwMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YmZjODdkNzU4MWM3NjA0YTMxOTk5ZDBiMDdjOGU5ZjVjMDY1MDQ4NWYxYTcy
|
10
|
+
NjgwNjRiNGNjNTk0OTU3NmRhMDg0MjExM2Q2YmE3ZmU4NDdkZDQ0M2U3NTU5
|
11
|
+
ZjVlMzA0MjBkOTdjOWExYTk1YTRiNTRhYWNhNDgxNGFmYmFhYmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
M2YwOTVkMDAzYjc1Mjc2ZTM3NzNkYjRhMWYzYWNhNzczMDA3OTdlYTI3NTVk
|
14
|
+
M2RhYWM2NTE0MzhmMDlhNTk5OWFlMDlkOWQ1ZWY1YjQxNTQ3Y2JmZGNlZDI2
|
15
|
+
OTU0MmNjMjJiZTg4MDYxYjRjZjViMTk0MWJkMGM2MDQ5MzA5MzE=
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
= Autoguid
|
2
|
+
|
3
|
+
This project rocks and uses MIT-LICENSE.
|
4
|
+
|
5
|
+
Autoguid lets you trivially add human readable uuids to all
|
6
|
+
your models, a whitelisted set of models, or all but a blacklisted set.
|
7
|
+
Indices are automatically created based on a configuration option.
|
8
|
+
There's also a rake task that will backfill these uuids into resources that
|
9
|
+
have already been created.
|
10
|
+
To get started, include the gem file, run `bundle install`, then run
|
11
|
+
`rake autoguid:install`. From there, edit the config/initializers/autoguid.rb
|
12
|
+
file to specify your configuration. Next, migrate your tables with
|
13
|
+
`rake autoguid:migrate:up` and `rake autoguid:migrate:backfill` as required.
|
14
|
+
`rake autoguid:migrate:drop_all` will drop all autoguid generated columns and
|
15
|
+
the data in them. You can always change the config/initializers/autoguid.rb file
|
16
|
+
and rerun `rake autoguid:migrate:up` to add autoguid to new models.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'rdoc/task'
|
9
|
+
|
10
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
11
|
+
rdoc.rdoc_dir = 'rdoc'
|
12
|
+
rdoc.title = 'Autoguid'
|
13
|
+
rdoc.options << '--line-numbers'
|
14
|
+
rdoc.rdoc_files.include('README.rdoc')
|
15
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
Bundler::GemHelper.install_tasks
|
22
|
+
|
23
|
+
# Default directory to look in is /specs
|
24
|
+
RSpec::Core::RakeTask.new(:test) do |task|
|
25
|
+
#task.rspec_opts = ['--color', '--format', 'nested']
|
26
|
+
# task.rspec_opts = '--tag'
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
task default: :test
|
data/lib/autoguid.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'railtie' if defined?(Rails)
|
2
|
+
|
3
|
+
module Autoguid
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_create do
|
8
|
+
save_autoguid( generate_autoguid )
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def save_autoguid(guid)
|
13
|
+
self.guid = guid
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_autoguid
|
17
|
+
uuid = SecureRandom.uuid
|
18
|
+
name = self.is_a?(Module) ? name : self.class.name
|
19
|
+
return name + '-' + uuid
|
20
|
+
end
|
21
|
+
|
22
|
+
def backfill(model)
|
23
|
+
puts "Backfilling " + model
|
24
|
+
model.all.each do |row|
|
25
|
+
puts "Found a row"
|
26
|
+
row.guid = generate_autoguid
|
27
|
+
row.save
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/initializer.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'migrator'
|
2
|
+
require 'autoguid'
|
3
|
+
|
4
|
+
class Initializer
|
5
|
+
|
6
|
+
def config
|
7
|
+
@migrator = Migrator.new
|
8
|
+
@migrator.config = Hash.new
|
9
|
+
|
10
|
+
# To configure autoguid you need to edit this file. Uncomment ONE
|
11
|
+
# of the following three lines based on your needs.
|
12
|
+
|
13
|
+
# Uncomment this line to add autoguids to all of your models
|
14
|
+
# @migrator.config[:all] = true
|
15
|
+
|
16
|
+
# Uncomment this line to whitelist models that you want autoguids on
|
17
|
+
# They will be added only to the resources listed
|
18
|
+
# @migrator.config[:whitelist] = ['User','Office']
|
19
|
+
|
20
|
+
# Uncomment this line to blacklist the models you dont want autoguids on
|
21
|
+
# They will be added to all other models
|
22
|
+
# @migrator.config[:blacklist] = ['User']
|
23
|
+
|
24
|
+
# Uncomment this line to add indices to your autoguid generated columns
|
25
|
+
# This will speed up lookups but occupy some database server memory
|
26
|
+
# @migrator.config[:indices] = true
|
27
|
+
end
|
28
|
+
|
29
|
+
def up
|
30
|
+
@migrator.up
|
31
|
+
end
|
32
|
+
|
33
|
+
def drop_all
|
34
|
+
@migrator.down
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/migrator.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Migrator < ActiveRecord::Migration
|
2
|
+
|
3
|
+
attr_accessor :config
|
4
|
+
|
5
|
+
def up
|
6
|
+
puts config
|
7
|
+
Rails.application.eager_load!
|
8
|
+
models = Array.new
|
9
|
+
whitelist = Array.new
|
10
|
+
if ( @config[:all] )
|
11
|
+
whitelist = ActiveRecord::Base.direct_descendants if @config[:all]
|
12
|
+
elsif ( @config[:whitelist] )
|
13
|
+
puts "Got into Whitelist"
|
14
|
+
models = ActiveRecord::Base.direct_descendants
|
15
|
+
models.each do |m|
|
16
|
+
puts "Checking for model " + m.name + " in whitelist "
|
17
|
+
whitelist.push(m) if @config[:whitelist].include?(m.name)
|
18
|
+
end
|
19
|
+
elsif ( @config[:blacklist] )
|
20
|
+
puts "Processing by blacklist"
|
21
|
+
whitelist = ActiveRecord::Base.direct_descendants
|
22
|
+
whitelist.each do |m|
|
23
|
+
puts "Checking for model " + m.name + " in blacklist "
|
24
|
+
whitelist.delete(m) if @config[:blacklist].include?(m.name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
puts whitelist
|
28
|
+
whitelist.each do |model|
|
29
|
+
puts "Processing " + model.name
|
30
|
+
model.reset_column_information
|
31
|
+
add_column(model, :guid, :string, :unique => true) unless column_exists?(model, :guid)
|
32
|
+
add_index(model, :guid) if @config[:indices] && !index_exists?(model, :guid)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def down
|
37
|
+
Rails.application.eager_load!
|
38
|
+
ActiveRecord::Base.direct_descendants.each do |model|
|
39
|
+
model.reset_column_information
|
40
|
+
remove_index(model, :guid) if index_exists?(model, :guid)
|
41
|
+
remove_column(model, :guid) if column_exists?(model, :guid)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/lib/railtie.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
namespace :autoguid do
|
2
|
+
task :install do
|
3
|
+
path = Rails.root.to_s + "/config/initializers/autoguid.rb"
|
4
|
+
template_path = File.expand_path('../../initializer.rb',__FILE__)
|
5
|
+
template = File.open(template_path)
|
6
|
+
File.open(path, "w") do |f|
|
7
|
+
IO.copy_stream(template,f)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
namespace :migrate do
|
11
|
+
task :up => :environment do
|
12
|
+
init = Initializer.new
|
13
|
+
init.config
|
14
|
+
init.up
|
15
|
+
end
|
16
|
+
task :drop_all => :environment do
|
17
|
+
init = Initializer.new
|
18
|
+
init.config
|
19
|
+
init.drop_all
|
20
|
+
end
|
21
|
+
task :backfill => :environment do
|
22
|
+
require 'autoguid'
|
23
|
+
Rails.application.eager_load!
|
24
|
+
ActiveRecord::Base.direct_descendants.each do |model|
|
25
|
+
puts model
|
26
|
+
model.reset_column_information
|
27
|
+
Autoguid.backfill(model) if model.columns_hash['guid']
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: autoguid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Kinnaird
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.4
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 4.1.4
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.1.4
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 4.1.4
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ~>
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 10.1.0
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 10.1.0
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ~>
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 10.1.0
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 10.1.0
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 3.0.0
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.0.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 3.0.0
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.0.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: sqlite3
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ~>
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 1.3.9
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.3.9
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.9
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 1.3.9
|
93
|
+
description: ! "Autoguid lets you trivially add human readable uuids to all\n your
|
94
|
+
models, a whitelisted set of models, or a blacklisted set.\n Indices are automatically
|
95
|
+
created based on a configuration option.\n There's also a rake task that will
|
96
|
+
backfill these uuids into resources that\n have already been created.\n To get
|
97
|
+
started, include the gem file, run `bundle install`, then run\n `rake autoguid:install`.
|
98
|
+
From there, edit the config/initializers/autoguid.rb\n file to specifcy your configuration.
|
99
|
+
Next, migrate your tables with\n `rake autoguid:migrate:up` and `rake autoguid:migrate:backfill`
|
100
|
+
as required.\n `rake autoguid:migrate:drop_all` will drop all autoguid generated
|
101
|
+
columns and\n the data in them. You can always change the config/initializers/autoguid.rb
|
102
|
+
file\n and rerun `rake autoguid:migrate:up` to add autoguid to new models."
|
103
|
+
email:
|
104
|
+
- peter@hacktivism.cc
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files: []
|
108
|
+
files:
|
109
|
+
- MIT-LICENSE
|
110
|
+
- README.rdoc
|
111
|
+
- Rakefile
|
112
|
+
- lib/autoguid.rb
|
113
|
+
- lib/autoguid/version.rb
|
114
|
+
- lib/initializer.rb
|
115
|
+
- lib/migrator.rb
|
116
|
+
- lib/railtie.rb
|
117
|
+
- lib/tasks/autoguid_tasks.rake
|
118
|
+
homepage: http://hacktivism.cc
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ! '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Autoguid adds human readable uuids to your models
|
142
|
+
test_files: []
|