uniq_identifier 0.0.7 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6ae67a2e8c41e72b1ce093225ba045e49dc2657f
4
- data.tar.gz: 54eb0124b08b54a3d8dd5ccda5041980ecec8de1
3
+ metadata.gz: 92214b9affc9199eb4dc10482043977e685904a8
4
+ data.tar.gz: 5d07a33eaeb2ccc6f61497cb1a53ee1745464faa
5
5
  SHA512:
6
- metadata.gz: 417c2e239763f9271c83c739bb02761cba7d0390056a16366efb4173475b24b8a24f0607a6d9bf9777be3047e32cbfc5b1b9d2b9a391cd454e50427991e32237
7
- data.tar.gz: fa51e4f12e137a88f0138c536a67fb45aec525eab3a3532c6a6bb7b7a5c3280cf4ef3d4dbd62ecf2b6e356c1ed00de16c58256f4f150a73ee59ae73ac59891ac
6
+ metadata.gz: 25296558a96400d97799d936517a959cd6e9c94960d0ce4705db05bc5fb511f4ff2fa07456a7a3caf6798bd965c59f6f498984dc4859c416c092ab150d35e655
7
+ data.tar.gz: 924b77a7cb8a605cbac0bab7a140205f2fc3d96a11236f730c23f408967d932c284e69e7f093a1129238ab9d069d47cf15af6a784c30259bc555a8898b3d5282
@@ -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/joel/uniq_identifier.png)](https://codeclimate.com/github/joel/uniq_identifier)
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/joel/uniq_identifier.png)](https://gemnasium.com/joel/uniq_identifier)
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/joel/uniq_identifier.png?branch=master)](https://travis-ci.org/joel/uniq_identifier) (Travis CI)
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/joel/uniq_identifier/badge.svg?branch=master)](https://coveralls.io/r/joel/uniq_identifier?branch=master)
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
- ```ruby
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
- ```ruby
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
+ ```
@@ -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
- prepend Hook
11
- before_save :set_uniq_identifier
12
- validates :uuid, presence: true, uniqueness: true
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
@@ -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
- self.send(:uuid=, UniqIdentifier.configuration.generator.uuid)
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 # Just call lazy loading
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
@@ -1,3 +1,6 @@
1
1
  module UniqIdentifier
2
- VERSION = '0.0.7'
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ PATCH = 1
5
+ VERSION = [MAJOR, MINOR, PATCH].join('.')
3
6
  end
@@ -4,12 +4,12 @@ require 'coveralls'
4
4
 
5
5
  require 'rails/all'
6
6
 
7
- begin
8
- require 'pry'
9
- rescue LoadError
10
- end
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"
@@ -6,5 +6,4 @@ ActiveRecord::Base.extend UniqIdentifier
6
6
  load File.dirname(__FILE__) + '/../schema.rb'
7
7
 
8
8
  class User < ActiveRecord::Base
9
- uniq_identifier
10
9
  end
@@ -4,3 +4,8 @@ test:
4
4
  database: godfather
5
5
  hosts:
6
6
  - localhost:27017
7
+ clients:
8
+ default:
9
+ database: godfather
10
+ hosts:
11
+ - localhost:27017
@@ -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
@@ -2,7 +2,7 @@ module UniqIdentifier
2
2
  class FakeGenerator
3
3
 
4
4
  def self.uuid
5
- "0c6bbc03-a269-44e2-8075-f442e1aac0c8"
5
+ '0c6bbc03-a269-44e2-8075-f442e1aac0c8'
6
6
  end
7
7
 
8
8
  end
@@ -4,67 +4,82 @@ describe UniqIdentifier do
4
4
  let(:user) { User.new }
5
5
  let(:fake_uuid) { SecureRandom.uuid }
6
6
 
7
- before do
8
- allow(UniqIdentifier).to receive_message_chain('configuration.generator.uuid') { fake_uuid }
9
- end
7
+ context 'all settings default' do
8
+ let(:fake_uuid) { UniqIdentifier::FakeGenerator.uuid }
10
9
 
11
- specify 'lazy load' do
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
- specify do
20
- expect {
21
- user.save!
22
- }.to change {
23
- user.attributes['uuid']
24
- }.from(nil).to(fake_uuid)
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 'persistence' do
28
- let(:user_id) { user.id }
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
- context 'set uuid on save' do
31
- before { user.save! }
37
+ it 'must use given generator' do
38
+ expect(custom_settings_class.uniq_identifier_generator).to be_eql generator
39
+ end
32
40
 
33
- specify do
34
- expect(User.find(user_id).uuid).to eql(fake_uuid)
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
- context 'keep given uuid on save' do
39
- before do
40
- user.uuid = fake_uuid
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
- specify do
45
- expect(User.find(user_id).uuid).to eql(fake_uuid)
46
- end
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
- context 'keep given uuid on save' do
50
- before do
51
- user.attributes = { uuid: fake_uuid }
52
- user.save!
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
- specify do
56
- expect(User.find(user_id).uuid).to eql(fake_uuid)
57
- end
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
- context 'keep given uuid on save' do
61
- let(:user) { User.new({ uuid: fake_uuid }) }
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
- before { user.save! }
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
- specify do
66
- expect(User.find(user_id).uuid).to eql(fake_uuid)
67
- end
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.0.7
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: 2016-09-28 00:00:00.000000000 Z
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/data.rb
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.6.6
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/data.rb
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
@@ -1,2 +0,0 @@
1
- User.destroy_all
2
- user.create(name: 'John Doe')