uniq_number_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 +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +9 -0
- data/lib/uniq_number_id/version.rb +3 -0
- data/lib/uniq_number_id.rb +62 -0
- data/spec/models/course.rb +17 -0
- data/spec/models/user.rb +17 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/uniq_number_id_spec.rb +29 -0
- data/uniq_number_id.gemspec +26 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1902e9dee1b699cf3e754a3df3e5fda63a5f1ff6
|
4
|
+
data.tar.gz: b4f4d49d8f66583d2f0bdc9776f3f2e1c7bda3c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bc7a10bccdea1f18ed453478cb766dbbff382a1b7b8026a2db71a0c49fd981830167df1a8e63ed14d0496e20df78621fb1f4618599281e7a89132f8fb409c57
|
7
|
+
data.tar.gz: 7eb44c96e57de0ae458bff94da1b63c69f760aa2201d524d36abd3ef11ae44260567bce7a0e38da5d08484e6dcc075535b3bea5101cc42bf0a99304c9eef91ba
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Yuan He
|
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,43 @@
|
|
1
|
+
# UniqNumberId
|
2
|
+
|
3
|
+
Create uniques random number id for any model in ruby on rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'uniq_number_id'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install uniq_number_id
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Example using Active Record:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# Schema: User(member_number:integer)
|
27
|
+
class User < ActiveRecord::Base
|
28
|
+
uniq_number_id :member_number, length: 5 # default length 8
|
29
|
+
end
|
30
|
+
|
31
|
+
user = User.new
|
32
|
+
user.save
|
33
|
+
user.member_number # => 17263
|
34
|
+
user.renew_member_number # => true, will update member_number with a new number
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( https://github.com/[my-github-username]/uniq_number_id/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "active_model"
|
2
|
+
require 'securerandom'
|
3
|
+
require "uniq_number_id/version"
|
4
|
+
|
5
|
+
module UniqNumberId
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
# Example using Active Record:
|
9
|
+
#
|
10
|
+
# # Schema: User(member_number:integer)
|
11
|
+
# class User < ActiveRecord::Base
|
12
|
+
# uniq_number_id :member_number, length: 5
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# user = User.new
|
16
|
+
# user.save
|
17
|
+
# user.member_number # => 17263
|
18
|
+
# user.renew_member_number # => true, will update member_number with a new number
|
19
|
+
#
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def uniq_number_id(*args)
|
23
|
+
include InstanceMethodsOnCallback
|
24
|
+
cattr_accessor :number_columns, :options
|
25
|
+
options = args.extract_options!
|
26
|
+
|
27
|
+
length = options.fetch(:length, 8)
|
28
|
+
bytes = ((length*3.0)/2.0)
|
29
|
+
bytes = 8 if bytes < 8
|
30
|
+
|
31
|
+
args.each do |attr|
|
32
|
+
define_method("renew_#{attr}") do
|
33
|
+
send(:generate_uniq_number_id, attr, bytes, length)
|
34
|
+
save
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
before_create do
|
39
|
+
args.each do |attr|
|
40
|
+
self.generate_uniq_number_id(attr, bytes, length)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethodsOnCallback
|
47
|
+
#TODO: support generate random number bases on attr type
|
48
|
+
# for example, if attr DB type is integer, then generate integer
|
49
|
+
# if attr DB type is String, then generate hex string
|
50
|
+
def generate_uniq_number_id(attr, bytes, length)
|
51
|
+
self.send("#{attr}=", loop do
|
52
|
+
random_number = SecureRandom.random_bytes(bytes).unpack('N')[0]
|
53
|
+
random_number = random_number.to_s[0..length-1].to_i
|
54
|
+
break random_number unless self.class.exists?(attr => random_number)
|
55
|
+
end)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
ActiveSupport::on_load(:active_record) do
|
60
|
+
include UniqNumberId
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class Course
|
2
|
+
extend ActiveModel::Callbacks
|
3
|
+
include ActiveModel::Validations
|
4
|
+
include UniqNumberId
|
5
|
+
|
6
|
+
define_model_callbacks :create
|
7
|
+
attr_accessor :course_code
|
8
|
+
uniq_number_id :course_code, length: 10
|
9
|
+
|
10
|
+
def self.exists?(attrs)
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
14
|
+
def save
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
data/spec/models/user.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
class User
|
2
|
+
extend ActiveModel::Callbacks
|
3
|
+
include ActiveModel::Validations
|
4
|
+
include UniqNumberId
|
5
|
+
|
6
|
+
define_model_callbacks :create
|
7
|
+
attr_accessor :member_number
|
8
|
+
uniq_number_id :member_number, length: 8
|
9
|
+
|
10
|
+
def self.exists?(attrs)
|
11
|
+
false
|
12
|
+
end
|
13
|
+
|
14
|
+
def save
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require 'uniq_number_id'
|
5
|
+
require_relative 'models/user'
|
6
|
+
require_relative 'models/course'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.expect_with :rspec do |expectations|
|
10
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
11
|
+
end
|
12
|
+
|
13
|
+
config.mock_with :rspec do |mocks|
|
14
|
+
mocks.verify_partial_doubles = true
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'uniq number id' do
|
4
|
+
before(:each) do
|
5
|
+
@user = User.new
|
6
|
+
@user.run_callbacks :create
|
7
|
+
@course = Course.new
|
8
|
+
@course.run_callbacks :create
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should has uniq number id' do
|
12
|
+
expect(@user.member_number).to be_a Fixnum
|
13
|
+
expect(@course.course_code).to be_a Fixnum
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should uniq number id has length 8 by default' do
|
17
|
+
expect(@user.member_number.to_s.length).to eq(8)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should custom uniq number length' do
|
21
|
+
expect(@course.course_code.to_s.length).to eq(10)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should renew uniq number id' do
|
25
|
+
old_num = @user.member_number
|
26
|
+
expect(@user.renew_member_number).to be true
|
27
|
+
expect(@user.member_number).not_to eq(old_num)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'uniq_number_id/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'uniq_number_id'
|
8
|
+
spec.version = UniqNumberId::VERSION
|
9
|
+
spec.authors = ['Yuan He']
|
10
|
+
spec.email = ['lendage@gmail.com']
|
11
|
+
spec.summary = %q{Create uniques random number id for any model in ruby on rails.}
|
12
|
+
spec.description = %q{Add to your models an easily way to generate uniq numbers}
|
13
|
+
spec.homepage = 'https://git.llsapp.com/yuan/uniq_number_id'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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 'activemodel', '~> 3.2'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.0'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uniq_number_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuan He
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
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.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.1'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 3.1.0
|
65
|
+
type: :development
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '3.1'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 3.1.0
|
75
|
+
description: Add to your models an easily way to generate uniq numbers
|
76
|
+
email:
|
77
|
+
- lendage@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".gitignore"
|
83
|
+
- ".rspec"
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- lib/uniq_number_id.rb
|
89
|
+
- lib/uniq_number_id/version.rb
|
90
|
+
- spec/models/course.rb
|
91
|
+
- spec/models/user.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/uniq_number_id_spec.rb
|
94
|
+
- uniq_number_id.gemspec
|
95
|
+
homepage: https://git.llsapp.com/yuan/uniq_number_id
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.2.2
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Create uniques random number id for any model in ruby on rails.
|
119
|
+
test_files:
|
120
|
+
- spec/models/course.rb
|
121
|
+
- spec/models/user.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
- spec/uniq_number_id_spec.rb
|
124
|
+
has_rdoc:
|