lite-uxid 1.1.3 → 1.2.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
  SHA256:
3
- metadata.gz: 6f8006571de0a1f66c804d3c3e5fd9aeab60be7ef80d5e04b30d08cdee9a4426
4
- data.tar.gz: 6ec85db8db509ff3fc06aafc2a3095aba7761bf6688923bbff25f7167b1daf97
3
+ metadata.gz: 8119c92432950cdd4fd849aafd041832a8f4ceee022c7f38009681726bb1181a
4
+ data.tar.gz: 121d26ff5203b65b8a4b148f9f82aad5585c4baeae6ae1008e71cc057d6fc1f8
5
5
  SHA512:
6
- metadata.gz: 185195e88da652756f517b592c274b07737309f42fe6dd7a10701e3677629c224e4247f20d2e51706f933ee4153635af06f39169e1ec0eaedc6db6cc2f66c585
7
- data.tar.gz: '0686eed4e263e0719107af86fb9aa3c55acb82ddcd30694a1fbcac6d5d5b383e2b365b1f17c29be60deb76717b8e6734d31a052c0fa75e799e53c8a69b5026bf'
6
+ metadata.gz: 4ed1ddf45549eef5ee6b6950b000272b8d62dfa8ac5b403fa5e6ffc70dfabd9a03978a7e96259184b3222b42e3b1dc240e44984ee89086cfe3f396e7fcfa3d8c
7
+ data.tar.gz: c63df74d2affbda2f3fefc86a0b5d44364dfef7417b22c6c56f7d411808d3935d1b26f06de92d8699f3ababaf8c84a9e3a0149123a3ed66d52e92bf59f064192
data/.rubocop.yml CHANGED
@@ -3,7 +3,6 @@ require:
3
3
  - rubocop-rake
4
4
  - rubocop-rspec
5
5
  AllCops:
6
- TargetRubyVersion: 3.0
7
6
  NewCops: enable
8
7
  DisplayCopNames: true
9
8
  DisplayStyleGuide: true
@@ -19,7 +18,7 @@ Layout/EmptyLinesAroundClassBody:
19
18
  Layout/EmptyLinesAroundModuleBody:
20
19
  EnforcedStyle: empty_lines_except_namespace
21
20
  Layout/LineLength:
22
- Max: 100
21
+ Enabled: false
23
22
  Layout/SpaceAroundMethodCallOperator:
24
23
  Enabled: true
25
24
  Lint/RaiseException:
@@ -30,6 +29,8 @@ Metrics/BlockLength:
30
29
  Exclude:
31
30
  - 'spec/**/**/*'
32
31
  - '*.gemspec'
32
+ Metrics/MethodLength:
33
+ Enabled: false
33
34
  RSpec/MultipleExpectations:
34
35
  Enabled: false
35
36
  Style/ArgumentsForwarding:
@@ -38,5 +39,9 @@ Style/Documentation:
38
39
  Enabled: false
39
40
  Style/ExpandPathArguments:
40
41
  Enabled: false
42
+ Style/HashSyntax:
43
+ EnforcedShorthandSyntax: never
41
44
  Style/SlicingWithRange:
42
45
  Enabled: false
46
+ Style/StringLiterals:
47
+ EnforcedStyle: double_quotes
data/CHANGELOG.md CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.2.0] - 2022-11-19
10
+ ### Added
11
+ - Added individual character and length options
12
+ ### Changed
13
+ - Improved docs
14
+ - Improved internal setup
15
+ - Improved rubocop setup
16
+
9
17
  ## [1.1.3] - 2021-07-30
10
18
  ### Changed
11
19
  - Skip uxid callback on records where its being assigned
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- source 'https://rubygems.org'
3
+ source "https://rubygems.org"
4
4
 
5
5
  # Specify your gem's dependencies in lite-uxid.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,39 +1,38 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-uxid (1.1.3)
4
+ lite-uxid (1.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- actionpack (6.1.4)
10
- actionview (= 6.1.4)
11
- activesupport (= 6.1.4)
12
- rack (~> 2.0, >= 2.0.9)
9
+ actionpack (7.0.4)
10
+ actionview (= 7.0.4)
11
+ activesupport (= 7.0.4)
12
+ rack (~> 2.0, >= 2.2.0)
13
13
  rack-test (>= 0.6.3)
14
14
  rails-dom-testing (~> 2.0)
15
15
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
16
- actionview (6.1.4)
17
- activesupport (= 6.1.4)
16
+ actionview (7.0.4)
17
+ activesupport (= 7.0.4)
18
18
  builder (~> 3.1)
19
19
  erubi (~> 1.4)
20
20
  rails-dom-testing (~> 2.0)
21
21
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
22
- activemodel (6.1.4)
23
- activesupport (= 6.1.4)
24
- activerecord (6.1.4)
25
- activemodel (= 6.1.4)
26
- activesupport (= 6.1.4)
27
- activesupport (6.1.4)
22
+ activemodel (7.0.4)
23
+ activesupport (= 7.0.4)
24
+ activerecord (7.0.4)
25
+ activemodel (= 7.0.4)
26
+ activesupport (= 7.0.4)
27
+ activesupport (7.0.4)
28
28
  concurrent-ruby (~> 1.0, >= 1.0.2)
29
29
  i18n (>= 1.6, < 2)
30
30
  minitest (>= 5.1)
31
31
  tzinfo (~> 2.0)
32
- zeitwerk (~> 2.3)
33
32
  ast (2.4.2)
34
33
  builder (3.2.4)
35
34
  colorize (0.8.1)
36
- concurrent-ruby (1.1.9)
35
+ concurrent-ruby (1.1.10)
37
36
  crass (1.0.6)
38
37
  database_cleaner (2.0.1)
39
38
  database_cleaner-active_record (~> 2.0.0)
@@ -41,89 +40,92 @@ GEM
41
40
  activerecord (>= 5.a)
42
41
  database_cleaner-core (~> 2.0.0)
43
42
  database_cleaner-core (2.0.1)
44
- diff-lcs (1.4.4)
45
- erubi (1.10.0)
46
- fasterer (0.9.0)
43
+ diff-lcs (1.5.0)
44
+ erubi (1.11.0)
45
+ fasterer (0.10.0)
47
46
  colorize (~> 0.7)
48
- ruby_parser (>= 3.14.1)
47
+ ruby_parser (>= 3.19.1)
49
48
  generator_spec (0.9.4)
50
49
  activesupport (>= 3.0.0)
51
50
  railties (>= 3.0.0)
52
- i18n (1.8.10)
51
+ i18n (1.12.0)
53
52
  concurrent-ruby (~> 1.0)
54
- loofah (2.10.0)
53
+ json (2.6.2)
54
+ loofah (2.19.0)
55
55
  crass (~> 1.0.2)
56
56
  nokogiri (>= 1.5.9)
57
57
  method_source (1.0.0)
58
- mini_portile2 (2.5.3)
59
- minitest (5.14.4)
60
- nokogiri (1.11.7)
61
- mini_portile2 (~> 2.5.0)
58
+ mini_portile2 (2.8.0)
59
+ minitest (5.16.3)
60
+ nokogiri (1.13.9)
61
+ mini_portile2 (~> 2.8.0)
62
62
  racc (~> 1.4)
63
- parallel (1.20.1)
64
- parser (3.0.2.0)
63
+ parallel (1.22.1)
64
+ parser (3.1.2.1)
65
65
  ast (~> 2.4.1)
66
- racc (1.5.2)
67
- rack (2.2.3)
68
- rack-test (1.1.0)
69
- rack (>= 1.0, < 3)
66
+ racc (1.6.0)
67
+ rack (2.2.4)
68
+ rack-test (2.0.2)
69
+ rack (>= 1.3)
70
70
  rails-dom-testing (2.0.3)
71
71
  activesupport (>= 4.2.0)
72
72
  nokogiri (>= 1.6)
73
- rails-html-sanitizer (1.3.0)
73
+ rails-html-sanitizer (1.4.3)
74
74
  loofah (~> 2.3)
75
- railties (6.1.4)
76
- actionpack (= 6.1.4)
77
- activesupport (= 6.1.4)
75
+ railties (7.0.4)
76
+ actionpack (= 7.0.4)
77
+ activesupport (= 7.0.4)
78
78
  method_source
79
- rake (>= 0.13)
79
+ rake (>= 12.2)
80
80
  thor (~> 1.0)
81
- rainbow (3.0.0)
81
+ zeitwerk (~> 2.5)
82
+ rainbow (3.1.1)
82
83
  rake (13.0.6)
83
- regexp_parser (2.1.1)
84
+ regexp_parser (2.6.1)
84
85
  rexml (3.2.5)
85
- rspec (3.10.0)
86
- rspec-core (~> 3.10.0)
87
- rspec-expectations (~> 3.10.0)
88
- rspec-mocks (~> 3.10.0)
89
- rspec-core (3.10.1)
90
- rspec-support (~> 3.10.0)
91
- rspec-expectations (3.10.1)
86
+ rspec (3.12.0)
87
+ rspec-core (~> 3.12.0)
88
+ rspec-expectations (~> 3.12.0)
89
+ rspec-mocks (~> 3.12.0)
90
+ rspec-core (3.12.0)
91
+ rspec-support (~> 3.12.0)
92
+ rspec-expectations (3.12.0)
92
93
  diff-lcs (>= 1.2.0, < 2.0)
93
- rspec-support (~> 3.10.0)
94
- rspec-mocks (3.10.2)
94
+ rspec-support (~> 3.12.0)
95
+ rspec-mocks (3.12.0)
95
96
  diff-lcs (>= 1.2.0, < 2.0)
96
- rspec-support (~> 3.10.0)
97
- rspec-support (3.10.2)
98
- rubocop (1.18.4)
97
+ rspec-support (~> 3.12.0)
98
+ rspec-support (3.12.0)
99
+ rubocop (1.39.0)
100
+ json (~> 2.3)
99
101
  parallel (~> 1.10)
100
- parser (>= 3.0.0.0)
102
+ parser (>= 3.1.2.1)
101
103
  rainbow (>= 2.2.2, < 4.0)
102
104
  regexp_parser (>= 1.8, < 3.0)
103
- rexml
104
- rubocop-ast (>= 1.8.0, < 2.0)
105
+ rexml (>= 3.2.5, < 4.0)
106
+ rubocop-ast (>= 1.23.0, < 2.0)
105
107
  ruby-progressbar (~> 1.7)
106
108
  unicode-display_width (>= 1.4.0, < 3.0)
107
- rubocop-ast (1.8.0)
108
- parser (>= 3.0.1.1)
109
- rubocop-performance (1.11.4)
109
+ rubocop-ast (1.23.0)
110
+ parser (>= 3.1.1.0)
111
+ rubocop-performance (1.15.1)
110
112
  rubocop (>= 1.7.0, < 2.0)
111
113
  rubocop-ast (>= 0.4.0)
112
114
  rubocop-rake (0.6.0)
113
115
  rubocop (~> 1.0)
114
- rubocop-rspec (2.4.0)
115
- rubocop (~> 1.0)
116
- rubocop-ast (>= 1.1.0)
116
+ rubocop-rspec (2.15.0)
117
+ rubocop (~> 1.33)
117
118
  ruby-progressbar (1.11.0)
118
- ruby_parser (3.16.0)
119
- sexp_processor (~> 4.15, >= 4.15.1)
120
- sexp_processor (4.15.3)
121
- sqlite3 (1.4.2)
122
- thor (1.1.0)
123
- tzinfo (2.0.4)
119
+ ruby_parser (3.19.1)
120
+ sexp_processor (~> 4.16)
121
+ sexp_processor (4.16.1)
122
+ sqlite3 (1.5.4)
123
+ mini_portile2 (~> 2.8.0)
124
+ thor (1.2.1)
125
+ tzinfo (2.0.5)
124
126
  concurrent-ruby (~> 1.0)
125
- unicode-display_width (2.0.0)
126
- zeitwerk (2.4.2)
127
+ unicode-display_width (2.3.0)
128
+ zeitwerk (2.6.6)
127
129
 
128
130
  PLATFORMS
129
131
  ruby
@@ -145,4 +147,4 @@ DEPENDENCIES
145
147
  sqlite3
146
148
 
147
149
  BUNDLED WITH
148
- 2.2.24
150
+ 2.3.26
data/README.md CHANGED
@@ -3,11 +3,9 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/lite-uxid.svg)](http://badge.fury.io/rb/lite-uxid)
4
4
  [![Build Status](https://travis-ci.org/drexed/lite-uxid.svg?branch=master)](https://travis-ci.org/drexed/lite-uxid)
5
5
 
6
- Lite::Uxid is a library for generating or obfuscating Id's based on different patterns.
6
+ Lite::Uxid is a library for generating or obfuscating ID's based on different patterns.
7
7
  It's very useful to hide the number of resources in your database and protect against enumeration attacks.
8
8
 
9
- **NOTE:** If you are coming from `ActiveUxid`, please read the [port](#port) section.
10
-
11
9
  ## Installation
12
10
 
13
11
  Add this line to your application's Gemfile:
@@ -30,20 +28,22 @@ Or install it yourself as:
30
28
  * [Hashid](#hashid)
31
29
  * [NanoID](#nanoid)
32
30
  * [ULID](#ulid)
31
+ * [Options](#options)
33
32
  * [ActiveRecord](#active_record)
34
33
  * [Benchmarks](#benchmarks)
35
- * [Port](#port)
36
34
 
37
35
  ## Configuration
38
36
 
39
- `rails g lite:uxid:install` will generate the following file:
40
- `../config/initalizers/lite_uxid.rb`
37
+ `rails g lite:uxid:install` will generate the following file in your application root:
38
+ `config/initalizers/lite_uxid.rb`
41
39
 
42
40
  ```ruby
43
41
  Lite::Uxid.configure do |config|
44
42
  config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
45
- config.encoding_length = 26
46
43
  config.encoding_salt = 1_369_136
44
+ config.hashid_length = 12
45
+ config.nanoid_length = 21
46
+ config.ulid_length = 26
47
47
  end
48
48
  ```
49
49
 
@@ -58,7 +58,7 @@ Lite::Uxid::Hashid.decode('q5D8inm0') #=> 10
58
58
 
59
59
  ## NanoID
60
60
 
61
- NanoID are not reversible and are the second fastest ID generator but while unlikely can produce collisions.
61
+ NanoID are irreversible and are the second fastest ID generator but while unlikely can produce collisions.
62
62
 
63
63
  ```ruby
64
64
  Lite::Uxid::Nanoid.encode #=> '0bmHjB5Gx8FTBqJekX6dS6XIXf'
@@ -66,38 +66,51 @@ Lite::Uxid::Nanoid.encode #=> '0bmHjB5Gx8FTBqJekX6dS6XIXf'
66
66
 
67
67
  ## ULID
68
68
 
69
- ULID are not reversible but provide information outside of just randomness.
69
+ ULID are irreversible but provide information outside of just randomness.
70
70
 
71
71
  ```ruby
72
72
  Lite::Uxid::Ulid.encode #=> '1mqfg9qa96s8s5f02o1ucf8lcc'
73
73
  ```
74
74
 
75
- #### ActiveRecord
75
+ ## Options
76
+
77
+ Local options can be passed to override global options.
78
+
79
+ ```ruby
80
+ Lite::Uxid::Ulid.encode(chars: 'abc123', length: 12) #=> 'a3b12c12c3ca'
81
+ ```
82
+
83
+ ## ActiveRecord
76
84
 
77
85
  **Table**
78
86
 
79
87
  Add the following attribute to all corresponding tables.
80
88
 
81
89
  ```ruby
82
- # omitted
83
- t.binary :uxid, index: { unique: true }
84
- # omitted
90
+ t.string :uxid, index: { unique: true }
85
91
  ```
86
92
 
87
93
  **Setup**
88
94
 
89
- All `nanoid` and `uxid` attributes will be automatically generated and applied when the record is created.
95
+ `uxid` attribute will be automatically generated and applied when the record is created.
90
96
 
97
+ #### HashID
91
98
  ```ruby
92
99
  class User < ActiveRecord::Base
93
100
  include Lite::Uxid::Record::Hashid
101
+ end
102
+ ```
94
103
 
95
- # - or -
96
-
104
+ #### NanoID
105
+ ```ruby
106
+ class User < ActiveRecord::Base
97
107
  include Lite::Uxid::Record::Nanoid
108
+ end
109
+ ```
98
110
 
99
- # - or -
100
-
111
+ #### ULID
112
+ ```ruby
113
+ class User < ActiveRecord::Base
101
114
  include Lite::Uxid::Record::Ulid
102
115
  end
103
116
  ```
@@ -106,13 +119,17 @@ end
106
119
 
107
120
  Using one of the mixins above provides a handy method to find records by uxid.
108
121
 
122
+ #### HashID methods
109
123
  ```ruby
110
- User.find_by_uxid('x123') #=> Find record by uxid
111
- User.find_by_uxid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
112
-
113
- # The following method is for Hashid based Uxid's.
114
124
  user = User.new
115
- user.uxid_to_id #=> Decodes the records uxid to id (only for Hashid based Id's)
125
+ user.id_to_uxid #=> Encodes the records id to uxid
126
+ user.uxid_to_id #=> Decodes the records uxid to id
127
+ ```
128
+
129
+ #### Finder methods
130
+ ```ruby
131
+ User.find_by_uxid('x123') #=> Find record by uxid
132
+ User.find_by_uxid!('x123') #=> Raises an ActiveRecord::RecordNotFound error if not found
116
133
  ```
117
134
 
118
135
  ## Benchmarks
@@ -121,12 +138,6 @@ The classes ranked from fastest to slowest are `Hashid`, `Nanoid`, and `Ulid`.
121
138
 
122
139
  View how each compares by running the [benchmarks](https://github.com/drexed/lite-uxid/tree/master/benchmarks).
123
140
 
124
- ## Port
125
-
126
- `Lite::Uxid` is a compatible port of [ActiveUxid](https://github.com/drexed/active_uxid).
127
-
128
- Switching is as easy as renaming `ActiveUxid` to `Lite::Uxid`.
129
-
130
141
  ## Development
131
142
 
132
143
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -135,7 +146,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
135
146
 
136
147
  ## Contributing
137
148
 
138
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-uxid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/drexed/lite-uxid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
139
150
 
140
151
  ## License
141
152
 
@@ -143,4 +154,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
143
154
 
144
155
  ## Code of Conduct
145
156
 
146
- Everyone interacting in the Lite::Uxid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-uxid/blob/master/CODE_OF_CONDUCT.md).
157
+ Everyone interacting in the Lite::Uxid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/drexed/lite-uxid/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
@@ -2,19 +2,19 @@
2
2
 
3
3
  %w[lib benchmarks].each { |name| $LOAD_PATH.unshift(name) }
4
4
 
5
- require 'benchmark/ips'
6
- require 'lite/uxid'
5
+ require "benchmark/ips"
6
+ require "lite/uxid"
7
7
 
8
8
  Benchmark.ips do |x|
9
- x.report('Hashid') do
9
+ x.report("Hashid") do
10
10
  Lite::Uxid::Hashid.encode(rand(1..1_000_000))
11
11
  end
12
12
 
13
- x.report('NanoID') do
13
+ x.report("NanoID") do
14
14
  Lite::Uxid::Nanoid.encode
15
15
  end
16
16
 
17
- x.report('ULID') do
17
+ x.report("ULID") do
18
18
  Lite::Uxid::Ulid.encode
19
19
  end
20
20
 
data/bin/console CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'bundler/setup'
5
- require 'lite/uxid'
4
+ require "bundler/setup"
5
+ require "lite/uxid"
6
6
 
7
7
  # You can add fixtures and/or initialization code here to make experimenting
8
8
  # with your gem easier. You can also use a different console, if you like.
@@ -11,5 +11,5 @@ require 'lite/uxid'
11
11
  # require "pry"
12
12
  # Pry.start
13
13
 
14
- require 'irb'
14
+ require "irb"
15
15
  IRB.start(__FILE__)
@@ -4,10 +4,10 @@ module Lite
4
4
  module Uxid
5
5
  class InstallGenerator < Rails::Generators::Base
6
6
 
7
- source_root File.expand_path('../templates', __FILE__)
7
+ source_root File.expand_path("../templates", __FILE__)
8
8
 
9
9
  def copy_initializer_file
10
- copy_file('install.rb', 'config/initializers/lite_uxid.rb')
10
+ copy_file("install.rb", "config/initializers/lite_uxid.rb")
11
11
  end
12
12
 
13
13
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Lite::Uxid.configure do |config|
4
- config.encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
5
- config.encoding_length = 26
4
+ config.encoding_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
6
5
  config.encoding_salt = 1_369_136
6
+ config.hashid_length = 12
7
+ config.nanoid_length = 21
8
+ config.ulid_length = 26
7
9
  end
@@ -5,12 +5,14 @@ module Lite
5
5
 
6
6
  class Configuration
7
7
 
8
- attr_accessor :encoding_chars, :encoding_length, :encoding_salt
8
+ attr_accessor :encoding_chars, :encoding_salt, :hashid_length, :nanoid_length, :ulid_length
9
9
 
10
10
  def initialize
11
- @encoding_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
12
- @encoding_length = 26
11
+ @encoding_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
13
12
  @encoding_salt = 1_369_136
13
+ @hashid_length = 12
14
+ @nanoid_length = 21
15
+ @ulid_length = 26
14
16
  end
15
17
 
16
18
  end
@@ -2,60 +2,41 @@
2
2
 
3
3
  module Lite
4
4
  module Uxid
5
- class Hashid < Lite::Uxid::Base
6
-
7
- def initialize(id)
8
- @id = id
9
- super()
10
- end
11
-
12
- class << self
13
-
14
- def encode(id)
15
- klass = new(id)
16
- klass.encode
17
- end
18
-
19
- def decode(id)
20
- klass = new(id)
21
- klass.decode
22
- end
23
-
24
- end
5
+ class Hashid < Reversible
25
6
 
26
7
  def encode
27
- encode_chars((@id + encoding_salt) << encoding_length)
8
+ encode_chars((id + coder_salt) << coder_length)
28
9
  end
29
10
 
30
11
  def decode
31
- (decode_chars(@id) >> encoding_length) - encoding_salt
12
+ (decode_chars(id) >> coder_length) - coder_salt
32
13
  end
33
14
 
34
15
  private
35
16
 
36
- def encode_chars(id)
37
- return '0' if id.zero?
38
- return nil if id.negative?
17
+ def encode_chars(decoded_id)
18
+ return "0" if decoded_id.zero?
19
+ return nil if decoded_id.negative?
39
20
 
40
- str = ''
21
+ str = ""
41
22
 
42
- while id.positive?
43
- str = "#{encoding_chars[id % encoding_base]}#{str}"
44
- id /= encoding_base
23
+ while decoded_id.positive?
24
+ str = "#{coder_chars[decoded_id % coder_base]}#{str}"
25
+ decoded_id /= coder_base
45
26
  end
46
27
 
47
28
  str
48
29
  end
49
30
 
50
- def decode_chars(id)
31
+ def decode_chars(encoded_id)
51
32
  pos = 0
52
33
  num = 0
53
- len = id.length
34
+ len = encoded_id.length
54
35
  max = len - 1
55
36
 
56
37
  while pos < len
57
- pow = encoding_base**(max - pos)
58
- num += encoding_chars.index(id[pos]) * pow
38
+ pow = coder_base**(max - pos)
39
+ num += coder_chars.index(encoded_id[pos]) * pow
59
40
  pos += 1
60
41
  end
61
42
 
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+ class Irreversible
6
+
7
+ attr_reader :opts
8
+
9
+ def initialize(opts = {})
10
+ @opts = opts
11
+ end
12
+
13
+ class << self
14
+
15
+ def encode(opts = {})
16
+ klass = new(opts)
17
+ klass.encode
18
+ end
19
+
20
+ def decode(opts = {})
21
+ klass = new(opts)
22
+ klass.decode
23
+ end
24
+
25
+ end
26
+
27
+ def encode
28
+ raise NotImplementedError, "override method in #{coder_class}"
29
+ end
30
+
31
+ def decode
32
+ raise NotImplementedError, "coder does not support decoding"
33
+ end
34
+
35
+ private
36
+
37
+ def coder_base
38
+ @coder_base ||= coder_chars.length
39
+ end
40
+
41
+ def coder_chars
42
+ @coder_chars ||=
43
+ opts.delete(:chars) ||
44
+ Lite::Uxid.configuration.encoding_chars
45
+ end
46
+
47
+ def coder_class
48
+ @coder_class ||= self.class.name.split("::").last
49
+ end
50
+
51
+ def coder_length
52
+ @coder_length ||=
53
+ opts.delete(:length) ||
54
+ Lite::Uxid.configuration.send("#{coder_class.downcase}_length")
55
+ end
56
+
57
+ def coder_salt
58
+ @coder_salt ||=
59
+ opts.delete(:salt) ||
60
+ Lite::Uxid.configuration.encoding_salt
61
+ end
62
+
63
+ end
64
+ end
65
+ end
@@ -2,19 +2,10 @@
2
2
 
3
3
  module Lite
4
4
  module Uxid
5
- class Nanoid < Lite::Uxid::Base
6
-
7
- class << self
8
-
9
- def encode
10
- klass = new
11
- klass.encode
12
- end
13
-
14
- end
5
+ class Nanoid < Irreversible
15
6
 
16
7
  def encode
17
- encoding_chars.chars.sample(encoding_length).join
8
+ coder_chars.chars.sample(coder_length).join
18
9
  end
19
10
 
20
11
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support' unless defined?(ActiveSupport)
3
+ require "active_support" unless defined?(ActiveSupport)
4
4
 
5
5
  module Lite
6
6
  module Uxid
@@ -27,6 +27,12 @@ module Lite
27
27
  end
28
28
  end
29
29
 
30
+ def id_to_uxid
31
+ return unless respond_to?(:uxid)
32
+
33
+ Lite::Uxid::Hashid.encode(id)
34
+ end
35
+
30
36
  def uxid_to_id
31
37
  return unless respond_to?(:uxid)
32
38
 
@@ -36,8 +42,7 @@ module Lite
36
42
  private
37
43
 
38
44
  def callback_generate_uxid!
39
- hash = Lite::Uxid::Hashid.encode(id)
40
- update_column(:uxid, hash)
45
+ update_column(:uxid, id_to_uxid)
41
46
  end
42
47
 
43
48
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support' unless defined?(ActiveSupport)
3
+ require "active_support" unless defined?(ActiveSupport)
4
4
 
5
5
  module Lite
6
6
  module Uxid
@@ -13,19 +13,6 @@ module Lite
13
13
  before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) && !uxid? }
14
14
  end
15
15
 
16
- class_methods do
17
- def find_by_uxid(uxid)
18
- find_by(uxid: uxid)
19
- end
20
-
21
- def find_by_uxid!(uxid)
22
- record = find_by_uxid(uxid)
23
- return record unless record.nil?
24
-
25
- raise ActiveRecord::RecordNotFound
26
- end
27
- end
28
-
29
16
  private
30
17
 
31
18
  def callback_generate_uxid!
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support' unless defined?(ActiveSupport)
3
+ require "active_support" unless defined?(ActiveSupport)
4
4
 
5
5
  module Lite
6
6
  module Uxid
@@ -13,19 +13,6 @@ module Lite
13
13
  before_create :callback_generate_uxid!, if: proc { respond_to?(:uxid) && !uxid? }
14
14
  end
15
15
 
16
- class_methods do
17
- def find_by_uxid(uxid)
18
- find_by(uxid: uxid)
19
- end
20
-
21
- def find_by_uxid!(uxid)
22
- record = find_by_uxid(uxid)
23
- return record unless record.nil?
24
-
25
- raise ActiveRecord::RecordNotFound
26
- end
27
- end
28
-
29
16
  private
30
17
 
31
18
  def callback_generate_uxid!
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Uxid
5
+ class Reversible < Irreversible
6
+
7
+ attr_reader :id
8
+
9
+ def initialize(id, opts = {})
10
+ @id = id
11
+ super(opts)
12
+ end
13
+
14
+ class << self
15
+
16
+ def encode(id, opts = {})
17
+ klass = new(id, opts)
18
+ klass.encode
19
+ end
20
+
21
+ def decode(id, opts = {})
22
+ klass = new(id, opts)
23
+ klass.decode
24
+ end
25
+
26
+ end
27
+
28
+ def decode
29
+ raise NotImplementedError, "override method in #{coder_class}"
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -1,29 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'securerandom' unless defined?(SecureRandom)
3
+ require "securerandom" unless defined?(SecureRandom)
4
4
 
5
5
  module Lite
6
6
  module Uxid
7
- class Ulid < Lite::Uxid::Base
7
+ class Ulid < Irreversible
8
8
 
9
9
  MASK = 0x1f
10
10
 
11
- class << self
12
-
13
- def encode
14
- klass = new
15
- klass.encode
16
- end
17
-
18
- end
19
-
20
11
  def encode
21
12
  oct = octect
22
- ele = '0' * encoding_length
23
- pos = encoding_length - 1
13
+ ele = "0" * coder_length
14
+ pos = coder_length - 1
24
15
 
25
16
  while oct.positive?
26
- ele[pos] = encoding_chars[oct & MASK]
17
+ ele[pos] = coder_chars[oct & MASK]
27
18
  oct >>= 5
28
19
  pos -= 1
29
20
  end
@@ -38,7 +29,7 @@ module Lite
38
29
  end
39
30
 
40
31
  def octect
41
- (hi, lo) = bytes.unpack('Q>Q>')
32
+ (hi, lo) = bytes.unpack("Q>Q>")
42
33
  (hi << 64) | lo
43
34
  end
44
35
 
@@ -48,7 +39,7 @@ module Lite
48
39
  end
49
40
 
50
41
  def unixtime_48bit
51
- [unixtime_ms].pack('Q>')[2..-1]
42
+ [unixtime_ms].pack("Q>")[2..-1]
52
43
  end
53
44
 
54
45
  end
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Uxid
5
5
 
6
- VERSION = '1.1.3'
6
+ VERSION = "1.2.0"
7
7
 
8
8
  end
9
9
  end
data/lib/lite/uxid.rb CHANGED
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'generators/lite/uxid/install_generator' if defined?(Rails::Generators)
3
+ require "generators/lite/uxid/install_generator" if defined?(Rails::Generators)
4
4
 
5
- require 'lite/uxid/version'
6
- require 'lite/uxid/configuration'
7
- require 'lite/uxid/base'
8
- require 'lite/uxid/record/hashid'
9
- require 'lite/uxid/record/nanoid'
10
- require 'lite/uxid/record/ulid'
11
- require 'lite/uxid/hashid'
12
- require 'lite/uxid/nanoid'
13
- require 'lite/uxid/ulid'
5
+ require "lite/uxid/version"
6
+ require "lite/uxid/configuration"
7
+ require "lite/uxid/irreversible"
8
+ require "lite/uxid/reversible"
9
+ require "lite/uxid/record/hashid"
10
+ require "lite/uxid/record/nanoid"
11
+ require "lite/uxid/record/ulid"
12
+ require "lite/uxid/hashid"
13
+ require "lite/uxid/nanoid"
14
+ require "lite/uxid/ulid"
data/lite-uxid.gemspec CHANGED
@@ -1,53 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path("../lib", __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'lite/uxid/version'
5
+ require "lite/uxid/version"
6
6
 
7
7
  Gem::Specification.new do |spec|
8
- spec.name = 'lite-uxid'
8
+ spec.name = "lite-uxid"
9
9
  spec.version = Lite::Uxid::VERSION
10
- spec.authors = ['Juan Gomez']
10
+ spec.authors = ["Juan Gomez"]
11
11
  spec.email = %w[j.gomez@drexed.com]
12
12
 
13
13
  spec.summary = "Generate or obfuscate Id's using different patterns"
14
- spec.homepage = 'http://drexed.github.io/lite-uxid'
15
- spec.license = 'MIT'
14
+ spec.homepage = "http://drexed.github.io/lite-uxid"
15
+ spec.license = "MIT"
16
16
 
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- if spec.respond_to?(:metadata)
20
- spec.metadata.merge(
21
- 'allowed_push_host' => 'https://rubygems.org',
22
- 'changelog_uri' => 'https://github.com/drexed/lite-uxid/blob/master/CHANGELOG.md',
23
- 'homepage_uri' => spec.homepage,
24
- 'source_code_uri' => 'https://github.com/drexed/lite-uxid'
25
- )
26
- else
27
- raise 'RubyGems 2.0 or newer is required to protect against ' \
28
- 'public gem pushes.'
29
- end
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ spec.metadata["changelog_uri"] = "https://github.com/drexed/lite-uxid/blob/master/CHANGELOG.md"
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["rubygems_mfa_required"] = "true"
23
+ spec.metadata["source_code_uri"] = "https://github.com/drexed/lite-uxid"
30
24
 
31
25
  # Specify which files should be added to the gem when it is released.
32
26
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
34
28
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
29
  end
36
- spec.bindir = 'exe'
30
+ spec.bindir = "exe"
37
31
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
32
  spec.require_paths = %w[lib]
39
33
 
40
- spec.add_development_dependency 'activerecord'
41
- spec.add_development_dependency 'activesupport'
42
- spec.add_development_dependency 'bundler'
43
- spec.add_development_dependency 'database_cleaner'
44
- spec.add_development_dependency 'fasterer'
45
- spec.add_development_dependency 'generator_spec'
46
- spec.add_development_dependency 'rake'
47
- spec.add_development_dependency 'rspec'
48
- spec.add_development_dependency 'rubocop'
49
- spec.add_development_dependency 'rubocop-performance'
50
- spec.add_development_dependency 'rubocop-rake'
51
- spec.add_development_dependency 'rubocop-rspec'
52
- spec.add_development_dependency 'sqlite3'
34
+ spec.add_development_dependency "activerecord"
35
+ spec.add_development_dependency "activesupport"
36
+ spec.add_development_dependency "bundler"
37
+ spec.add_development_dependency "database_cleaner"
38
+ spec.add_development_dependency "fasterer"
39
+ spec.add_development_dependency "generator_spec"
40
+ spec.add_development_dependency "rake"
41
+ spec.add_development_dependency "rspec"
42
+ spec.add_development_dependency "rubocop"
43
+ spec.add_development_dependency "rubocop-performance"
44
+ spec.add_development_dependency "rubocop-rake"
45
+ spec.add_development_dependency "rubocop-rspec"
46
+ spec.add_development_dependency "sqlite3"
53
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-uxid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-31 00:00:00.000000000 Z
11
+ date: 2022-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -203,7 +203,6 @@ files:
203
203
  - ".gitignore"
204
204
  - ".rspec"
205
205
  - ".rubocop.yml"
206
- - ".travis.yml"
207
206
  - CHANGELOG.md
208
207
  - CODE_OF_CONDUCT.md
209
208
  - Gemfile
@@ -218,20 +217,26 @@ files:
218
217
  - lib/generators/lite/uxid/install_generator.rb
219
218
  - lib/generators/lite/uxid/templates/install.rb
220
219
  - lib/lite/uxid.rb
221
- - lib/lite/uxid/base.rb
222
220
  - lib/lite/uxid/configuration.rb
223
221
  - lib/lite/uxid/hashid.rb
222
+ - lib/lite/uxid/irreversible.rb
224
223
  - lib/lite/uxid/nanoid.rb
225
224
  - lib/lite/uxid/record/hashid.rb
226
225
  - lib/lite/uxid/record/nanoid.rb
227
226
  - lib/lite/uxid/record/ulid.rb
227
+ - lib/lite/uxid/reversible.rb
228
228
  - lib/lite/uxid/ulid.rb
229
229
  - lib/lite/uxid/version.rb
230
230
  - lite-uxid.gemspec
231
231
  homepage: http://drexed.github.io/lite-uxid
232
232
  licenses:
233
233
  - MIT
234
- metadata: {}
234
+ metadata:
235
+ allowed_push_host: https://rubygems.org
236
+ changelog_uri: https://github.com/drexed/lite-uxid/blob/master/CHANGELOG.md
237
+ homepage_uri: http://drexed.github.io/lite-uxid
238
+ rubygems_mfa_required: 'true'
239
+ source_code_uri: https://github.com/drexed/lite-uxid
235
240
  post_install_message:
236
241
  rdoc_options: []
237
242
  require_paths:
@@ -247,7 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
247
252
  - !ruby/object:Gem::Version
248
253
  version: '0'
249
254
  requirements: []
250
- rubygems_version: 3.2.24
255
+ rubygems_version: 3.3.26
251
256
  signing_key:
252
257
  specification_version: 4
253
258
  summary: Generate or obfuscate Id's using different patterns
data/.travis.yml DELETED
@@ -1,25 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.5
6
- - 2.6
7
- - 2.7
8
- - ruby-head
9
- matrix:
10
- fast_finish: true
11
- allow_failures:
12
- - rvm: ruby-head
13
- before_install:
14
- - gem update --system
15
- - gem install bundler
16
- install:
17
- - bundle install --jobs=3 --retry=3
18
- script:
19
- - bundle exec rspec
20
- - bundle exec rubocop
21
- - bundle exec fasterer
22
- notifications:
23
- email: false
24
- slack:
25
- secure: tGjUBos/5ypoNmEc2L05h0qTKpizE/g/Mx5McqJo390y3NniU9Quc7ci8JqTzPJq3kvZugMKo33SMvOFBN6drwcNN6I3BQyfGv77ZIt9coJOnz3ASr55fLtnO5gPcDxYdRAV5sOXBtaudY39mXRby/YEJx7PX6AE2NHpdhqwco3gsTWp77VxQ0cHxyPUvW0v+vEBaozzsduUKh1pH6+OckzH9/7lBQSGdljRAlhOM4KBnYD0bttw9bZk1QTyoGnmjAReTpbIPaIPFG28d9uVWD2WgQvyMGjpjdEzM8vTa7htNyfT71Hu7gToUDU9vknHQINuDPsBfpMZqKDtEUbGQwgfRAsdEzoBX/QRh9ArWDQ+ycmX+UuDT7+E5lt73Y6CX59EECMYQKM9LVNd08utDkhfYPH5r0MUs5kNblFi4NIUtgbHKFw2b4lmzNYuhdSUwVoUgdpW8NHtg4I4LzdTgFMI5or8r8Pbn2djGiGYq9kMC8eu5BksiAlZq8w0UcMlGz+uRtbpkf9IJNB+VuaeXohqIT8NKGfcC1j7kbbxrvQEbOW36TA09n34IGNF7iS/FWL4Rb2OhrsE16VPZ397GbSDXZsH7JzB1E3BW2lhONrw3U332RW+QSBxCdQZ9ig+xsoxfOfJVoqho6jHoNxJxDYvo7WWTY2Ashb7yu62RNo=
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Lite
4
- module Uxid
5
- class Base
6
-
7
- def initialize
8
- @config = Lite::Uxid.configuration
9
- end
10
-
11
- def encoding_base
12
- encoding_chars.length
13
- end
14
-
15
- Lite::Uxid.configuration.instance_variables.each do |setting|
16
- setting = setting.to_s.tr(':@', '')
17
- define_method(setting) { @config.send(setting) }
18
- end
19
-
20
- end
21
- end
22
- end