unique_identifier 0.0.1 → 0.0.2
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 +6 -14
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +12 -0
- data/lib/unique_identifier/version.rb +3 -0
- data/lib/unique_identifier.rb +57 -0
- data/spec/spec_helper.rb +52 -0
- data/spec/unique_identifier_spec.rb +15 -0
- data/unique_identifier.gemspec +28 -0
- metadata +29 -17
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
NmFiZWUxNzI0MTBmYTIwOTEwNDBmNTE1MWRhNjE1NWQ1YmEyYWNlNDVkZjNh
|
10
|
-
YTE5ODZlNzliYzg0NTE2YWI4NjUzNWI3MDNiOGU5NzRmZjY1MTYxZGI0MTU4
|
11
|
-
ZTA2NTk3ODE2YjMyY2Y1MmRjODgzODg2NjE4ZjdiOGVjNTEwNGI=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
MzEwOTYyZWI0Nzg4NWYyMzc0ODQ4NTBlYzMwZGQwYTJhMGMyMzg3MzczZGRl
|
14
|
-
NmRjOTkwNDUzNTI5NjU1OTM3YjZhMTFkY2E5ZGY1OGE2YWE5MmNiOGJkZDQ5
|
15
|
-
MjI0OGY3YjY0OGIwYWEzY2VkYjgyNWI3NGM5MzYwMjU2ZGQxMGI=
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ef2436a63aa5338ba9990158576c833975acb805
|
4
|
+
data.tar.gz: 6569f75cf20811cbbcc35f35facb961784edc7b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 104ca7b13cabdc8c20b017558b6a4d7ac289a2a5f46f859d8a479db320cf12252aad99a03a3b121a20ca4d2c688ce9319660278effd4645f7a7e45d5fa9565f8
|
7
|
+
data.tar.gz: 935896bf3463c6917e5c82b7648af904ed1fb283f0a9af5ca18e49e298d39a52dc9c3bef7c5d87559430084fcb07ff4fc1340965322b1859f35f95d7f52ef7e3
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Justin Grubbs
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Unique Identifier
|
2
|
+
|
3
|
+
make unique identifier fields quick and simple
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'unique_identifier'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install unique_identifier
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
class SomeModel < ActiveRecord::Base
|
24
|
+
|
25
|
+
# name of column to store unique identifier
|
26
|
+
# |
|
27
|
+
# -------
|
28
|
+
unique_id :number, -> { Array.new(9) { rand(9) }.join }
|
29
|
+
# ------------------------------------
|
30
|
+
# |
|
31
|
+
# proc to be run before_create to generate unique identifier
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
instance = SomeModel.create
|
36
|
+
instance.number # => "324516542"
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
## How it works
|
41
|
+
|
42
|
+
The above example is equivalent to this:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
|
46
|
+
class SomeModel < ActiveRecord::Base
|
47
|
+
|
48
|
+
before_create :generate_unique_id
|
49
|
+
|
50
|
+
def generate_unique_id
|
51
|
+
self.number = loop do
|
52
|
+
random = Array.new(9) { rand(9) }.join
|
53
|
+
break random unless self.class.exists?(number: random)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:spec)
|
11
|
+
|
12
|
+
task :default => :spec
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "unique_identifier/version"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
module UniqueIdentifier
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
|
8
|
+
mattr_accessor :field, :block, :klass
|
9
|
+
|
10
|
+
def unique_id(field, block)
|
11
|
+
@klass = self.name.constantize
|
12
|
+
@klass.const_set('BLOCK', block)
|
13
|
+
@klass.const_set('FIELD', field)
|
14
|
+
@klass.set_callback(:create, :before, :generate_unique_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
def generate_unique_id
|
21
|
+
return if self.send(self.class::FIELD)
|
22
|
+
identifier = loop do
|
23
|
+
random = self.class::BLOCK.call
|
24
|
+
break random unless self.class.exists?(self.class::FIELD => random)
|
25
|
+
end
|
26
|
+
self.send "#{self.class::FIELD}=", identifier
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
module Glue
|
32
|
+
def self.included(base)
|
33
|
+
base.extend(ClassMethods)
|
34
|
+
base.send :include, InstanceMethods
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if defined? Rails::Railtie
|
39
|
+
|
40
|
+
class Railtie < Rails::Railtie
|
41
|
+
initializer 'unique_identifier.insert_into_active_record' do
|
42
|
+
ActiveSupport.on_load :active_record do
|
43
|
+
UniqueIdentifier::Railtie.insert
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Railtie
|
50
|
+
|
51
|
+
def self.insert
|
52
|
+
ActiveRecord::Base.send(:include, UniqueIdentifier::Glue)
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'rails'
|
5
|
+
require 'active_record'
|
6
|
+
require 'unique_identifier'
|
7
|
+
|
8
|
+
UniqueIdentifier::Railtie.insert
|
9
|
+
|
10
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
|
14
|
+
config.before(:all) do
|
15
|
+
m = ActiveRecord::Migration.new
|
16
|
+
m.verbose = false
|
17
|
+
m.create_table :dummy_models do |t|
|
18
|
+
t.string :number
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
config.after(:all) do
|
23
|
+
m = ActiveRecord::Migration.new
|
24
|
+
m.verbose = false
|
25
|
+
m.drop_table :dummy_models
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def build_class(name, options = {})
|
32
|
+
# setup class and include delayed_cron
|
33
|
+
|
34
|
+
class_name = "DummyModel"
|
35
|
+
|
36
|
+
ActiveRecord::Base.send(:include, UniqueIdentifier::Glue)
|
37
|
+
Object.send(:remove_const, class_name) rescue nil
|
38
|
+
|
39
|
+
# Set class as a constant
|
40
|
+
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
41
|
+
|
42
|
+
klass.const_set("MODE", "test")
|
43
|
+
|
44
|
+
klass.class_eval do
|
45
|
+
include UniqueIdentifier::Glue
|
46
|
+
|
47
|
+
unique_identifier name, options
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
klass
|
52
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UniqueIdentifier do
|
4
|
+
|
5
|
+
describe "unique_identifier" do
|
6
|
+
|
7
|
+
it "should" do
|
8
|
+
DummyModel = build_class( :number, Proc.new { "#{DummyModel::MODE}#{Array.new(9) { rand(9) }.join}" } )
|
9
|
+
model1 = DummyModel.create
|
10
|
+
model1.number.should_not be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'unique_identifier/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "unique_identifier"
|
8
|
+
spec.version = UniqueIdentifier::VERSION
|
9
|
+
spec.authors = ["Justin Grubbs"]
|
10
|
+
spec.email = ["justin@jgrubbs.net"]
|
11
|
+
spec.description = %q{before_create helper to create unique idenfication fields}
|
12
|
+
spec.summary = %q{before_create helper to create unique idenfication fields}
|
13
|
+
spec.homepage = "https://github.com/jGRUBBS/unique_identifier"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rails"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "debugger"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unique_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Grubbs
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,70 +28,70 @@ dependencies:
|
|
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: '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: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
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
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: debugger
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: sqlite3
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
description: before_create helper to create unique idenfication fields
|
@@ -100,7 +100,17 @@ email:
|
|
100
100
|
executables: []
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
|
-
files:
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- Gemfile
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- lib/unique_identifier.rb
|
110
|
+
- lib/unique_identifier/version.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/unique_identifier_spec.rb
|
113
|
+
- unique_identifier.gemspec
|
104
114
|
homepage: https://github.com/jGRUBBS/unique_identifier
|
105
115
|
licenses:
|
106
116
|
- MIT
|
@@ -111,18 +121,20 @@ require_paths:
|
|
111
121
|
- lib
|
112
122
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
123
|
requirements:
|
114
|
-
- -
|
124
|
+
- - '>='
|
115
125
|
- !ruby/object:Gem::Version
|
116
126
|
version: '0'
|
117
127
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
128
|
requirements:
|
119
|
-
- -
|
129
|
+
- - '>='
|
120
130
|
- !ruby/object:Gem::Version
|
121
131
|
version: '0'
|
122
132
|
requirements: []
|
123
133
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.
|
134
|
+
rubygems_version: 2.2.2
|
125
135
|
signing_key:
|
126
136
|
specification_version: 4
|
127
137
|
summary: before_create helper to create unique idenfication fields
|
128
|
-
test_files:
|
138
|
+
test_files:
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
- spec/unique_identifier_spec.rb
|