uniq_identifier 0.0.7 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +39 -6
- data/lib/uniq_identifier.rb +24 -5
- data/lib/uniq_identifier/hook.rb +9 -7
- data/lib/uniq_identifier/version.rb +4 -1
- data/spec/spec_helper.rb +5 -5
- data/spec/support/adapters/active_record.rb +0 -1
- data/spec/support/adapters/mongoid.yml +5 -0
- data/spec/support/class_generator.rb +43 -0
- data/{lib/uniq_identifier → spec/support}/fake_generator.rb +1 -1
- data/spec/uniq_identifier/uniq_identifier_spec.rb +60 -45
- metadata +7 -6
- data/spec/support/data.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92214b9affc9199eb4dc10482043977e685904a8
|
4
|
+
data.tar.gz: 5d07a33eaeb2ccc6f61497cb1a53ee1745464faa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25296558a96400d97799d936517a959cd6e9c94960d0ce4705db05bc5fb511f4ff2fa07456a7a3caf6798bd965c59f6f498984dc4859c416c092ab150d35e655
|
7
|
+
data.tar.gz: 924b77a7cb8a605cbac0bab7a140205f2fc3d96a11236f730c23f408967d932c284e69e7f093a1129238ab9d069d47cf15af6a784c30259bc555a8898b3d5282
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
### VERSION 0.1.1
|
2
|
+
|
3
|
+
* Enhancements
|
4
|
+
* You now can define your own specific Generator for every model, not just in a global config level.
|
5
|
+
* You can deactivate generation auto to ensure a UUID is provided
|
6
|
+
* You can deactivate the validation and authorize recording a empty UUID
|
7
|
+
|
1
8
|
### VERSION 0.0.7
|
2
9
|
|
3
10
|
* Enhancements
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# UniqIdentifier
|
2
2
|
|
3
|
-
[![Code Climate](https://codeclimate.com/github/
|
3
|
+
[![Code Climate](https://codeclimate.com/github/FinalCAD/uniq_identifier.png)](https://codeclimate.com/github/FinalCAD/uniq_identifier)
|
4
4
|
|
5
|
-
[![Dependency Status](https://gemnasium.com/
|
5
|
+
[![Dependency Status](https://gemnasium.com/FinalCAD/uniq_identifier.png)](https://gemnasium.com/FinalCAD/uniq_identifier)
|
6
6
|
|
7
|
-
[![Build Status](https://travis-ci.org/
|
7
|
+
[![Build Status](https://travis-ci.org/FinalCAD/uniq_identifier.svg?branch=master)](https://travis-ci.org/FinalCAD/uniq_identifier) (Travis CI)
|
8
8
|
|
9
|
-
[![Coverage Status](https://coveralls.io/repos/
|
9
|
+
[![Coverage Status](https://coveralls.io/repos/FinalCAD/uniq_identifier/badge.svg?branch=master)](https://coveralls.io/r/FinalCAD/uniq_identifier?branch=master)
|
10
|
+
|
11
|
+
[![Gem Version](https://badge.fury.io/rb/uniq_identifier.svg)](https://badge.fury.io/rb/uniq_identifier)
|
10
12
|
|
11
13
|
Add an uniq identifier on your models. For make your model agnostic from backend unique identifier.
|
12
14
|
|
@@ -65,15 +67,36 @@ end
|
|
65
67
|
|
66
68
|
you can use the generator
|
67
69
|
|
68
|
-
```
|
70
|
+
```bash
|
69
71
|
rails g uniq_identifier:install
|
70
72
|
rails g uniq_identifier:add <model>
|
71
73
|
```
|
72
74
|
for mongoid use
|
73
75
|
|
74
|
-
```
|
76
|
+
```bash
|
75
77
|
rails g uniq_identifier:add <model> --orm=mongoid
|
76
78
|
```
|
79
|
+
|
80
|
+
## Options
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class CustomGenerator
|
84
|
+
def uuid
|
85
|
+
# ...
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class Bar
|
90
|
+
uniq_identifier generator: CustomGenerator,
|
91
|
+
auto: bool,
|
92
|
+
validate: bool
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
* `generator` can be any object which respond to `uuid` signal or `:default` (default: `:default`)
|
97
|
+
* `auto` if set to false then uuid will not be automatically generated after initialize (default: `true`)
|
98
|
+
* `validate` if set to false validation will not be added (default: `true`)
|
99
|
+
|
77
100
|
## Contributing
|
78
101
|
|
79
102
|
1. Fork it ( https://github.com/[my-github-username]/uniq_identifier/fork )
|
@@ -81,3 +104,13 @@ rails g uniq_identifier:add <model> --orm=mongoid
|
|
81
104
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
82
105
|
4. Push to the branch (`git push origin my-new-feature`)
|
83
106
|
5. Create a new Pull Request
|
107
|
+
|
108
|
+
## Test
|
109
|
+
|
110
|
+
```bash
|
111
|
+
bundle && ADAPTER=mongoid bundle
|
112
|
+
```
|
113
|
+
|
114
|
+
```bash
|
115
|
+
rake
|
116
|
+
```
|
data/lib/uniq_identifier.rb
CHANGED
@@ -1,14 +1,33 @@
|
|
1
1
|
require_relative 'uniq_identifier/railtie' if defined?(Rails)
|
2
2
|
require_relative 'uniq_identifier/hook'
|
3
3
|
require_relative 'uniq_identifier/configure'
|
4
|
-
require_relative 'uniq_identifier/fake_generator'
|
5
4
|
|
6
5
|
module UniqIdentifier
|
7
6
|
extend Configure
|
8
7
|
|
9
|
-
def uniq_identifier
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
def uniq_identifier(auto: true, validate: true, generator: :default)
|
9
|
+
@uniq_identifier_generator = generator
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def uniq_identifier_generator
|
13
|
+
generator = @uniq_identifier_generator
|
14
|
+
if generator.nil? && superclass.respond_to?(:uniq_identifier_generator)
|
15
|
+
superclass.uniq_identifier_generator
|
16
|
+
elsif generator == :default
|
17
|
+
UniqIdentifier.configuration.generator
|
18
|
+
else
|
19
|
+
generator
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
if auto
|
25
|
+
before_validation :set_uniq_identifier
|
26
|
+
include Hook
|
27
|
+
end
|
28
|
+
|
29
|
+
if validate
|
30
|
+
validates :uuid, presence: true, uniqueness: true
|
31
|
+
end
|
13
32
|
end
|
14
33
|
end
|
data/lib/uniq_identifier/hook.rb
CHANGED
@@ -2,17 +2,19 @@ require 'securerandom'
|
|
2
2
|
|
3
3
|
module UniqIdentifier
|
4
4
|
module Hook
|
5
|
-
|
6
5
|
def uuid(*args, &block)
|
7
|
-
if super(*args).nil?
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
super(*args)
|
6
|
+
generate_uniq_identifier! if super(*args, &block).nil?
|
7
|
+
super
|
12
8
|
end
|
13
9
|
|
14
10
|
def set_uniq_identifier
|
15
|
-
self.uuid
|
11
|
+
generate_uniq_identifier! if self.uuid.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_uniq_identifier!
|
15
|
+
if self.class.uniq_identifier_generator.respond_to?(:uuid)
|
16
|
+
self.uuid = self.class.uniq_identifier_generator.uuid
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -4,12 +4,12 @@ require 'coveralls'
|
|
4
4
|
|
5
5
|
require 'rails/all'
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
require 'pry' rescue nil
|
8
|
+
|
9
|
+
require_relative './support/fake_generator'
|
10
|
+
require_relative './support/class_generator'
|
11
11
|
|
12
|
-
Coveralls.wear!
|
12
|
+
Coveralls.wear! rescue nil
|
13
13
|
|
14
14
|
ENV['ADAPTER'] ||= 'active_record'
|
15
15
|
load File.dirname(__FILE__) + "/support/adapters/#{ENV['ADAPTER']}.rb"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module ClassGenerator
|
2
|
+
extend self
|
3
|
+
|
4
|
+
def generate(**opts)
|
5
|
+
if defined? Mongoid
|
6
|
+
generate_mongoid_document!(opts)
|
7
|
+
else
|
8
|
+
generate_active_record!(opts)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def generate_active_record!(**opts)
|
15
|
+
klass = ::Class.new(parent_class)
|
16
|
+
|
17
|
+
def klass.name
|
18
|
+
'User'
|
19
|
+
end
|
20
|
+
|
21
|
+
klass.inheritance_column = '__'
|
22
|
+
klass.define_callbacks :initialize # reset initialize callbacks
|
23
|
+
klass.define_model_callbacks :initialize # reset initialize callbacks
|
24
|
+
klass.uniq_identifier opts
|
25
|
+
klass
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_mongoid_document!(**opts)
|
29
|
+
klass = ::Class.new(parent_class)
|
30
|
+
|
31
|
+
def klass.name
|
32
|
+
'User'
|
33
|
+
end
|
34
|
+
|
35
|
+
klass.define_model_callbacks :initialize # reset initialize callbacks
|
36
|
+
klass.uniq_identifier opts
|
37
|
+
klass
|
38
|
+
end
|
39
|
+
|
40
|
+
def parent_class
|
41
|
+
::User
|
42
|
+
end
|
43
|
+
end
|
@@ -4,67 +4,82 @@ describe UniqIdentifier do
|
|
4
4
|
let(:user) { User.new }
|
5
5
|
let(:fake_uuid) { SecureRandom.uuid }
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
7
|
+
context 'all settings default' do
|
8
|
+
let(:fake_uuid) { UniqIdentifier::FakeGenerator.uuid }
|
10
9
|
|
11
|
-
|
12
|
-
expect {
|
13
|
-
expect(user.uuid).to eql(fake_uuid)
|
14
|
-
}.to change {
|
15
|
-
user.attributes['uuid']
|
16
|
-
}.from(nil).to(fake_uuid)
|
17
|
-
end
|
10
|
+
let(:default_settings_class) { ClassGenerator.generate }
|
18
11
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
12
|
+
it 'must set uuid for new record' do
|
13
|
+
expect(default_settings_class.new.uuid).to be_eql fake_uuid
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'must have callback method' do
|
17
|
+
expect(default_settings_class.new).to respond_to :set_uniq_identifier
|
18
|
+
end
|
25
19
|
end
|
26
20
|
|
27
|
-
context '
|
28
|
-
let(:
|
21
|
+
context 'generator option' do
|
22
|
+
let(:fake_uuid) { '0c6bbc03-a269-44e2-8075-f442e1aac0c1' }
|
23
|
+
let(:default_uuid) { UniqIdentifier::FakeGenerator.uuid }
|
24
|
+
let(:generator) { OpenStruct.new(uuid: fake_uuid) }
|
25
|
+
|
26
|
+
let!(:custom_settings_class) { ClassGenerator.generate generator: generator }
|
27
|
+
let!(:default_settings_class) { ClassGenerator.generate }
|
28
|
+
let!(:nil_settings_class) { ClassGenerator.generate generator: nil }
|
29
|
+
let!(:inherited_settings_class) { ::Class.new(custom_settings_class) }
|
30
|
+
|
31
|
+
it 'must have different generator when some was given' do
|
32
|
+
default_generator_current = default_settings_class.uniq_identifier_generator
|
33
|
+
custom_generator_current = custom_settings_class.uniq_identifier_generator
|
34
|
+
expect(default_generator_current).to_not be_eql custom_generator_current
|
35
|
+
end
|
29
36
|
|
30
|
-
|
31
|
-
|
37
|
+
it 'must use given generator' do
|
38
|
+
expect(custom_settings_class.uniq_identifier_generator).to be_eql generator
|
39
|
+
end
|
32
40
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
41
|
+
it 'must accept nil as generator when option was given' do
|
42
|
+
expect(nil_settings_class.uniq_identifier_generator).to be_eql nil
|
36
43
|
end
|
37
44
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
user.save!
|
42
|
-
end
|
45
|
+
it 'must nil as generator can\'t raise error' do
|
46
|
+
expect { nil_settings_class.new.uuid }.to_not raise_exception
|
47
|
+
end
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
|
49
|
+
it 'inherited class must have parent generator' do
|
50
|
+
custom_generator_current = custom_settings_class.uniq_identifier_generator
|
51
|
+
inherited_generator = inherited_settings_class.uniq_identifier_generator
|
52
|
+
expect(inherited_generator).to be_eql custom_generator_current
|
47
53
|
end
|
54
|
+
end
|
48
55
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
56
|
+
context 'auto option' do
|
57
|
+
let(:fake_uuid) { '0c6bbc03-a269-44e2-8075-f442e1aac0c1' }
|
58
|
+
let(:default_uuid) { UniqIdentifier::FakeGenerator.uuid }
|
59
|
+
let(:generator) { OpenStruct.new(uuid: fake_uuid) }
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
let!(:custom_settings_class) { ClassGenerator.generate auto: false }
|
62
|
+
let!(:default_settings_class) { ClassGenerator.generate }
|
63
|
+
|
64
|
+
it 'disabled auto must not set uuid' do
|
65
|
+
expect(custom_settings_class.new.uuid).to be_nil
|
58
66
|
end
|
59
67
|
|
60
|
-
|
61
|
-
|
68
|
+
it 'enabled auto must set uuid' do
|
69
|
+
expect(default_settings_class.new.uuid).to_not be_nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'persistence' do
|
74
|
+
let!(:default_settings_class) { ClassGenerator.generate generator: SecureRandom }
|
62
75
|
|
63
|
-
|
76
|
+
it 'must save uuid and return it' do
|
77
|
+
expect(default_settings_class.create!(name: 'example').uuid).to_not be_nil
|
78
|
+
end
|
64
79
|
|
65
|
-
|
66
|
-
|
67
|
-
|
80
|
+
it 'must not change uuid during save' do
|
81
|
+
user = default_settings_class.new(name: 'example')
|
82
|
+
expect { user.save! }.to_not change { user.uuid }
|
68
83
|
end
|
69
84
|
end
|
70
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uniq_identifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel AZEMAR
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -67,7 +67,6 @@ files:
|
|
67
67
|
- lib/uniq_identifier.rb
|
68
68
|
- lib/uniq_identifier/configuration.rb
|
69
69
|
- lib/uniq_identifier/configure.rb
|
70
|
-
- lib/uniq_identifier/fake_generator.rb
|
71
70
|
- lib/uniq_identifier/hook.rb
|
72
71
|
- lib/uniq_identifier/railtie.rb
|
73
72
|
- lib/uniq_identifier/version.rb
|
@@ -78,7 +77,8 @@ files:
|
|
78
77
|
- spec/support/adapters/active_record.rb
|
79
78
|
- spec/support/adapters/mongoid.rb
|
80
79
|
- spec/support/adapters/mongoid.yml
|
81
|
-
- spec/support/
|
80
|
+
- spec/support/class_generator.rb
|
81
|
+
- spec/support/fake_generator.rb
|
82
82
|
- spec/support/schema.rb
|
83
83
|
- spec/uniq_identifier/configuration_spec.rb
|
84
84
|
- spec/uniq_identifier/uniq_identifier_spec.rb
|
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
version: '0'
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.5.1
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: Add an uniq identifier
|
@@ -115,7 +115,8 @@ test_files:
|
|
115
115
|
- spec/support/adapters/active_record.rb
|
116
116
|
- spec/support/adapters/mongoid.rb
|
117
117
|
- spec/support/adapters/mongoid.yml
|
118
|
-
- spec/support/
|
118
|
+
- spec/support/class_generator.rb
|
119
|
+
- spec/support/fake_generator.rb
|
119
120
|
- spec/support/schema.rb
|
120
121
|
- spec/uniq_identifier/configuration_spec.rb
|
121
122
|
- spec/uniq_identifier/uniq_identifier_spec.rb
|
data/spec/support/data.rb
DELETED