uniqueness 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/gemfiles/40.gemfile.lock +3 -3
- data/gemfiles/41.gemfile.lock +3 -3
- data/gemfiles/42.gemfile.lock +3 -3
- data/gemfiles/50.gemfile.lock +3 -3
- data/lib/uniqueness.rb +1 -0
- data/lib/uniqueness/generator.rb +31 -0
- data/lib/uniqueness/model.rb +3 -25
- data/lib/uniqueness/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99d56bdd5db29acb11bcd1259aafe34004684f88
|
4
|
+
data.tar.gz: 6cea06eaa400b0ee4c1c96aaa765aec6a5912288
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d093e6299223e48b634116ee995b5b878a404bfa6424bb106735bb756f725ec577246f32127078184363e29e993f750235820bd4fa56cbdc38a6fd2372d86e50
|
7
|
+
data.tar.gz: 242b28d6005cd2daaa3616510ec9459a0341973ef5bada7f97a00d265eb566d7c378be806ee178f09feb1fdcc145575476e89f805ecfb0a3e1f50f42a00f63db
|
data/README.md
CHANGED
@@ -61,6 +61,10 @@ Human type generates strings easier to read by excluding ambiguous characters li
|
|
61
61
|
|
62
62
|
`:scope` scopes, defines the `ActiveRecord` `scope` applied before calculating the `position` field value. Defaults to __[]__
|
63
63
|
|
64
|
+
To generate a unique-random on the fly `Uniqueness.generate` that will produce a random field for you.
|
65
|
+
|
66
|
+
You can also pass some options `Uniqueness.generate(type: :human)`.
|
67
|
+
|
64
68
|
## Development
|
65
69
|
|
66
70
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/gemfiles/40.gemfile.lock
CHANGED
data/gemfiles/41.gemfile.lock
CHANGED
data/gemfiles/42.gemfile.lock
CHANGED
data/gemfiles/50.gemfile.lock
CHANGED
data/lib/uniqueness.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Uniqueness
|
2
|
+
class << self
|
3
|
+
def generate(opts = {})
|
4
|
+
options ||= {}
|
5
|
+
options = uniqueness_default_options.merge(opts)
|
6
|
+
dict = uniqueness_dictionary - options[:blacklist]
|
7
|
+
dict -= [*(:A..:Z)].map(&:to_s) unless options[:case_sensitive]
|
8
|
+
dict -= uniqueness_ambigious_dictionary if options[:type].to_sym == :human
|
9
|
+
Array.new(options[:length]).map { dict[rand(dict.length)] }.join
|
10
|
+
end
|
11
|
+
|
12
|
+
# Dictionary used for uniqueness generation
|
13
|
+
def uniqueness_dictionary
|
14
|
+
[*(:A..:Z), *(:a..:z), *(0..9)].map(&:to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
def uniqueness_ambigious_dictionary
|
18
|
+
[:b, :B, :o, :O, :q, :i, :I, :l, :L, :s, :S, :u, :U, :z, :Z, :g, 1, 2, 9, 5].map(&:to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Default sorting options
|
22
|
+
def uniqueness_default_options
|
23
|
+
{
|
24
|
+
length: 32,
|
25
|
+
type: :auto,
|
26
|
+
blacklist: [],
|
27
|
+
scope: []
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/uniqueness/model.rb
CHANGED
@@ -11,7 +11,7 @@ module Uniqueness
|
|
11
11
|
#
|
12
12
|
# Examples:
|
13
13
|
# To auto-generate a new random string for field +foo+
|
14
|
-
#
|
14
|
+
# has_unique_field :foo
|
15
15
|
#
|
16
16
|
# You can customize the generated string by
|
17
17
|
# passing an options hash. The following keys are supported:
|
@@ -31,20 +31,10 @@ module Uniqueness
|
|
31
31
|
# defaults to <tt>[]</tt>
|
32
32
|
def has_unique_field(name, options = {})
|
33
33
|
self.uniqueness_options ||= {}
|
34
|
-
self.uniqueness_options[name] =
|
34
|
+
self.uniqueness_options[name] = Uniqueness.uniqueness_default_options.merge(options)
|
35
35
|
before_validation :uniqueness_generate
|
36
36
|
validate :uniqueness_validation
|
37
37
|
end
|
38
|
-
|
39
|
-
# Default sorting options
|
40
|
-
def uniqueness_default_options
|
41
|
-
{
|
42
|
-
length: 32,
|
43
|
-
type: :auto,
|
44
|
-
blacklist: [],
|
45
|
-
scope: []
|
46
|
-
}
|
47
|
-
end
|
48
38
|
end
|
49
39
|
|
50
40
|
# Generates a new code based on given options
|
@@ -52,24 +42,12 @@ module Uniqueness
|
|
52
42
|
self.uniqueness_options.each do |field, options|
|
53
43
|
value = send(field)
|
54
44
|
unless value.present?
|
55
|
-
|
56
|
-
dict -= [*(:A..:Z)].map(&:to_s) unless options[:case_sensitive]
|
57
|
-
dict -= uniqueness_ambigious_dictionary if options[:type].to_sym == :human
|
58
|
-
value = Array.new(options[:length]).map { dict[rand(dict.length)] }.join
|
45
|
+
value = Uniqueness.generate(options)
|
59
46
|
self.send("#{field}=", value)
|
60
47
|
end
|
61
48
|
end
|
62
49
|
end
|
63
50
|
|
64
|
-
# Dictionary used for uniqueness generation
|
65
|
-
def uniqueness_dictionary
|
66
|
-
[*(:A..:Z), *(:a..:z), *(0..9)].map(&:to_s)
|
67
|
-
end
|
68
|
-
|
69
|
-
def uniqueness_ambigious_dictionary
|
70
|
-
[:b, :B, :o, :O, :q, :i, :I, :l, :L, :s, :S, :u, :U, :z, :Z, :g, 1, 2, 9, 5].map(&:to_s)
|
71
|
-
end
|
72
|
-
|
73
51
|
def uniqueness_validation
|
74
52
|
self.class.uniqueness_options.each do |field, options|
|
75
53
|
value = send(field)
|
data/lib/uniqueness/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uniqueness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Abdel-Wahab
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- gemfiles/50.gemfile.lock
|
153
153
|
- lib/uniqueness.rb
|
154
154
|
- lib/uniqueness/engine.rb
|
155
|
+
- lib/uniqueness/generator.rb
|
155
156
|
- lib/uniqueness/model.rb
|
156
157
|
- lib/uniqueness/version.rb
|
157
158
|
- uniqueness.gemspec
|
@@ -175,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
176
|
version: '0'
|
176
177
|
requirements: []
|
177
178
|
rubyforge_project:
|
178
|
-
rubygems_version: 2.
|
179
|
+
rubygems_version: 2.6.12
|
179
180
|
signing_key:
|
180
181
|
specification_version: 4
|
181
182
|
summary: Adds unique attribute support to ActiveModel models.
|