visibilize 0.1.1 → 0.1.2

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
  SHA256:
3
- metadata.gz: 84568a8140b7e27087461d5707673a258efad3d5dc8fdd5305eca9516398f8a3
4
- data.tar.gz: 902201dd9774ab0f5e0b971f3125a4b9840193bff28cf0bacfa77fd618b7fa8d
3
+ metadata.gz: 80a09f94dca61143ddf4d2552946629a5d4af1dde248b5c98f8bdccb5f7657ff
4
+ data.tar.gz: c7be6a3803139cc8121528f4438c4e1135e63070c91549fd9a0fd2f808b4e11e
5
5
  SHA512:
6
- metadata.gz: 4ddf68adcb0fa813ed29ed03553b8dd8cdb04cd369cc860d67ac33432c3003b236e0c24d89645a5b08599a613a533dfb773ba0240348118f38e1d9e0f57f1732
7
- data.tar.gz: 1ca2048a188fa08798e2e637bb29b5a82d81be4f80fe50da3050e7284d3b76cf90cfd288aea4f898cfba5624513045a5174005cac0780fa20bc4d50aa7e964b0
6
+ metadata.gz: 19cf227b9d94edc6e5fef2a2d383a6c323703ecd530ef72727bb9841b26412da616b0b2d5d52c7b867ca27bbd6a99a09d92b3ba2782e32a27a9af80bf71f5bf7
7
+ data.tar.gz: 371a1d72ffb2ece0ed672c52798368685d223ce5e58ee05bde0befc2a5f314c5d2d8212661143f47db9ec18a0f213d0e1d4ebdc0c83ead93a96a3c187d04badb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.0.0
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in visibilize.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'rubocop', '~> 1.65.1'
8
+ end
data/Gemfile.lock CHANGED
@@ -1,59 +1,97 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- visibilize (0.1.1)
4
+ visibilize (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- activemodel (6.0.3.2)
10
- activesupport (= 6.0.3.2)
11
- activerecord (6.0.3.2)
12
- activemodel (= 6.0.3.2)
13
- activesupport (= 6.0.3.2)
14
- activesupport (6.0.3.2)
9
+ activemodel (7.1.4)
10
+ activesupport (= 7.1.4)
11
+ activerecord (7.1.4)
12
+ activemodel (= 7.1.4)
13
+ activesupport (= 7.1.4)
14
+ timeout (>= 0.4.0)
15
+ activesupport (7.1.4)
16
+ base64
17
+ bigdecimal
15
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
- i18n (>= 0.7, < 2)
17
- minitest (~> 5.1)
18
- tzinfo (~> 1.1)
19
- zeitwerk (~> 2.2, >= 2.2.2)
20
- concurrent-ruby (1.1.7)
21
- diff-lcs (1.3)
22
- i18n (1.8.5)
19
+ connection_pool (>= 2.2.5)
20
+ drb
21
+ i18n (>= 1.6, < 2)
22
+ minitest (>= 5.1)
23
+ mutex_m
24
+ tzinfo (~> 2.0)
25
+ ast (2.4.2)
26
+ base64 (0.2.0)
27
+ bigdecimal (3.1.8)
28
+ concurrent-ruby (1.3.4)
29
+ connection_pool (2.4.1)
30
+ diff-lcs (1.5.1)
31
+ drb (2.2.1)
32
+ i18n (1.14.5)
23
33
  concurrent-ruby (~> 1.0)
24
- minitest (5.14.1)
25
- mysql2 (0.5.3)
34
+ json (2.7.2)
35
+ language_server-protocol (3.17.0.3)
36
+ mini_portile2 (2.8.7)
37
+ minitest (5.25.1)
38
+ mutex_m (0.2.0)
39
+ parallel (1.26.3)
40
+ parser (3.3.4.2)
41
+ ast (~> 2.4.1)
42
+ racc
43
+ racc (1.8.1)
44
+ rainbow (3.1.1)
26
45
  rake (12.3.3)
27
- rspec (3.8.0)
28
- rspec-core (~> 3.8.0)
29
- rspec-expectations (~> 3.8.0)
30
- rspec-mocks (~> 3.8.0)
31
- rspec-core (3.8.2)
32
- rspec-support (~> 3.8.0)
33
- rspec-expectations (3.8.4)
46
+ regexp_parser (2.9.2)
47
+ rexml (3.3.6)
48
+ strscan
49
+ rspec (3.13.0)
50
+ rspec-core (~> 3.13.0)
51
+ rspec-expectations (~> 3.13.0)
52
+ rspec-mocks (~> 3.13.0)
53
+ rspec-core (3.13.0)
54
+ rspec-support (~> 3.13.0)
55
+ rspec-expectations (3.13.2)
34
56
  diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.8.0)
36
- rspec-mocks (3.8.1)
57
+ rspec-support (~> 3.13.0)
58
+ rspec-mocks (3.13.1)
37
59
  diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.8.0)
39
- rspec-support (3.8.2)
40
- thread_safe (0.3.6)
41
- tzinfo (1.2.7)
42
- thread_safe (~> 0.1)
43
- yaml (0.1.0)
44
- zeitwerk (2.4.0)
60
+ rspec-support (~> 3.13.0)
61
+ rspec-support (3.13.1)
62
+ rubocop (1.65.1)
63
+ json (~> 2.3)
64
+ language_server-protocol (>= 3.17.0)
65
+ parallel (~> 1.10)
66
+ parser (>= 3.3.0.2)
67
+ rainbow (>= 2.2.2, < 4.0)
68
+ regexp_parser (>= 2.4, < 3.0)
69
+ rexml (>= 3.2.5, < 4.0)
70
+ rubocop-ast (>= 1.31.1, < 2.0)
71
+ ruby-progressbar (~> 1.7)
72
+ unicode-display_width (>= 2.4.0, < 3.0)
73
+ rubocop-ast (1.32.1)
74
+ parser (>= 3.3.1.0)
75
+ ruby-progressbar (1.13.0)
76
+ sqlite3 (1.7.3)
77
+ mini_portile2 (~> 2.8.0)
78
+ strscan (3.1.0)
79
+ timeout (0.4.1)
80
+ tzinfo (2.0.6)
81
+ concurrent-ruby (~> 1.0)
82
+ unicode-display_width (2.5.0)
45
83
 
46
84
  PLATFORMS
47
85
  ruby
48
86
 
49
87
  DEPENDENCIES
50
- activerecord (~> 6.0)
88
+ activerecord (~> 7.0)
51
89
  bundler (~> 2.0)
52
- mysql2 (~> 0.5.3)
53
90
  rake (~> 12.3.3)
54
91
  rspec (~> 3.0)
92
+ rubocop (~> 1.65.1)
93
+ sqlite3 (~> 1.4)
55
94
  visibilize!
56
- yaml (~> 0.1.0)
57
95
 
58
96
  BUNDLED WITH
59
- 2.1.4
97
+ 2.2.22
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ # ⚠️ This is a very old and unmaintaned repository, refer to [friendly_id](https://github.com/norman/friendly_id) for a better solution.
1
2
 
2
3
 
3
4
  # Visibilize
@@ -1,72 +1,68 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'securerandom'
2
4
 
5
+ # Handles value generation for Visibilize w.r.t. provided type argument
3
6
  module VisibilizeGenerator
7
+ # Raised when the generator is not found for the given type
8
+ class InvalidGeneratorError < StandardError
9
+ def initialize(type, klass, column)
10
+ super("Visibilize Error: No generator defined for type #{type}. (Asked for column #{column} of #{klass.name})")
11
+ end
12
+ end
13
+
14
+ # Raised when the maximum number of attempts to generate a unique value is exceeded
15
+ class AttemptsExceededError < StandardError
16
+ def initialize(attempts)
17
+ super("Visibilize Error: Failed to generate a unique value after #{attempts} attempts")
18
+ end
19
+ end
4
20
 
5
21
  class << self
22
+ # We're limiting the number of attempts to generate a unique value to 10
23
+ # This is to prevent infinite loops in case of collisions
24
+ MAX_UNIQUE_ATTEMPTS = 10
6
25
 
7
- def get_value_for(type, klass, column, length, unique)
8
- method="generate_#{type}"
9
-
10
-
11
- if self.respond_to?(method)
12
- from=:generators
13
- elsif SecureRandom.respond_to?(type)
14
- from=:securerandom
15
- else
16
- raise "Visibilize Error: No generator defined for type #{type}. (Asked for column #{column} of #{klass.name})"
26
+ def value(type, klass, column, length, unique)
27
+ return generate_value(type, klass, column, length) unless unique
28
+
29
+ MAX_UNIQUE_ATTEMPTS.times do
30
+ generated = generate_value(type, klass, column, length)
31
+ return generated unless klass.exists?(column => generated)
17
32
  end
33
+ raise AttemptsExceededError, MAX_UNIQUE_ATTEMPTS
34
+ end
18
35
 
19
- loop do
20
- if from==:generators
21
- generated=public_send(method, length)
22
- elsif from==:securerandom
23
- generated=generate_from_securerandom(type, length)
24
- end
36
+ def generate_value(type, klass, column, length)
37
+ case type
38
+ when :integer then generate_integer(length)
39
+ when :string then generate_string(length)
40
+ else
41
+ raise InvalidGeneratorError.new(type, klass, column) unless SecureRandom.respond_to?(type)
25
42
 
26
- return generated if !unique || klass.where("#{column}='#{generated}'").empty?
43
+ generate_from_securerandom(type, length)
27
44
  end
28
-
29
45
  end
30
- # end get_value_for
31
46
 
32
-
33
- #
34
- # Generators
35
- #
47
+ private
36
48
 
37
49
  def generate_integer(length)
38
- return rand(1...10) if length==1
50
+ return rand(1...10) if length == 1
39
51
 
40
- min=10**(length-1) #1000
41
- max=9*min + (min-1) #9999
42
- rand(min...(max+1))
52
+ min = 10**(length - 1)
53
+ max = (10**length) - 1
54
+ rand(min..max)
43
55
  end
44
56
 
45
57
  def generate_string(length)
46
- available_chars=("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a
47
-
48
- str=""
49
- length.times do
50
- str+=available_chars[rand(available_chars.size)].to_s
51
- end
52
-
53
- str
58
+ available_chars = [*'a'..'z', *'A'..'Z', *'0'..'9']
59
+ Array.new(length) { available_chars.sample }.join
54
60
  end
55
61
 
56
62
  def generate_from_securerandom(type, length)
57
- if SecureRandom.respond_to?(type)
58
- begin
59
- return SecureRandom.public_send(type, length)
60
- rescue ArgumentError
61
- return SecureRandom.public_send(type)
62
- end
63
- end
64
-
65
- return nil
63
+ SecureRandom.public_send(type, length)
64
+ rescue ArgumentError
65
+ SecureRandom.public_send(type)
66
66
  end
67
-
68
-
69
67
  end
70
- # end static
71
-
72
- end
68
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Visibilize
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.2'
3
5
  end
data/lib/visibilize.rb CHANGED
@@ -1,35 +1,44 @@
1
- require "visibilize/version"
2
- require "visibilize/generator"
1
+ # frozen_string_literal: true
3
2
 
3
+ require 'visibilize/version'
4
+ require 'visibilize/generator'
4
5
 
6
+ # Raised when the column is not found in the model
7
+ class AttributeNotFoundError < StandardError
8
+ def initialize(column, klass)
9
+ super("Visibilize Error: Attribute #{column} not found in #{klass.name} instance.")
10
+ end
11
+ end
5
12
 
6
- def visibilize(options={})
7
-
8
- column = options[:column] || :visible_id
9
- callback = options[:callback] || :before_create
10
- type = options[:type] || :integer
11
- unique = options[:unique] || true
12
- length = options[:length] || 8
13
- lamb = options[:lambda] || nil
14
-
15
- # Define a method to fill the column with appropiate value
16
- method_name="visibilize_#{column}"
17
- define_method(method_name) do
13
+ # Visibilize
14
+ #
15
+ # @param [Hash] options
16
+ # @option options [Symbol] :column The column to be generated
17
+ # @option options [Symbol] :callback The callback to be used (before_create, before_save, etc.)
18
+ # @option options [Symbol] :type The type of value to be generated (integer, string, uuid, etc.)
19
+ # @option options [Boolean] :unique Whether the value should be unique
20
+ # @option options [Integer] :length The length of the value to be generated
21
+ # @option options [Proc] :lambda A lambda to be used to generate the value
22
+ def visibilize(options = {})
23
+ options.reverse_merge!(column: :visible_id, callback: :before_create, type: :integer, unique: true, length: 8)
24
+
25
+ # Define a method to fill the column with an appropiate value
26
+ define_visibilize_method(**options.slice(:column, :type, :unique, :length, :lambda))
18
27
 
19
- raise "Visibilize Error: Attribute #{column} not found in #{self.class.name} instance." unless self.has_attribute?(column)
28
+ # Create a callback to execute method
29
+ callback_type, _, callback_timing = options[:callback].to_s.partition('_')
30
+ set_callback callback_timing.to_sym, callback_type.to_sym, method_name(options[:column])
31
+ end
20
32
 
21
- # Values are generated by VisibilizeGenerator class
22
- value=lamb ? lamb.call : VisibilizeGenerator.get_value_for(type, self.class, column, length, unique)
33
+ def define_visibilize_method(column: :visible_id, type: :integer, unique: true, length: 8, lambda: nil)
34
+ define_method(method_name(column)) do
35
+ raise AttributeNotFoundError.new(column, self.class) unless has_attribute?(column)
23
36
 
37
+ value = lambda&.call || VisibilizeGenerator.value(type, self.class, column, length, unique)
24
38
  write_attribute(column, value)
25
39
  end
26
-
27
- # Create a callback to execute method
28
- callback=callback.to_s.split('_')
29
- set_callback callback[1].to_sym, callback[0].to_sym, method_name.to_sym
30
-
31
40
  end
32
- # end visibilize
33
-
34
-
35
41
 
42
+ def method_name(column)
43
+ "visibilize_#{column}".to_sym
44
+ end
data/visibilize.gemspec CHANGED
@@ -1,6 +1,8 @@
1
- lib = File.expand_path("lib", __dir__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "visibilize/version"
5
+ require 'visibilize/version'
4
6
 
5
7
  Gem::Specification.new do |spec|
6
8
  spec.name = 'visibilize'
@@ -8,26 +10,26 @@ Gem::Specification.new do |spec|
8
10
  spec.authors = ['Furkan Enes Apaydın']
9
11
  spec.email = ['feapaydin@gmail.com']
10
12
 
11
- spec.summary = %q{Generate random friendly identifiers for ActiveRecord instances.}
12
- spec.description = %q{Visibilize generates random friendly identifiers (visible_id) that can be exposed to end users for ActiveRecord models. }
13
- spec.homepage = 'https://github.com/FEApaydin/visibilize'
13
+ spec.summary = 'Generate random friendly identifiers for ActiveRecord instances.'
14
+ spec.description = 'Visibilize generates random friendly identifiers that can be exposed to end users for ActiveRecord models.'
15
+ spec.homepage = 'https://github.com/feapaydin/visibilize'
14
16
  spec.license = 'MIT'
15
17
 
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = 'https://github.com/FEApaydin/visibilize'
18
- spec.metadata["changelog_uri"] = 'https://github.com/FEApaydin/visibilize'
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata['source_code_uri'] = 'https://github.com/feapaydin/visibilize'
20
+ spec.metadata['changelog_uri'] = 'https://github.com/feapaydin/visibilize'
19
21
 
20
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
23
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
24
  end
23
25
  spec.bindir = 'exe'
24
26
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
27
  spec.require_paths = ['lib']
28
+ spec.required_ruby_version = '>= 3.0'
26
29
 
30
+ spec.add_development_dependency 'activerecord', '~> 7.0'
27
31
  spec.add_development_dependency 'bundler', '~> 2.0'
28
32
  spec.add_development_dependency 'rake', '~> 12.3.3'
29
33
  spec.add_development_dependency 'rspec', '~> 3.0'
30
- spec.add_development_dependency 'activerecord', '~> 6.0'
31
- spec.add_development_dependency 'mysql2', '~> 0.5.3'
32
- spec.add_development_dependency 'yaml', '~> 0.1.0'
34
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
33
35
  end
metadata CHANGED
@@ -1,101 +1,87 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: visibilize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Furkan Enes Apaydın
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-19 00:00:00.000000000 Z
11
+ date: 2024-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.0'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
14
+ name: activerecord
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: 12.3.3
19
+ version: '7.0'
34
20
  type: :development
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: 12.3.3
26
+ version: '7.0'
41
27
  - !ruby/object:Gem::Dependency
42
- name: rspec
28
+ name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
31
  - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '3.0'
33
+ version: '2.0'
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
38
  - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '3.0'
40
+ version: '2.0'
55
41
  - !ruby/object:Gem::Dependency
56
- name: activerecord
42
+ name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '6.0'
47
+ version: 12.3.3
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '6.0'
54
+ version: 12.3.3
69
55
  - !ruby/object:Gem::Dependency
70
- name: mysql2
56
+ name: rspec
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - "~>"
74
60
  - !ruby/object:Gem::Version
75
- version: 0.5.3
61
+ version: '3.0'
76
62
  type: :development
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: 0.5.3
68
+ version: '3.0'
83
69
  - !ruby/object:Gem::Dependency
84
- name: yaml
70
+ name: sqlite3
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: 0.1.0
75
+ version: '1.4'
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
80
  - - "~>"
95
81
  - !ruby/object:Gem::Version
96
- version: 0.1.0
97
- description: 'Visibilize generates random friendly identifiers (visible_id) that can
98
- be exposed to end users for ActiveRecord models. '
82
+ version: '1.4'
83
+ description: Visibilize generates random friendly identifiers that can be exposed
84
+ to end users for ActiveRecord models.
99
85
  email:
100
86
  - feapaydin@gmail.com
101
87
  executables: []
@@ -104,6 +90,7 @@ extra_rdoc_files: []
104
90
  files:
105
91
  - ".gitignore"
106
92
  - ".rspec"
93
+ - ".ruby-version"
107
94
  - ".travis.yml"
108
95
  - CODE_OF_CONDUCT.md
109
96
  - Gemfile
@@ -117,13 +104,13 @@ files:
117
104
  - lib/visibilize/generator.rb
118
105
  - lib/visibilize/version.rb
119
106
  - visibilize.gemspec
120
- homepage: https://github.com/FEApaydin/visibilize
107
+ homepage: https://github.com/feapaydin/visibilize
121
108
  licenses:
122
109
  - MIT
123
110
  metadata:
124
- homepage_uri: https://github.com/FEApaydin/visibilize
125
- source_code_uri: https://github.com/FEApaydin/visibilize
126
- changelog_uri: https://github.com/FEApaydin/visibilize
111
+ homepage_uri: https://github.com/feapaydin/visibilize
112
+ source_code_uri: https://github.com/feapaydin/visibilize
113
+ changelog_uri: https://github.com/feapaydin/visibilize
127
114
  post_install_message:
128
115
  rdoc_options: []
129
116
  require_paths:
@@ -132,14 +119,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
119
  requirements:
133
120
  - - ">="
134
121
  - !ruby/object:Gem::Version
135
- version: '0'
122
+ version: '3.0'
136
123
  required_rubygems_version: !ruby/object:Gem::Requirement
137
124
  requirements:
138
125
  - - ">="
139
126
  - !ruby/object:Gem::Version
140
127
  version: '0'
141
128
  requirements: []
142
- rubygems_version: 3.1.2
129
+ rubygems_version: 3.2.22
143
130
  signing_key:
144
131
  specification_version: 4
145
132
  summary: Generate random friendly identifiers for ActiveRecord instances.