ksuid 0.5.0 → 1.0.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: a25a1c0246464a41b5b419ad0fffbd8c5b08b7dfb882ccb31a29a0a6fbbc0fe1
4
- data.tar.gz: 70a978445c93cc062cb704a5156fd919c623677fc1666723ba1f828067978fcd
3
+ metadata.gz: c97a36378134ab6cdd674866a6744e5b84ff5976db6575d5364cc844cc9dbf4f
4
+ data.tar.gz: c5ebfa42d22044e518a63f9508924517a5b7ec83a9e3059c64ecb8075ee88e07
5
5
  SHA512:
6
- metadata.gz: 7ed9fde240ebee2cdffcb81877548b780286844d2ccfa6a4200f4a0c36161e1d53f073b761e22d7b3fb0d857a31b2fae50fdb30a6ca3571b1cc933ea09281363
7
- data.tar.gz: 946ad6b51f84fca3b1a0230c4892fc3afd862baabe232efac3b0f9d196d16e2c16702e645faf12bcb9e2614c15a504f007c9cc44383c5ac6e87b8feeb5554d7f
6
+ metadata.gz: 6743031c8702c60e3be6d58f2bde58b1290abc1943afb110adf3a9a2066017d24a453a826373312539175629b2ed9a612ed257abdad22e0cef74a88e045a12e5
7
+ data.tar.gz: c2c504f17a31fe7d0c25475b606045424868aca2aa0015e78ff41da89828a7e1eb5b88bcf88a8e0da34a81945d5c68b3cbfb1d4acead1d9227c423cc89ece0ae
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.0.0](https://github.com/michaelherold/ksuid/compare/v0.5.0...v1.0.0) - 2023-02-25
8
+
9
+ ### Removed
10
+
11
+ - Extracted the ActiveRecord functionality into its own gem, `activerecord-ksuid`. It is API-compatible with v0.5.0 so to restore functionality, you should only need to add the new gem to your application. See [the upgrading notice](./UPGRADING.md) for more information.
12
+
7
13
  ## [0.5.0](https://github.com/michaelherold/ksuid/compare/v0.4.0...v0.5.0) - 2022-08-18
8
14
 
9
15
  ### Added
data/CONTRIBUTING.md CHANGED
@@ -28,9 +28,9 @@ Ideally, a bug report should include a pull request with failing specs.
28
28
  1. [Fork the repository].
29
29
  2. [Create a topic branch].
30
30
  3. Add specs for your unimplemented feature or bug fix.
31
- 4. Run `appraisal rake spec`. If your specs pass, return to step 3.
31
+ 4. Run `bundle exec rake spec`. If your specs pass, return to step 3.
32
32
  5. Implement your feature or bug fix.
33
- 6. Run `appraisal rake`. If your specs or any of the linters fail, return to step 5.
33
+ 6. Run `bundle exec rake`. If your specs or any of the linters fail, return to step 5.
34
34
  7. Open `coverage/index.html`. If your changes are not completely covered by your tests, return to step 3.
35
35
  8. Add documentation for your feature or bug fix.
36
36
  9. Commit and push your changes.
data/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # The MIT License (MIT)
2
2
 
3
- Copyright © 2017 Michael Herold
3
+ Copyright © 2017-2022 Michael Herold
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
data/README.md CHANGED
@@ -122,138 +122,21 @@ ksuid = KSUID::Prefixed.from_base62(base62_string, prefix: 'evt_')
122
122
 
123
123
  Prefixed KSUIDs order themselves with non-prefixed KSUIDs as if their prefix did not exist. With other prefixed KSUIDs, they order first by their prefix, then their timestamp.
124
124
 
125
- ### ActiveRecord
125
+ ### Integrations
126
126
 
127
- Whether you are using ActiveRecord inside an existing project or in a new project, usage is simple. Additionally, you can use it with or without Rails.
127
+ KSUID for Ruby can integrate with other systems through adapter gems. Below is a sample of these adapter gems.
128
128
 
129
- _Note: In v1.0.0 of KSUID for Ruby, we will extract this behavior into a separate gem, `activerecord-ksuid`. It will be API-compatible with the implementation in v0.5.0+ of KSUID for Ruby. There will be upgrade instructions for v1.0.0 once we release it._
129
+ #### ActiveRecord
130
130
 
131
- #### Adding to an existing model
131
+ If you want to include KSUID columns in your ActiveRecord models, install the `activerecord-ksuid` gem. If you are using it within a Rails app, run the following:
132
132
 
133
- Within a Rails project, it is very easy to get started using KSUIDs within your models. You can use the `ksuid` column type in a Rails migration to add a column to an existing model:
133
+ bundle add activerecord-ksuid --require active_record/ksuid/railtie
134
+
135
+ If you are using it outside of Rails, add this to your Gemfile:
134
136
 
135
- rails generate migration add_ksuid_to_events ksuid:ksuid
137
+ gem 'activerecord-ksuid', require: ['ksuid', 'active_record/ksuid', 'active_record/ksuid/table_definition']
136
138
 
137
- This will generate a migration like the following:
138
-
139
- ```ruby
140
- class AddKsuidToEvents < ActiveRecord::Migration[5.2]
141
- def change
142
- add_column :events, :unique_id, :ksuid
143
- end
144
- end
145
- ```
146
-
147
- Then, to add proper handling to the field, you will want to mix a module into the model:
148
-
149
- ```ruby
150
- class Event < ApplicationRecord
151
- include ActiveRecord::KSUID[:unique_id]
152
- end
153
- ```
154
-
155
- #### Creating a new model
156
-
157
- To create a new model with a `ksuid` field that is stored as a KSUID, use the `ksuid` column type. Using the Rails generators, this looks like:
158
-
159
- rails generate model Event my_field_name:ksuid
160
-
161
- If you would like to add a KSUID to an existing model, you can do so with the following:
162
-
163
- ```ruby
164
- class AddKsuidToEvents < ActiveRecord::Migration[5.2]
165
- change_table :events do |table|
166
- table.ksuid :my_field_name
167
- end
168
- end
169
- ```
170
-
171
- Once you have generated the table that you will use for your model, you will need to include a module into the model class, as follows:
172
-
173
- ```ruby
174
- class Event < ApplicationRecord
175
- include ActiveRecord::KSUID[:my_field_name]
176
- end
177
- ```
178
-
179
- ##### With a KSUID primary key
180
-
181
- You can also use a KSUID as the primary key on a table, much like you can use a UUID in vanilla Rails. To do so requires a little more finagling than you can manage through the generators. When hand-writing the migration, it will look like this:
182
-
183
- ```ruby
184
- class CreateEvents < ActiveRecord::Migration[5.2]
185
- create_table :events, id: false do |table|
186
- table.ksuid :id, primary_key: true
187
- end
188
- end
189
- ```
190
-
191
- You will need to mix in the module into your model as well:
192
-
193
- ```ruby
194
- class Event < ApplicationRecord
195
- include ActiveRecord::KSUID[:id]
196
- end
197
- ```
198
-
199
- #### Outside of Rails
200
-
201
- Outside of Rails, you cannot rely on the Railtie to load the appropriate files for you automatically. Toward the start of your application's boot process, you will want to require the following:
202
-
203
- ```ruby
204
- require 'active_record/ksuid'
205
-
206
- # If you will be using the ksuid column type in a migration
207
- require 'active_record/ksuid/table_definition'
208
- ```
209
-
210
- Once you have required the file(s) that you need, everything else will work as it does above.
211
-
212
- #### Binary vs. String KSUIDs
213
-
214
- These examples all store your identifier as a string-based KSUID. If you would like to use binary KSUIDs instead, use the `ksuid_binary` column type. Unless you need to be super-efficient with your database, we recommend using string-based KSUIDs because it makes looking at the data while in the database a little easier to understand.
215
-
216
- When you include the KSUID module into your model, you will want to pass the `:binary` option as well:
217
-
218
- ```ruby
219
- class Event < ApplicationRecord
220
- include ActiveRecord::KSUID[:my_field_name, binary: true]
221
- end
222
- ```
223
-
224
- #### Using a prefix on your KSUID field
225
-
226
- For prefixed KSUIDs in ActiveRecord, you must pass the intended prefix during table definition so that the field is of appropriate size.
227
-
228
- ```ruby
229
- class CreateEvents < ActiveRecord::Migration[5.2]
230
- create_table :events do |table|
231
- table.ksuid :ksuid, prefix: 'evt_'
232
- end
233
- end
234
- ```
235
-
236
- You also must pass it in the module builder that you include in your model:
237
-
238
- ```ruby
239
- class Event < ApplicationRecord
240
- include ActiveRecord::KSUID[:ksuid, prefix: 'evt_']
241
- end
242
- ```
243
-
244
- You cannot use a prefix with a binary-encoded KSUID.
245
-
246
- #### Use the KSUID as your `created_at` timestamp
247
-
248
- Since KSUIDs include a timestamp as well, you can infer the `#created_at` timestamp from the KSUID. The module builder enables that option automatically with the `:created_at` option, like so:
249
-
250
- ```ruby
251
- class Event < ApplicationRecord
252
- include ActiveRecord::KSUID[:my_field_name, created_at: true]
253
- end
254
- ```
255
-
256
- This allows you to be efficient in your database design if that is a constraint you need to satisfy.
139
+ See [the readme for the integration](https://github.com/michaelherold/ksuid-ruby/blob/main/activerecord-ksuid/README.md) for more information.
257
140
 
258
141
  ## Contributing
259
142
 
data/UPGRADING.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Upgrading instructions for KSUID for Ruby
2
2
 
3
+ ## v1.0.0
4
+
5
+ ### Extracted `ActiveRecord::KSUID` into its own gem
6
+
7
+ That KSUID for Ruby included ActiveRecord support directly in its gem has always been a regret of mine. It adds ActiveRecord and Rails concerns to a gem that you can use in any context. It makes running the test suite more complicated for no real gain. And it makes it kludgy to add support for more systems, like Sequel, since you have conflicting concerns in the same gem.
8
+
9
+ To remove this problem, v1.0.0 extracts the ActiveRecord behavior into its own gem, `activerecord-ksuid`. This version is a straight extraction with an improved test suite so it _should_ mean that the only change you have to make when upgrading from v0.5.0 is to do the following in your Gemfile:
10
+
11
+ ```diff
12
+ - gem 'ksuid'
13
+ + gem 'activerecord-ksuid'
14
+ ```
15
+
16
+ If you are still on a version prior to v0.5.0, upgrade to that version first, solve the deprecation notice below, ensure your app still works, and then upgrade to v1.0.0.
17
+
3
18
  ## v0.5.0
4
19
 
5
20
  ### Deprecated `KSUID::ActiveRecord` in favor of `ActiveRecord::KSUID`
data/lib/ksuid/version.rb CHANGED
@@ -4,5 +4,5 @@ module KSUID
4
4
  # The version of the KSUID gem
5
5
  #
6
6
  # @return [String]
7
- VERSION = '0.5.0'
7
+ VERSION = '1.0.0'
8
8
  end
data/lib/ksuid.rb CHANGED
@@ -250,5 +250,3 @@ module KSUID
250
250
  end
251
251
  private_class_method :cast_string
252
252
  end
253
-
254
- require 'active_record/ksuid/railtie' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ksuid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Herold
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-18 00:00:00.000000000 Z
11
+ date: 2023-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -37,14 +37,7 @@ files:
37
37
  - README.md
38
38
  - UPGRADING.md
39
39
  - ksuid.gemspec
40
- - lib/active_record/ksuid.rb
41
- - lib/active_record/ksuid/binary_type.rb
42
- - lib/active_record/ksuid/prefixed_type.rb
43
- - lib/active_record/ksuid/railtie.rb
44
- - lib/active_record/ksuid/table_definition.rb
45
- - lib/active_record/ksuid/type.rb
46
40
  - lib/ksuid.rb
47
- - lib/ksuid/activerecord.rb
48
41
  - lib/ksuid/base62.rb
49
42
  - lib/ksuid/configuration.rb
50
43
  - lib/ksuid/prefixed.rb
@@ -71,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
64
  - !ruby/object:Gem::Version
72
65
  version: '0'
73
66
  requirements: []
74
- rubygems_version: 3.1.6
67
+ rubygems_version: 3.3.7
75
68
  signing_key:
76
69
  specification_version: 4
77
70
  summary: Ruby implementation of the K-Sortable Unique IDentifier
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module KSUID
5
- # A binary-serialized KSUID for storage within an ActiveRecord database
6
- #
7
- # @api private
8
- #
9
- # @example Set an attribute as a KSUID using the verbose syntax
10
- # class EventWithBareBinaryType < ActiveRecord::Base
11
- # attribute :ksuid, ActiveRecord::KSUID::BinaryType.new, default: -> { KSUID.new }
12
- # end
13
- #
14
- # @example Set an attribute as a KSUID using the pre-registered type
15
- # class EventWithRegisteredBinaryType < ActiveRecord::Base
16
- # attribute :ksuid, :ksuid_binary, default: -> { KSUID.new }
17
- # end
18
- class BinaryType < ::ActiveRecord::Type::Binary
19
- # Casts a value from user input into a KSUID
20
- #
21
- # Type casting happens via the attribute setter and can take input from
22
- # many places, including:
23
- #
24
- # 1. The Rails form builder
25
- # 2. Directly from the attribute setter
26
- # 3. From the model initializer
27
- #
28
- # @param value [String, Array<Integer>, KSUID::Type] the value to cast into a KSUID
29
- # @return [KSUID::Type] the type-casted value
30
- def cast(value)
31
- ::KSUID.call(value)
32
- end
33
-
34
- # Converts a value from database input to a KSUID
35
- #
36
- # @param value [String, nil] the database-serialized KSUID to convert
37
- # @return [KSUID::Type] the deserialized KSUID
38
- def deserialize(value)
39
- return unless value
40
-
41
- value = value.to_s if value.is_a?(::ActiveRecord::Type::Binary::Data)
42
- ::KSUID.call(value)
43
- end
44
-
45
- # Casts the value from a KSUID into a database-understandable format
46
- #
47
- # @param value [KSUID::Type, nil] the KSUID in Ruby format
48
- # @return [String, nil] the base 62-encoded KSUID for storage in the database
49
- def serialize(value)
50
- return unless value
51
-
52
- super(::KSUID.call(value).to_bytes)
53
- end
54
- end
55
- end
56
- end
57
-
58
- ActiveRecord::Type.register(:ksuid_binary, ActiveRecord::KSUID::BinaryType)
@@ -1,70 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module KSUID
5
- # A string-serialized, prefixed KSUID for storage within an ActiveRecord database
6
- #
7
- # @api private
8
- # @since 0.5.0
9
- #
10
- # @example Set an attribute as a prefixed KSUID using the verbose syntax
11
- # class EventWithBarePrefixedType < ActiveRecord::Base
12
- # attribute(
13
- # :ksuid,
14
- # ActiveRecord::KSUID::PrefixedType.new(prefix: 'evt_'),
15
- # default: -> { KSUID.prefixed('evt_') }
16
- # )
17
- # end
18
- #
19
- # @example Set an attribute as a prefixed KSUID using the pre-registered type
20
- # class EventWithRegisteredPrefixedType < ActiveRecord::Base
21
- # attribute :ksuid, :ksuid_prefixed, prefix: 'evt_', default: -> { KSUID.prefixed('evt_') }
22
- # end
23
- class PrefixedType < ::ActiveRecord::Type::String
24
- # Instantiates an ActiveRecord::Type for handling prefixed KSUIDs
25
- #
26
- # @param prefix [String] the prefix to add to the KSUID
27
- def initialize(prefix: '')
28
- @prefix = prefix
29
- super()
30
- end
31
-
32
- # Casts a value from user input into a {KSUID::Prefixed}
33
- #
34
- # Type casting happens via the attribute setter and can take input from
35
- # many places, including:
36
- #
37
- # 1. The Rails form builder
38
- # 2. Directly from the attribute setter
39
- # 3. From the model initializer
40
- #
41
- # @param value [String, Array<Integer>, KSUID::Prefixed] the value to cast into a KSUID
42
- # @return [KSUID::Prefixed] the type-casted value
43
- def cast(value)
44
- ::KSUID::Prefixed.call(value, prefix: @prefix)
45
- end
46
-
47
- # Converts a value from database input to a {KSUID::Prefixed}
48
- #
49
- # @param value [String, nil] the database-serialized, prefixed KSUID to convert
50
- # @return [KSUID::Prefixed] the deserialized, prefixed KSUID
51
- def deserialize(value)
52
- return unless value
53
-
54
- ::KSUID::Prefixed.from_base62(value, prefix: @prefix)
55
- end
56
-
57
- # Casts the value from a KSUID into a database-understandable format
58
- #
59
- # @param value [KSUID::Prefixed, nil] the prefixed KSUID in Ruby format
60
- # @return [String, nil] the base 62-encoded, prefixed KSUID for storage in the database
61
- def serialize(value)
62
- return unless value
63
-
64
- ::KSUID::Prefixed.call(value, prefix: @prefix).to_s
65
- end
66
- end
67
- end
68
- end
69
-
70
- ActiveRecord::Type.register(:ksuid_prefixed, ActiveRecord::KSUID::PrefixedType)
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module KSUID
5
- # Enables the usage of KSUID types within ActiveRecord when Rails is loaded
6
- #
7
- # @api private
8
- class Railtie < ::Rails::Railtie
9
- initializer 'ksuid' do
10
- ActiveSupport.on_load :active_record do
11
- require 'active_record/ksuid'
12
- require 'ksuid/activerecord'
13
- end
14
- end
15
-
16
- initializer 'ksuid.table_definition' do
17
- ActiveSupport.on_load :active_record do
18
- require 'active_record/ksuid/table_definition'
19
-
20
- ActiveRecord::ConnectionAdapters::TableDefinition.include(
21
- ActiveRecord::KSUID::TableDefinition
22
- )
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module KSUID
5
- # Extends ActiveRecord's table definition language for KSUIDs
6
- module TableDefinition
7
- # Defines a field as a string-based KSUID
8
- #
9
- # @example Define a KSUID field as a non-primary key
10
- # ActiveRecord::Schema.define do
11
- # create_table :events, force: true do |table|
12
- # table.ksuid :ksuid, index: true, unique: true
13
- # end
14
- # end
15
- #
16
- # @example Define a KSUID field as a primary key
17
- # ActiveRecord::Schema.define do
18
- # create_table :events, force: true, id: false do |table|
19
- # table.ksuid :id, primary_key: true
20
- # end
21
- # end
22
- #
23
- # @param args [Array<Symbol>] the list of fields to define as KSUIDs
24
- # @param options [Hash] see {ActiveRecord::ConnectionAdapters::TableDefinition}
25
- # @option options [String] :prefix the prefix expected in front of the KSUID
26
- # @return [void]
27
- def ksuid(*args, **options)
28
- prefix_length = options.delete(:prefix)&.length || 0
29
-
30
- args.each { |name| column(name, :string, **options.merge(limit: 27 + prefix_length)) }
31
- end
32
-
33
- # Defines a field as a binary-based KSUID
34
- #
35
- # @example Define a KSUID field as a non-primary key
36
- # ActiveRecord::Schema.define do
37
- # create_table :events, force: true do |table|
38
- # table.ksuid_binary :ksuid, index: true, unique: true
39
- # end
40
- # end
41
- #
42
- # @example Define a KSUID field as a primary key
43
- # ActiveRecord::Schema.define do
44
- # create_table :events, force: true, id: false do |table|
45
- # table.ksuid_binary :id, primary_key: true
46
- # end
47
- # end
48
- #
49
- # @param args [Array<Symbol>] the list of fields to define as KSUIDs
50
- # @param options [Hash] see {ActiveRecord::ConnectionAdapters::TableDefinition}
51
- # @return [void]
52
- def ksuid_binary(*args, **options)
53
- args.each { |name| column(name, :binary, **options.merge(limit: 20)) }
54
- end
55
- end
56
- end
57
- end
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveRecord
4
- module KSUID
5
- # A string-serialized KSUID for storage within an ActiveRecord database
6
- #
7
- # @api private
8
- #
9
- # @example Set an attribute as a KSUID using the verbose syntax
10
- # class EventWithBareType < ActiveRecord::Base
11
- # attribute :ksuid, ActiveRecord::KSUID::Type.new, default: -> { KSUID.new }
12
- # end
13
- #
14
- # @example Set an attribute as a KSUID using the pre-registered type
15
- # class EventWithRegisteredType < ActiveRecord::Base
16
- # attribute :ksuid, :ksuid, default: -> { KSUID.new }
17
- # end
18
- class Type < ::ActiveRecord::Type::String
19
- # Casts a value from user input into a KSUID
20
- #
21
- # Type casting happens via the attribute setter and can take input from
22
- # many places, including:
23
- #
24
- # 1. The Rails form builder
25
- # 2. Directly from the attribute setter
26
- # 3. From the model initializer
27
- #
28
- # @param value [String, Array<Integer>, KSUID::Type] the value to cast into a KSUID
29
- # @return [KSUID::Type] the type-casted value
30
- def cast(value)
31
- ::KSUID.call(value)
32
- end
33
-
34
- # Converts a value from database input to a KSUID
35
- #
36
- # @param value [String, nil] the database-serialized KSUID to convert
37
- # @return [KSUID::Type] the deserialized KSUID
38
- def deserialize(value)
39
- return unless value
40
-
41
- ::KSUID.from_base62(value)
42
- end
43
-
44
- # Casts the value from a KSUID into a database-understandable format
45
- #
46
- # @param value [KSUID::Type, nil] the KSUID in Ruby format
47
- # @return [String, nil] the base 62-encoded KSUID for storage in the database
48
- def serialize(value)
49
- return unless value
50
-
51
- ::KSUID.call(value).to_s
52
- end
53
- end
54
- end
55
- end
56
-
57
- ActiveRecord::Type.register(:ksuid, ActiveRecord::KSUID::Type)
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/ksuid/binary_type'
4
- require 'active_record/ksuid/prefixed_type'
5
- require 'active_record/ksuid/type'
6
-
7
- # The Ruby on Rails object-relational mapper
8
- #
9
- # @see https://guides.rubyonrails.org/ Ruby on Rails documentation
10
- module ActiveRecord
11
- # Enables an Active Record model to have a KSUID attribute
12
- #
13
- # @api public
14
- # @since 0.5.0
15
- module KSUID
16
- # Builds a module to include into the model
17
- #
18
- # @api public
19
- #
20
- # @example Add a `#ksuid` attribute to a model
21
- # class Event < ActiveRecord::Base
22
- # include ActiveRecord::KSUID[:ksuid]
23
- # end
24
- #
25
- # @example Add a `#remote_id` attribute to a model and overrides `#created_at` to use the KSUID
26
- # class Event < ActiveRecord::Base
27
- # include ActiveRecord::KSUID[:remote_id, created_at: true]
28
- # end
29
- #
30
- # @example Add a prefixed `#ksuid` attribute to a model
31
- # class Event < ActiveRecord::Base
32
- # include ActiveRecord::KSUID[:ksuid, prefix: 'evt_']
33
- # end
34
- #
35
- # @param field [String, Symbol] the name of the field to use as a KSUID
36
- # @param created_at [Boolean] whether to override the `#created_at` method
37
- # @param binary [Boolean] whether to store the KSUID as a binary or a string
38
- # @param prefix [String, nil] a prefix to prepend to the KSUID attribute
39
- # @return [Module] the module to include into the model
40
- def self.[](field, created_at: false, binary: false, prefix: nil)
41
- raise ArgumentError, 'cannot include a prefix on a binary KSUID' if binary && prefix
42
-
43
- Module.new.tap do |mod|
44
- if prefix
45
- define_prefixed_attribute(field, mod, prefix)
46
- else
47
- define_attribute(field, mod, binary)
48
- end
49
- define_created_at(field, mod) if created_at
50
- end
51
- end
52
-
53
- # Defines the attribute method that will be written in the module
54
- #
55
- # @api private
56
- #
57
- # @param field [String, Symbol] the name of the field to set as an attribute
58
- # @param mod [Module] the module to extend
59
- # @param binary [Boolean] whether to store the KSUID as a binary or a string
60
- # @return [void]
61
- def self.define_attribute(field, mod, binary)
62
- type = 'ksuid'
63
- type = 'ksuid_binary' if binary
64
-
65
- mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
66
- def self.included(base) # def self.included(base)
67
- base.__send__( # base.__send__(
68
- :attribute, # :attribute,
69
- :#{field}, # :id,
70
- :#{type}, # :ksuid,
71
- default: -> { ::KSUID.new } # default: -> { ::KSUID.new }
72
- ) # )
73
- end # end
74
- RUBY
75
- end
76
- private_class_method :define_attribute
77
-
78
- # Defines the attribute method that will be written in the module for a field
79
- #
80
- # @api private
81
- #
82
- # @param field [String, Symbol] the name of the field to set as an attribute
83
- # @param mod [Module] the module to extend
84
- # @param prefix [String] the prefix to add to the KSUID
85
- # @return [void]
86
- def self.define_prefixed_attribute(field, mod, prefix)
87
- mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
88
- def self.included(base) # def self.included(base)
89
- base.__send__( # base.__send__(
90
- :attribute, # :attribute,
91
- :#{field}, # :id,
92
- :ksuid_prefixed, # :ksuid_prefixed,
93
- prefix: #{prefix.inspect}, # prefix: 'evt_'
94
- default: -> { ::KSUID.prefixed(#{prefix.inspect}) } # default: -> { ::KSUID.prefixed('evt_') }
95
- ) # )
96
- end # end
97
- RUBY
98
- end
99
- private_class_method :define_prefixed_attribute
100
-
101
- # Defines the `#created_at` method that will be written in the module
102
- #
103
- # @api private
104
- #
105
- # @param field [String, Symbol] the name of the KSUID attribute field
106
- # @param mod [Module] the module to extend
107
- # @return [void]
108
- def self.define_created_at(field, mod)
109
- mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
110
- def created_at # def created_at
111
- return unless #{field} # return unless ksuid
112
-
113
- #{field}.to_time # ksuid.to_time
114
- end # end
115
- RUBY
116
- end
117
- private_class_method :define_created_at
118
- end
119
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record/ksuid'
4
-
5
- module KSUID
6
- # Enables an Active Record model to have a KSUID attribute
7
- #
8
- # @api public
9
- # @deprecated Use {ActiveRecord::KSUID} instead.
10
- module ActiveRecord
11
- # Builds a module to include into the model
12
- #
13
- # @api public
14
- # @deprecated Use {::ActiveRecord::KSUID.[]} instead.
15
- #
16
- # @example Add a `#ksuid` attribute to a model
17
- # class Event < ActiveRecord::Base
18
- # include KSUID::ActiveRecord[:ksuid]
19
- # end
20
- #
21
- # @example Add a `#remote_id` attribute to a model and overrides `#created_at` to use the KSUID
22
- # class Event < ActiveRecord::Base
23
- # include KSUID::ActiveRecord[:remote_id, created_at: true]
24
- # end
25
- #
26
- # @param (see ::ActiveRecord::KSUID.[])
27
- # @return (see ::ActiveRecord::KSUID.[])
28
- def self.[](field, created_at: false, binary: false, prefix: nil)
29
- ActiveSupport::Deprecation.instance.warn(
30
- 'KSUID::ActiveRecord is deprecated! Use ActiveRecord::KSUID instead.',
31
- caller_locations
32
- )
33
-
34
- ::ActiveRecord::KSUID[field, created_at: created_at, binary: binary, prefix: prefix]
35
- end
36
- end
37
- end