uidable 1.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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +138 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/uidable/version.rb +3 -0
- data/lib/uidable.rb +63 -0
- data/uidable.gemspec +28 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5f28135965e6d202b5b1bed1cba057d98c38c9c4
|
4
|
+
data.tar.gz: 22045616ed69d00b46ad8aa92bbd2c18164dfeeb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fa5a9acc323073795f704b5c808dddf9500e3bb617e822a153c93cceda2806a30d38fe252a0c404adbc4ed0a03857d67e0b77bdb3eee4653f0864ad80c8ce357
|
7
|
+
data.tar.gz: 2786c31ff3cf9cfe7319eb569770ff7203a17882b2bee37598e82e67b83872fc178d04dfac2abc2ad1cd3de7f5a54ca5b0790afb92ac31360f57c216e4c07fd7
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Sibevin Wang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
# Uidable
|
2
|
+
|
3
|
+
Create the uid(unqiue identifier) attribute in your model or class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'uidable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install uidable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### ActiveRecord
|
24
|
+
|
25
|
+
1. Create a migration to add the uid column in your table.
|
26
|
+
2. Include uidable in your model.
|
27
|
+
3. Setup uidable with/without options in your model.
|
28
|
+
4. Uid is generated and ready to use when a record is created. Note that the uid is still nil when the record is initialized but haven't be saved.
|
29
|
+
|
30
|
+
A migration example:
|
31
|
+
|
32
|
+
class AddUidToMyModels < ActiveRecord::Migration
|
33
|
+
def change
|
34
|
+
add_column :my_models, :uid, :string, null: false
|
35
|
+
add_index :my_models, :uid, unique: true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
A model example:
|
40
|
+
|
41
|
+
class MyModel
|
42
|
+
include Uidable
|
43
|
+
uidable
|
44
|
+
end
|
45
|
+
|
46
|
+
a = MyClass.new
|
47
|
+
a.uid # nil
|
48
|
+
a.save
|
49
|
+
a.uid "cmerft8rotdy7wvmtxc63ljoxos67bc8"
|
50
|
+
|
51
|
+
### Ruby Class
|
52
|
+
|
53
|
+
1. Include uidable in your class.
|
54
|
+
2. Setup uidable with/without options in your class.
|
55
|
+
3. Uid is generated and ready to use when an instance is initialized.
|
56
|
+
|
57
|
+
A class example:
|
58
|
+
|
59
|
+
class MyClass
|
60
|
+
include Uidable
|
61
|
+
uidable uid_size: 64, read_only: false
|
62
|
+
end
|
63
|
+
|
64
|
+
a = MyClass.new
|
65
|
+
a.uid # "zcf45ltmkyh4w2ofsc1rp8dka6wi4flt3h3szwo1z4rkfsvk387mclg1cikutbc7"
|
66
|
+
|
67
|
+
Please reference tests for more usage examples.
|
68
|
+
|
69
|
+
## Options
|
70
|
+
|
71
|
+
### Uid Name
|
72
|
+
|
73
|
+
The default uid attribute is named with "uid", you can change it with `uid_name: <name>`. Note that you need change the column name in your migration as well.
|
74
|
+
|
75
|
+
### Uid Size
|
76
|
+
|
77
|
+
The default uid is a 32-bit length string with numbers and alphabets. You can change the uid size with `uid_size: <size>`. If you want to generate the uid with your own way, please see [Redefine uid generation].
|
78
|
+
|
79
|
+
### Read Only
|
80
|
+
|
81
|
+
The uid is read only by default. You can disabled it with `read_only: false`.
|
82
|
+
|
83
|
+
## Options for ActiveRecord
|
84
|
+
|
85
|
+
### Uniqueness and Presence Validation
|
86
|
+
|
87
|
+
The uniqueness and presence validation is enabled by default. You can disable them with `uniqueness: false` and `presence: false`. Note that you should change your migration as well if needed.
|
88
|
+
|
89
|
+
### Set to_param
|
90
|
+
|
91
|
+
If the option `set_to_param: true` is given, the `to_param` is overrided with uid and it means you can use uid in your routes path.
|
92
|
+
|
93
|
+
### Scope
|
94
|
+
|
95
|
+
If the option `scope: true` is given, a scope `with_uid` is created and you can use it to find records with uid. Note that if you change the uid name with the option `uid_name: <name>`, the scope is also changed to `with_<name>`.
|
96
|
+
|
97
|
+
## Redefine Uid Generation
|
98
|
+
|
99
|
+
You override `gen_uid` method in your class/model if you want to generate your own uid. Here is an example:
|
100
|
+
|
101
|
+
require `random_token`
|
102
|
+
|
103
|
+
class MyModel < ActiveRecord::Base
|
104
|
+
include Uidable
|
105
|
+
uidable
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def gen_uid
|
110
|
+
RandomToken.gen(64, s: 8)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
## Test
|
115
|
+
|
116
|
+
Run `ruby test/test_all.rb`
|
117
|
+
|
118
|
+
## Development
|
119
|
+
|
120
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
121
|
+
|
122
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
123
|
+
|
124
|
+
## Contributing
|
125
|
+
|
126
|
+
1. Fork it ( https://github.com/sibevin/uidable/fork )
|
127
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
128
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
129
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
130
|
+
5. Create a new Pull Request
|
131
|
+
|
132
|
+
## Authors
|
133
|
+
|
134
|
+
Sibevin Wang
|
135
|
+
|
136
|
+
## Copyright
|
137
|
+
|
138
|
+
Copyright (c) 2015 Sibevin Wang. Released under the MIT license.
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "uidable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/uidable.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "uidable/version"
|
2
|
+
|
3
|
+
module Uidable
|
4
|
+
DEFAULT_UID_SIZE = 32
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def uidable(
|
12
|
+
uid_name: "uid",
|
13
|
+
uid_size: DEFAULT_UID_SIZE,
|
14
|
+
read_only: true,
|
15
|
+
presence: true,
|
16
|
+
uniqueness: true,
|
17
|
+
set_to_param: false,
|
18
|
+
scope: false)
|
19
|
+
mod = Module.new
|
20
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
21
|
+
def self.included(base)
|
22
|
+
if defined?(::ActiveRecord::Base) && base < ::ActiveRecord::Base
|
23
|
+
base.before_validation :assign_uid, on: :create
|
24
|
+
#{ scope ? "base.scope :'with_#{uid_name}', -> (uid) { base.where(:'#{uid_name}' => uid) }" : "" }
|
25
|
+
#{ set_to_param ? "base.include SetToParam" : "" }
|
26
|
+
#{ read_only ? "base.attr_readonly :'#{uid_name}'" : "" }
|
27
|
+
else
|
28
|
+
base.prepend InitUid
|
29
|
+
#{ read_only ? "attr_reader :'#{uid_name}'" : "attr_accessor :'#{uid_name}'" }
|
30
|
+
end
|
31
|
+
if base.respond_to?(:validates)
|
32
|
+
#{ presence ? "base.validates :'#{uid_name}', presence: true" : "" }
|
33
|
+
#{ uniqueness ? "base.validates :'#{uid_name}', uniqueness: true" : "" }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module InitUid
|
38
|
+
def initialize
|
39
|
+
@#{uid_name} = gen_uid
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module SetToParam
|
45
|
+
def to_param
|
46
|
+
self.#{uid_name}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def assign_uid
|
53
|
+
self.#{uid_name} = gen_uid
|
54
|
+
end
|
55
|
+
|
56
|
+
def gen_uid
|
57
|
+
Array.new(#{uid_size}){[*'a'..'b', *'0'..'9'].sample}.join
|
58
|
+
end
|
59
|
+
RUBY
|
60
|
+
include mod
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/uidable.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 'uidable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "uidable"
|
8
|
+
spec.version = Uidable::VERSION
|
9
|
+
spec.authors = ["Sibevin Wang"]
|
10
|
+
spec.email = ["sibevin@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Create the uid attribute in your model or class.}
|
13
|
+
spec.description = <<-EOF
|
14
|
+
Uidable is a module to add a uid(unique identifier) attribute in your model or class.
|
15
|
+
With ActiveRecord, the presence and uniqueness validations are supported.
|
16
|
+
EOF
|
17
|
+
spec.homepage = "https://github.com/sibevin/uidable"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 2.0.0'
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uidable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sibevin Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: |2
|
42
|
+
Uidable is a module to add a uid(unique identifier) attribute in your model or class.
|
43
|
+
With ActiveRecord, the presence and uniqueness validations are supported.
|
44
|
+
email:
|
45
|
+
- sibevin@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".rspec"
|
52
|
+
- ".travis.yml"
|
53
|
+
- CODE_OF_CONDUCT.md
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- bin/console
|
58
|
+
- bin/setup
|
59
|
+
- lib/uidable.rb
|
60
|
+
- lib/uidable/version.rb
|
61
|
+
- uidable.gemspec
|
62
|
+
homepage: https://github.com/sibevin/uidable
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.0.0
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Create the uid attribute in your model or class.
|
86
|
+
test_files: []
|