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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b043ebfc469376d47e1a60af8647a94425a38b0
4
- data.tar.gz: 80a5d65068bf1ab36a2a0c5ced5cd3a65acf217a
3
+ metadata.gz: 99d56bdd5db29acb11bcd1259aafe34004684f88
4
+ data.tar.gz: 6cea06eaa400b0ee4c1c96aaa765aec6a5912288
5
5
  SHA512:
6
- metadata.gz: 8510e0540577c7839ea96bd06b95cc527c5fa16e260c47236d164970dbf652a36fb668d9af0d383d0d8a9d2c394c0662bfe67912f4a079aa466be0d133922a1a
7
- data.tar.gz: 0f53b82f825eca3eb034225a3c20f1ebd653a1572f055ce25063ed5e5af8ec7ebe5a2151e7a63dd160732af0f785ae573ed5a80c0237c2868bdbdc13f9b57638
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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: ../
2
+ remote: ..
3
3
  specs:
4
- uniqueness (0.5.0)
4
+ uniqueness (0.7.0)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -121,4 +121,4 @@ DEPENDENCIES
121
121
  uniqueness!
122
122
 
123
123
  BUNDLED WITH
124
- 1.11.2
124
+ 1.14.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: ../
2
+ remote: ..
3
3
  specs:
4
- uniqueness (0.5.0)
4
+ uniqueness (0.7.0)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -125,4 +125,4 @@ DEPENDENCIES
125
125
  uniqueness!
126
126
 
127
127
  BUNDLED WITH
128
- 1.11.2
128
+ 1.14.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: ../
2
+ remote: ..
3
3
  specs:
4
- uniqueness (0.5.0)
4
+ uniqueness (0.7.0)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -150,4 +150,4 @@ DEPENDENCIES
150
150
  uniqueness!
151
151
 
152
152
  BUNDLED WITH
153
- 1.11.2
153
+ 1.14.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
- remote: ../
2
+ remote: ..
3
3
  specs:
4
- uniqueness (0.5.0)
4
+ uniqueness (0.7.0)
5
5
  activerecord (>= 4.0.0)
6
6
  railties (>= 4.0.0)
7
7
 
@@ -160,4 +160,4 @@ DEPENDENCIES
160
160
  uniqueness!
161
161
 
162
162
  BUNDLED WITH
163
- 1.11.2
163
+ 1.14.6
@@ -1,5 +1,6 @@
1
1
  require 'rails/engine'
2
2
  require 'active_record'
3
3
  require 'uniqueness/version'
4
+ require 'uniqueness/generator'
4
5
  require 'uniqueness/model'
5
6
  require 'uniqueness/engine'
@@ -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
@@ -11,7 +11,7 @@ module Uniqueness
11
11
  #
12
12
  # Examples:
13
13
  # To auto-generate a new random string for field +foo+
14
- # has_random_field :foo
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] = self.uniqueness_default_options.merge(options)
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
- dict = uniqueness_dictionary - options[:blacklist]
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)
@@ -1,3 +1,3 @@
1
1
  module Uniqueness
2
- VERSION = '0.6.1'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
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.6.1
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: 2016-11-10 00:00:00.000000000 Z
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.5.1
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.