scoped_id 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +4 -0
- data/lib/scoped_id/concern.rb +56 -0
- data/lib/scoped_id/errors.rb +13 -0
- data/lib/scoped_id/version.rb +3 -0
- data/lib/scoped_id.rb +3 -0
- data/scoped_id.gemspec +28 -0
- data/spec/scoped_id/concern_spec.rb +70 -0
- data/spec/spec_helper.rb +30 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e98b2cafc0f93b81f6b9872ce8c01a1f9a51c5be
|
4
|
+
data.tar.gz: e0f6d16fa4c5532d6b1588ba4ad535015d5ea205
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f6ca0dea90dfabfd778f40ed6615153f438f4672c70093fbdfa7b3271c664ed0c4686381eeac686775eeebca94683564a367ba65fcf45440b2b7d396107e800
|
7
|
+
data.tar.gz: b633b35869cdf8b33d0ea87063053c208fe4a6c2ce6d80449b4ab80a8dfe20f7199bbd6071a335d334e211e2ff43ab3386c7ed6221e23dce4fa04592d5de4dba
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Michel Billard
|
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,42 @@
|
|
1
|
+
# scoped_id
|
2
|
+
|
3
|
+
Generates scoped unique identifiers.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `scoped_id` to your Gemfile:
|
8
|
+
|
9
|
+
gem 'scoped_id', '~> 0.0.1'
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
class Project < ActiveRecord::Base
|
15
|
+
include ScopedId::Concern
|
16
|
+
scoped_id :per_owner_id, scope: :owner_id
|
17
|
+
end
|
18
|
+
|
19
|
+
jacks_project = Project.create(owner_id: 1)
|
20
|
+
jacks_project.per_owner_id # => 1
|
21
|
+
|
22
|
+
johns_project = Project.create(owner_id: 2)
|
23
|
+
johns_project.per_owner_id # => 1
|
24
|
+
```
|
25
|
+
|
26
|
+
The scoped_id is generated in a `before_create` callback unless it has been manually set.
|
27
|
+
|
28
|
+
The scoped_id will be marked as readonly and will validate the uniqueness of its value.
|
29
|
+
|
30
|
+
### Options
|
31
|
+
|
32
|
+
#### Scope (required)
|
33
|
+
|
34
|
+
The scope by which to determine the next identifier when creating a new object.
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it ( http://github.com/mbillard/scoped_id/fork )
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
4
|
+
module ScopedId
|
5
|
+
|
6
|
+
ScopedIdDefinition = Struct.new(:attr_name, :scope_attr)
|
7
|
+
|
8
|
+
module Concern
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
before_create :generate_next_scoped_ids
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def scoped_id(attr_name, options = {})
|
17
|
+
scope = options[:scope]
|
18
|
+
if scope.nil?
|
19
|
+
raise ArgumentError.new(":scope is not defined. It is a mandatory option of scoped_id.")
|
20
|
+
end
|
21
|
+
|
22
|
+
scoped_ids_definitions << ScopedIdDefinition.new(attr_name, scope)
|
23
|
+
|
24
|
+
attr_readonly attr_name
|
25
|
+
|
26
|
+
validates_uniqueness_of attr_name, scope: scope
|
27
|
+
end
|
28
|
+
|
29
|
+
def scoped_ids_definitions
|
30
|
+
@scoped_ids_definitions ||= []
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_next_scoped_ids
|
35
|
+
self.class.scoped_ids_definitions.each do |definition|
|
36
|
+
attr_name = definition.attr_name
|
37
|
+
if send("#{attr_name}").nil?
|
38
|
+
send "#{attr_name}=", send("get_next_scoped_id", definition)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_next_scoped_id(scoped_id_definition)
|
44
|
+
attr_name = scoped_id_definition.attr_name
|
45
|
+
scope_attr = scoped_id_definition.scope_attr
|
46
|
+
|
47
|
+
scope = self.class.where(scope_attr => read_attribute(scope_attr))
|
48
|
+
current_max_scoped_id = scope.maximum(attr_name) || 0
|
49
|
+
current_max_scoped_id + 1
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Uncomment to auto-extend ActiveRecord, probably not a good idea
|
56
|
+
# ActiveRecord::Base.send(:extend, ScopedId)
|
data/lib/scoped_id.rb
ADDED
data/scoped_id.gemspec
ADDED
@@ -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 'scoped_id/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scoped_id"
|
8
|
+
spec.version = ScopedId::VERSION
|
9
|
+
spec.authors = ["Michel Billard"]
|
10
|
+
spec.email = ["michel@mbillard.com"]
|
11
|
+
spec.summary = "Generates scoped unique identifiers"
|
12
|
+
# spec.description = %q{TODO: Write a longer description. Optional.}
|
13
|
+
spec.homepage = "http://github.com/mbillard/scoped_id"
|
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_dependency 'activerecord', '>= 4.0.0'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
24
|
+
spec.add_development_dependency 'pry'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
spec.add_development_dependency 'sqlite3'
|
28
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ScopedId do
|
4
|
+
|
5
|
+
class Model < BaseModel
|
6
|
+
scoped_id :owner_scoped_id, scope: :owner_id
|
7
|
+
end
|
8
|
+
|
9
|
+
it "generates 1 for the first ID" do
|
10
|
+
model = Model.create
|
11
|
+
model.owner_scoped_id.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "generates 2 for the second ID" do
|
15
|
+
Model.create
|
16
|
+
model = Model.create
|
17
|
+
model.owner_scoped_id.should == 2
|
18
|
+
end
|
19
|
+
|
20
|
+
it "generates 1 if other models exist for a different scope" do
|
21
|
+
Model.create(owner_id: 3)
|
22
|
+
model = Model.create(owner_id: 7)
|
23
|
+
model.owner_scoped_id.should == 1
|
24
|
+
end
|
25
|
+
|
26
|
+
it "generates the next id even if a model was deleted" do
|
27
|
+
Model.create # 1
|
28
|
+
model_to_delete = Model.create # 2
|
29
|
+
Model.create # 3
|
30
|
+
model = Model.create
|
31
|
+
|
32
|
+
model_to_delete.destroy
|
33
|
+
model.owner_scoped_id.should == 4
|
34
|
+
end
|
35
|
+
|
36
|
+
it "marks the scoped id as readonly" do
|
37
|
+
model = Model.create
|
38
|
+
model.owner_scoped_id = 78
|
39
|
+
model.save
|
40
|
+
model.reload.owner_scoped_id.should == 1
|
41
|
+
end
|
42
|
+
|
43
|
+
it "allows setting a scoped id value before creation" do
|
44
|
+
model = Model.new
|
45
|
+
model.owner_scoped_id = 123
|
46
|
+
model.save
|
47
|
+
model.owner_scoped_id.should == 123
|
48
|
+
end
|
49
|
+
|
50
|
+
it "validates the uniqueness of the scoped id" do
|
51
|
+
Model.create(owner_scoped_id: 123)
|
52
|
+
model = Model.new(owner_scoped_id: 123)
|
53
|
+
model.valid?.should be_false
|
54
|
+
end
|
55
|
+
|
56
|
+
it "validates the uniqueness of the scoped id per scope" do
|
57
|
+
Model.create(owner_scoped_id: 123, owner_id: 13)
|
58
|
+
model = Model.new(owner_scoped_id: 123, owner_id: 9)
|
59
|
+
model.valid?.should be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
it "raises an exception if scope is not specified" do
|
63
|
+
expect{
|
64
|
+
Class.new(BaseModel) do
|
65
|
+
scoped_id :owner_scoped_id # no scope
|
66
|
+
end
|
67
|
+
}.to raise_error(ArgumentError)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'scoped_id'
|
3
|
+
|
4
|
+
# Require this file using `require 'spec_helper'` to ensure that it is only
|
5
|
+
# loaded once.
|
6
|
+
#
|
7
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.before :each do
|
10
|
+
Model.delete_all
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
|
15
|
+
database: ':memory:')
|
16
|
+
|
17
|
+
ActiveRecord::Schema.define do
|
18
|
+
self.verbose = false
|
19
|
+
|
20
|
+
create_table :models, force: true do |t|
|
21
|
+
t.string :owner_id
|
22
|
+
t.integer :owner_scoped_id
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
class BaseModel < ActiveRecord::Base
|
28
|
+
self.table_name = 'models'
|
29
|
+
include ScopedId::Concern
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scoped_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michel Billard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- michel@mbillard.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- .gitignore
|
105
|
+
- .rspec
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE.txt
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- lib/scoped_id.rb
|
111
|
+
- lib/scoped_id/concern.rb
|
112
|
+
- lib/scoped_id/errors.rb
|
113
|
+
- lib/scoped_id/version.rb
|
114
|
+
- scoped_id.gemspec
|
115
|
+
- spec/scoped_id/concern_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
homepage: http://github.com/mbillard/scoped_id
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.3.0
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Generates scoped unique identifiers
|
141
|
+
test_files:
|
142
|
+
- spec/scoped_id/concern_spec.rb
|
143
|
+
- spec/spec_helper.rb
|