public_uid 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +8 -0
- data/Gemfile +2 -0
- data/README.md +5 -0
- data/lib/orm/active_record.rb +2 -0
- data/lib/public_uid/model.rb +24 -0
- data/lib/public_uid/version.rb +1 -1
- data/lib/public_uid.rb +3 -24
- data/public_uid.gemspec +2 -2
- data/test/models/custom_public_uid_column_test.rb +17 -21
- data/test/models/model_with_custom_generator_test.rb +27 -34
- data/test/models/model_with_generator_defaults_test.rb +19 -22
- data/test/support/orm/active_record.rb +29 -0
- data/test/test_helper.rb +24 -4
- metadata +21 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e60e9adac30a4438f4749528b067dc3574adf28e
|
4
|
+
data.tar.gz: fb17f29505924fad7949a2b173b6ff15b76910e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caca27924c4a11a42333a5e0da57c41441056988d35e58a2e4c49314619b920f95020a7e5c3c01336f7e8d29e5700224975cc4fec316db076b6fd594f0ec376a
|
7
|
+
data.tar.gz: 05e33a8dabd884f4b369591d249f1073969f173293f33be3a8997512bcdc68ef72699837704be8ed8978d0dd954c7d929486e64b0a5398aea1ea74f87e6e4e9b
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# PublicUid
|
2
2
|
|
3
|
+
|
4
|
+
[![Build Status](https://api.travis-ci.org/equivalent/lpgrid.png?branch=master)](http://travis-ci.org/equivalent/public_uid)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/equivalent/public_uid.png)](https://codeclimate.com/github/equivalent/public_uid)
|
6
|
+
|
7
|
+
|
3
8
|
Generates random string (or random number) to represent public unique
|
4
9
|
record identifier.
|
5
10
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module PublicUid
|
2
|
+
module Model
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
def generate_uid
|
8
|
+
pub_uid = SetPublicUid.new(record: self, column: self.class.public_uid_column)
|
9
|
+
pub_uid.generate self.class.public_uid_generator
|
10
|
+
pub_uid.set
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
attr_reader :public_uid_column
|
15
|
+
attr_reader :public_uid_generator
|
16
|
+
|
17
|
+
def generate_public_uid(options={})
|
18
|
+
@public_uid_column = options[:column] || :public_uid
|
19
|
+
@public_uid_generator = options[:generator] || Generators::NumberRandom.new
|
20
|
+
before_create :generate_uid, unless: @public_uid_column
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/public_uid/version.rb
CHANGED
data/lib/public_uid.rb
CHANGED
@@ -1,29 +1,8 @@
|
|
1
|
+
require 'orm_adapter'
|
1
2
|
require "public_uid/version"
|
2
3
|
require "public_uid/set_public_uid"
|
4
|
+
require "public_uid/model"
|
3
5
|
require "public_uid/generators/number_random"
|
4
6
|
require "public_uid/generators/range_string"
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
def self.included(base)
|
9
|
-
base.extend(ClassMethods)
|
10
|
-
end
|
11
|
-
|
12
|
-
def generate_uid
|
13
|
-
pub_uid = SetPublicUid.new(record: self, column: self.class.public_uid_column)
|
14
|
-
pub_uid.generate self.class.public_uid_generator
|
15
|
-
pub_uid.set
|
16
|
-
end
|
17
|
-
|
18
|
-
module ClassMethods
|
19
|
-
attr_reader :public_uid_column
|
20
|
-
attr_reader :public_uid_generator
|
21
|
-
|
22
|
-
def generate_public_uid(options={})
|
23
|
-
@public_uid_column = options[:column] || :public_uid
|
24
|
-
@public_uid_generator = options[:generator] || Generators::NumberRandom.new
|
25
|
-
before_create :generate_uid, unless: @public_uid_column
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
8
|
+
require 'orm/active_record'
|
data/public_uid.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["equivalent@eq8.eu"]
|
11
11
|
spec.description = %q{Automatic generates public unique identifier for model}
|
12
12
|
spec.summary = %q{Automatic generates public UID column}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/equivalent/public_uid"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -18,9 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_dependency "orm_adapter", "~> 0.5"
|
21
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
23
|
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "activerecord", "~> 3.2"
|
24
24
|
spec.add_development_dependency "minitest", "~> 5"
|
25
25
|
spec.add_development_dependency "sqlite3"
|
26
26
|
end
|
@@ -1,29 +1,25 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
generate_public_uid column: :custom_uid
|
7
|
-
end
|
8
|
-
|
9
|
-
describe 'CustomPublicUidColumnModel' do
|
10
|
-
let(:user){CustomPublicUidColumnModel.new}
|
3
|
+
TestConf.orm_modules.each do |orm_module|
|
4
|
+
describe orm_module.description do
|
5
|
+
let(:user) { "#{orm_module}::CustomPublicUidColumnModel".constantize.new }
|
11
6
|
|
12
|
-
|
13
|
-
|
7
|
+
describe '#custom_uid' do
|
8
|
+
subject{ user.custom_uid }
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'after save' do
|
20
|
-
before do
|
21
|
-
user.save
|
22
|
-
user.reload
|
10
|
+
context 'in new record' do
|
11
|
+
it{ subject.must_be_nil }
|
23
12
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
13
|
+
|
14
|
+
context 'after save' do
|
15
|
+
before do
|
16
|
+
user.save
|
17
|
+
user.reload
|
18
|
+
end
|
19
|
+
it{ subject.wont_be_nil }
|
20
|
+
it 'by default should generate 7 digit number string' do
|
21
|
+
subject.to_i.to_s.length.must_equal(7)
|
22
|
+
end
|
27
23
|
end
|
28
24
|
end
|
29
25
|
end
|
@@ -1,40 +1,33 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
subject.must_be_kind_of String
|
31
|
-
subject.length.must_equal(10)
|
3
|
+
TestConf.orm_modules.each do |orm_module|
|
4
|
+
describe orm_module.description do
|
5
|
+
describe 'ModelWithCustomGererator' do
|
6
|
+
let(:user) { "#{orm_module}::ModelWithCustomGererator".constantize.new }
|
7
|
+
|
8
|
+
describe '#public_uid' do
|
9
|
+
subject{ user.public_uid }
|
10
|
+
|
11
|
+
context 'in new record' do
|
12
|
+
it{ subject.must_be_nil }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'after save' do
|
16
|
+
before do
|
17
|
+
user.save
|
18
|
+
user.reload
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should generate 10 chars' do
|
22
|
+
subject.must_be_kind_of String
|
23
|
+
subject.length.must_equal(10)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'string including up & down case' do
|
27
|
+
subject.must_match(/^[a-zA-Z]+$/)
|
28
|
+
end
|
29
|
+
end
|
32
30
|
end
|
33
|
-
|
34
|
-
it 'string including up & down case' do
|
35
|
-
subject.must_match(/^[a-zA-Z]+$/)
|
36
|
-
end
|
37
|
-
|
38
31
|
end
|
39
32
|
end
|
40
33
|
end
|
@@ -1,31 +1,28 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
describe 'ModelWithGeneratorDefaults' do
|
10
|
-
let(:user){ModelWithGeneratorDefaults.new}
|
3
|
+
TestConf.orm_modules.each do |orm_module|
|
4
|
+
describe orm_module.description do
|
5
|
+
describe 'ModelWithGeneratorDefaults' do
|
6
|
+
let(:user) { "#{orm_module}::ModelWithGeneratorDefaults".constantize.new }
|
11
7
|
|
12
|
-
|
13
|
-
|
8
|
+
describe '#public_uid' do
|
9
|
+
subject{ user.public_uid }
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
context 'in new record' do
|
12
|
+
it{ subject.must_be_nil }
|
13
|
+
end
|
18
14
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
context 'after save' do
|
16
|
+
before do
|
17
|
+
user.save
|
18
|
+
user.reload
|
19
|
+
end
|
20
|
+
it{ subject.wont_be_nil }
|
21
|
+
it 'by default should generate 7 digit number string' do
|
22
|
+
subject.to_i.to_s.length.must_equal(7)
|
23
|
+
end
|
24
|
+
end
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
30
28
|
end
|
31
|
-
|
@@ -0,0 +1,29 @@
|
|
1
|
+
ActiveRecord::Base::establish_connection(adapter: 'sqlite3', database: ':memory:')
|
2
|
+
ActiveRecord::Base.connection.execute(%{CREATE TABLE users (id INTEGER PRIMARY KEY, public_uid VARCHAR, custom_uid INTEGER);})
|
3
|
+
|
4
|
+
module ActRec
|
5
|
+
def self.description
|
6
|
+
'ActiveRecord based'
|
7
|
+
end
|
8
|
+
|
9
|
+
class CustomPublicUidColumnModel < ActiveRecord::Base
|
10
|
+
self.table_name = "users"
|
11
|
+
generate_public_uid column: :custom_uid
|
12
|
+
end
|
13
|
+
|
14
|
+
class ModelWithCustomGererator < ActiveRecord::Base
|
15
|
+
self.table_name = "users"
|
16
|
+
|
17
|
+
gener_range = ('a'..'z').to_a+('A'..'Z').to_a
|
18
|
+
|
19
|
+
generate_public_uid generator: PublicUid::Generators::RangeString.
|
20
|
+
new(10, gener_range)
|
21
|
+
end
|
22
|
+
|
23
|
+
class ModelWithGeneratorDefaults < ActiveRecord::Base
|
24
|
+
self.table_name = "users"
|
25
|
+
generate_public_uid
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
TestConf.add_orm_module(ActRec)
|
data/test/test_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
+
require 'pathname'
|
1
2
|
require 'bundler/setup'
|
2
|
-
require 'public_uid'
|
3
3
|
require 'minitest/autorun'
|
4
|
-
require '
|
4
|
+
require 'public_uid'
|
5
5
|
|
6
6
|
class Minitest::Spec
|
7
7
|
class << self
|
@@ -9,6 +9,26 @@ class Minitest::Spec
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
class TestConf
|
13
|
+
class << self
|
14
|
+
def root_path
|
15
|
+
Pathname.new(File.expand_path '../..', __FILE__)
|
16
|
+
end
|
14
17
|
|
18
|
+
def orm_modules
|
19
|
+
@orm_modules ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_orm_module(konstant)
|
23
|
+
orm_modules << konstant
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
['active_record'].each do |orm|
|
29
|
+
begin
|
30
|
+
load TestConf.root_path.join 'test', 'support', 'orm', "#{orm}.rb"
|
31
|
+
rescue LoadError
|
32
|
+
raise "ORM #{orm} not available"
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: public_uid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Valent
|
@@ -11,47 +11,47 @@ cert_chain: []
|
|
11
11
|
date: 2014-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: orm_adapter
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
type: :
|
19
|
+
version: '0.5'
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.3'
|
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
|
-
version: '
|
40
|
+
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
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
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,13 +88,16 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- .gitignore
|
91
|
+
- .travis.yml
|
91
92
|
- Gemfile
|
92
93
|
- LICENSE.txt
|
93
94
|
- README.md
|
94
95
|
- Rakefile
|
96
|
+
- lib/orm/active_record.rb
|
95
97
|
- lib/public_uid.rb
|
96
98
|
- lib/public_uid/generators/number_random.rb
|
97
99
|
- lib/public_uid/generators/range_string.rb
|
100
|
+
- lib/public_uid/model.rb
|
98
101
|
- lib/public_uid/set_public_uid.rb
|
99
102
|
- lib/public_uid/version.rb
|
100
103
|
- public_uid.gemspec
|
@@ -104,8 +107,9 @@ files:
|
|
104
107
|
- test/models/custom_public_uid_column_test.rb
|
105
108
|
- test/models/model_with_custom_generator_test.rb
|
106
109
|
- test/models/model_with_generator_defaults_test.rb
|
110
|
+
- test/support/orm/active_record.rb
|
107
111
|
- test/test_helper.rb
|
108
|
-
homepage:
|
112
|
+
homepage: https://github.com/equivalent/public_uid
|
109
113
|
licenses:
|
110
114
|
- MIT
|
111
115
|
metadata: {}
|
@@ -136,4 +140,5 @@ test_files:
|
|
136
140
|
- test/models/custom_public_uid_column_test.rb
|
137
141
|
- test/models/model_with_custom_generator_test.rb
|
138
142
|
- test/models/model_with_generator_defaults_test.rb
|
143
|
+
- test/support/orm/active_record.rb
|
139
144
|
- test/test_helper.rb
|