has_unique_identifier 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8297a453b96808f1f92e967cff6c8ae331822c27
4
- data.tar.gz: 247efc8edb1cfcdda610da4f2b58542e68ea0861
2
+ SHA256:
3
+ metadata.gz: 7825723098ab566018574a086305673bbd077790f29dac349f5728c89bb97bb1
4
+ data.tar.gz: 9565c5c0639109eb07b76f7da47a4f21fbba57911e982f9de45542fc8ecf61e2
5
5
  SHA512:
6
- metadata.gz: d2d4579fcc72ea6ef4c8675f0c5569b575f1ed5e1661b05794389ddb48471030fab903e1b6766a37590a61fbe908491595bcf0aa3fd25437b97156270fb3bac9
7
- data.tar.gz: e9256637898f776c2e020b11f84aa3b0b61dc04268cad5e1ae1ca253037822f5103763fd80d4f3df281b6c529b48eb3e2f68895593a588ecb971cffd4436a3be
6
+ metadata.gz: d094d4019f90a9ff5fdd557380f459bede65a1fe93e09282f0d0de773057abe5417c18aa5df1acd09c0055987d41dde22fb10201cbc3dfdee2e7c17845e40678
7
+ data.tar.gz: 7cc010860c47131c6b5d084ea6822159763236c554880d2ca492ffbac4600038e1d0b39ae86837a36047ec873d7ceb7cefaaba02724d9a0548139f51b8207d85
data/gems.locked ADDED
@@ -0,0 +1,57 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_unique_identifier (0.2.0)
5
+ activemodel (>= 4.0)
6
+ activesupport (>= 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.5)
12
+ activesupport (= 5.1.5)
13
+ activesupport (5.1.5)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (~> 0.7)
16
+ minitest (~> 5.1)
17
+ tzinfo (~> 1.1)
18
+ coderay (1.1.2)
19
+ concurrent-ruby (1.0.5)
20
+ diff-lcs (1.3)
21
+ i18n (0.9.5)
22
+ concurrent-ruby (~> 1.0)
23
+ method_source (0.9.0)
24
+ minitest (5.11.3)
25
+ pry (0.11.3)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
28
+ rake (10.5.0)
29
+ rspec (3.7.0)
30
+ rspec-core (~> 3.7.0)
31
+ rspec-expectations (~> 3.7.0)
32
+ rspec-mocks (~> 3.7.0)
33
+ rspec-core (3.7.1)
34
+ rspec-support (~> 3.7.0)
35
+ rspec-expectations (3.7.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.7.0)
38
+ rspec-mocks (3.7.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.7.0)
41
+ rspec-support (3.7.1)
42
+ thread_safe (0.3.6)
43
+ tzinfo (1.2.5)
44
+ thread_safe (~> 0.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ bundler (~> 1.10)
51
+ has_unique_identifier!
52
+ pry
53
+ rake (~> 10.0)
54
+ rspec
55
+
56
+ BUNDLED WITH
57
+ 1.16.1
File without changes
@@ -23,5 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec"
25
25
 
26
- spec.add_dependency 'rails', '>= 4.0'
26
+ spec.add_dependency 'activesupport', '>= 4.0'
27
+ spec.add_dependency 'activemodel' , '>= 4.0'
27
28
  end
@@ -1,7 +1,10 @@
1
1
  module HasUniqueIdentifier
2
2
  module ClassMethods
3
3
  def unique_identifier
4
- @unique_identifier
4
+ return @unique_identifier if @unique_identifier
5
+ if superclass.respond_to?(:unique_identifier)
6
+ return superclass.unique_identifier
7
+ end
5
8
  end
6
9
 
7
10
  def has_unique_identifier(name, opts = {})
@@ -1,8 +1,14 @@
1
1
  module HasUniqueIdentifier
2
2
  module InstanceMethods
3
3
  DEFAULT_RANDOM_IDENTIFIER_LENGTH = 24
4
+ DEFAULT_SAFE_CHARS = (
5
+ ('A'..'Z').to_a +
6
+ ('a'..'z').to_a +
7
+ ('0'..'9').to_a +
8
+ %w[- _ ~]
9
+ ).freeze
4
10
 
5
- extend ActiveSupport::Concern
11
+ extend ::ActiveSupport::Concern
6
12
 
7
13
  def to_param
8
14
  if self.class.unique_identifier &&
@@ -14,7 +20,7 @@ module HasUniqueIdentifier
14
20
  end
15
21
 
16
22
  included do
17
- before_create :ensure_unique_identifier
23
+ before_validation :ensure_unique_identifier
18
24
 
19
25
  private
20
26
 
@@ -25,16 +31,29 @@ module HasUniqueIdentifier
25
31
  length = self.class.unique_identifier.options[:length] ||
26
32
  DEFAULT_RANDOM_IDENTIFIER_LENGTH
27
33
 
34
+ unless self.class.unique_identifier.options[:force]
35
+ return if self[self.class.unique_identifier.name].present?
36
+ end
28
37
  send("#{self.class.unique_identifier.name}=",
29
38
  loop do
30
- hash =
31
- if self.class.unique_identifier.options[:only_numbers]
32
- number_size = 10 ** length
33
- SecureRandom.random_number(9 * number_size) + number_size
39
+ chars =
40
+ if self.class.unique_identifier.options[:safe_chars]
41
+ self.class.unique_identifier.options[:safe_chars].dup
42
+ elsif self.class.unique_identifier.options[:only_numbers]
43
+ (0..9).to_a
44
+ elsif self.class.unique_identifier.options[:only_letters]
45
+ ('a'..'z').to_a + ('A'..'Z').to_a
46
+ elsif self.class.unique_identifier.options[:no_symbols]
47
+ ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
34
48
  else
35
- SecureRandom.hex(length)
49
+ DEFAULT_SAFE_CHARS.dup
36
50
  end
37
- break hash unless self.class.exists?(self.class.unique_identifier.name => hash)
51
+ chars.shuffle!
52
+
53
+ hash = Array.new(length) { chars.sample }.join
54
+ unless self.class.exists?(self.class.unique_identifier.name => hash.to_s)
55
+ break hash
56
+ end
38
57
  end
39
58
  )
40
59
  end
@@ -3,9 +3,9 @@ require 'rails/railtie'
3
3
  module HasUniqueIdentifier
4
4
  class Railtie < Rails::Railtie
5
5
 
6
- initializer "has_unique_identifier.setup_orm" do |app|
6
+ initializer 'has_unique_identifier.setup_orm' do |app|
7
7
  [:active_record, :sequel].each do |orm|
8
- ActiveSupport.on_load orm do
8
+ ::ActiveSupport.on_load orm do
9
9
  HasUniqueIdentifier.setup_orm self
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module HasUniqueIdentifier
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,3 +1,6 @@
1
+ require 'active_support'
2
+ require 'active_model'
3
+
1
4
  require 'has_unique_identifier/version'
2
5
  require 'has_unique_identifier/class_methods'
3
6
  require 'has_unique_identifier/instance_methods'
@@ -5,10 +8,8 @@ require 'has_unique_identifier/instance_methods'
5
8
  require_relative 'has_unique_identifier/railtie' if defined? Rails
6
9
 
7
10
  module HasUniqueIdentifier
8
- def self.setup_orm(base)
9
- base.class_eval do
10
- extend ClassMethods
11
- include InstanceMethods
12
- end
11
+ def self.included(base)
12
+ base.send :extend, ClassMethods
13
+ base.send :include, InstanceMethods
13
14
  end
14
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_unique_identifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Besedin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-06 00:00:00.000000000 Z
11
+ date: 2018-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -53,7 +53,21 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rails
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activemodel
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -76,12 +90,13 @@ files:
76
90
  - ".gitignore"
77
91
  - ".rspec"
78
92
  - ".travis.yml"
79
- - Gemfile
80
93
  - LICENSE
81
94
  - README.md
82
95
  - Rakefile
83
96
  - bin/console
84
97
  - bin/setup
98
+ - gems.locked
99
+ - gems.rb
85
100
  - has_unique_identifier.gemspec
86
101
  - lib/has_unique_identifier.rb
87
102
  - lib/has_unique_identifier/class_methods.rb
@@ -108,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
123
  version: '0'
109
124
  requirements: []
110
125
  rubyforge_project:
111
- rubygems_version: 2.4.5
126
+ rubygems_version: 2.7.3
112
127
  signing_key:
113
128
  specification_version: 4
114
129
  summary: Make random unique identifiers with ease