globalid 1.0.1 → 1.1.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: e5a7361595eea95f395b66f4a79123ad35e61b7008d256646bea775291fa9f41
4
- data.tar.gz: a9ece75bb440f6b49b8ad623b976bc05f4f3da2151104d045d1b7305c4a8d9b2
3
+ metadata.gz: 52c4368f15fc8952c9d79189ed9a78e649e0142421721aef338625c19b24c3b9
4
+ data.tar.gz: 4d6d1c379d45ccaf5761488c540d19cac9d0488b578564964edd88c52073c511
5
5
  SHA512:
6
- metadata.gz: 8290a4a79953bb92053c2f52ae847c557e7f5d4f0528124c7f1217ee4a34b5c7d42290a2584d729e0c5011c05cd1df4c9c073be49bde7d4fa13597f44a52513f
7
- data.tar.gz: 2d9e7c573f7223406733b8be7ce1d6d764cca584bcfbb0a3f2ffb8d6f71f82970ceacf827e9c8a251975c0681b5d7d8293c1e158927dbf38f362e1337fd027ce
6
+ metadata.gz: 49ff5a4b4d4f68afdffa67a85b5aebfb3b2f518257ffec22be8a60a29ac5596a67552aae46bebab28a4fe6b37a5802ac520b0f567098617110b2f54c4a74cfcb
7
+ data.tar.gz: f6ba35dc2c080b0c996bd4529cec1438a35545c693cc0271e25c6b7bdaa8f759de61f26ae6ecf16487d89c7e6e5afc41caab7114541cd8a54ebdf9e2a3ce1f60
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2016 David Heinemeier Hansson
1
+ Copyright (c) 2014-2023 David Heinemeier Hansson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -18,4 +18,3 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
@@ -2,12 +2,20 @@
2
2
 
3
3
  class GlobalID
4
4
  module FixtureSet
5
- def signed_global_id(fixture_set_name, label, column_type: :integer, **options)
6
- identifier = identify(label, column_type)
7
- model_name = default_fixture_model_name(fixture_set_name)
8
- uri = URI::GID.build([GlobalID.app, model_name, identifier, {}])
5
+ def global_id(fixture_set_name, label, column_type: :integer, **options)
6
+ create_global_id(fixture_set_name, label, column_type: column_type, klass: GlobalID, **options)
7
+ end
9
8
 
10
- SignedGlobalID.new(uri, **options)
9
+ def signed_global_id(fixture_set_name, label, column_type: :integer, **options)
10
+ create_global_id(fixture_set_name, label, column_type: column_type, klass: SignedGlobalID, **options)
11
11
  end
12
+
13
+ private
14
+ def create_global_id(fixture_set_name, label, klass:, column_type: :integer, **options)
15
+ identifier = identify(label, column_type)
16
+ model_name = default_fixture_model_name(fixture_set_name)
17
+ uri = URI::GID.build([GlobalID.app, model_name, identifier, {}])
18
+ klass.new(uri, **options)
19
+ end
12
20
  end
13
21
  end
@@ -1,4 +1,3 @@
1
- require 'active_support'
2
1
  require 'active_support/core_ext/string/inflections' # For #model_class constantize
3
2
  require 'active_support/core_ext/array/access'
4
3
  require 'active_support/core_ext/object/try' # For #find
@@ -35,18 +34,12 @@ class GlobalID
35
34
 
36
35
  private
37
36
  def parse_encoded_gid(gid, options)
38
- new(Base64.urlsafe_decode64(repad_gid(gid)), options) rescue nil
39
- end
40
-
41
- # We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
42
- def repad_gid(gid)
43
- padding_chars = gid.length.modulo(4).zero? ? 0 : (4 - gid.length.modulo(4))
44
- gid + ('=' * padding_chars)
37
+ new(Base64.urlsafe_decode64(gid), options) rescue nil
45
38
  end
46
39
  end
47
40
 
48
41
  attr_reader :uri
49
- delegate :app, :model_name, :model_id, :params, :to_s, to: :uri
42
+ delegate :app, :model_name, :model_id, :params, :to_s, :deconstruct_keys, to: :uri
50
43
 
51
44
  def initialize(gid, options = {})
52
45
  @uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)
@@ -57,7 +50,13 @@ class GlobalID
57
50
  end
58
51
 
59
52
  def model_class
60
- model_name.constantize
53
+ model = model_name.constantize
54
+
55
+ unless model <= GlobalID
56
+ model
57
+ else
58
+ raise ArgumentError, "GlobalID and SignedGlobalID cannot be used as model_class."
59
+ end
61
60
  end
62
61
 
63
62
  def ==(other)
@@ -70,7 +69,10 @@ class GlobalID
70
69
  end
71
70
 
72
71
  def to_param
73
- # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
74
- Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
72
+ Base64.urlsafe_encode64(to_s, padding: false)
73
+ end
74
+
75
+ def as_json(*)
76
+ to_s
75
77
  end
76
78
  end
@@ -1,4 +1,3 @@
1
- require 'active_support'
2
1
  require 'active_support/core_ext/enumerable' # For Enumerable#index_by
3
2
 
4
3
  class GlobalID
@@ -3,7 +3,6 @@ require 'rails/railtie'
3
3
  rescue LoadError
4
4
  else
5
5
  require 'global_id'
6
- require 'active_support'
7
6
  require 'active_support/core_ext/string/inflections'
8
7
  require 'active_support/core_ext/integer/time'
9
8
 
@@ -1,4 +1,3 @@
1
- require 'global_id'
2
1
  require 'active_support/message_verifier'
3
2
  require 'time'
4
3
 
@@ -98,6 +98,10 @@ module URI
98
98
  "gid://#{app}#{path}#{'?' + query if query}"
99
99
  end
100
100
 
101
+ def deconstruct_keys(_keys)
102
+ {app: app, model_name: model_name, model_id: model_id, params: params}
103
+ end
104
+
101
105
  protected
102
106
  def set_path(path)
103
107
  set_model_components(path) unless defined?(@model_name) && @model_id
@@ -135,7 +139,7 @@ module URI
135
139
 
136
140
  def check_scheme(scheme)
137
141
  if scheme == 'gid'
138
- super
142
+ true
139
143
  else
140
144
  raise URI::BadURIError, "Not a gid:// URI scheme: #{inspect}"
141
145
  end
@@ -1,4 +1,3 @@
1
- require 'active_support'
2
1
  require 'active_support/message_verifier'
3
2
 
4
3
  class GlobalID
data/lib/global_id.rb CHANGED
@@ -1,5 +1,5 @@
1
- require 'global_id/global_id'
2
1
  require 'active_support'
2
+ require 'global_id/global_id'
3
3
 
4
4
  autoload :SignedGlobalID, 'global_id/signed_global_id'
5
5
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-17 00:00:00.000000000 Z
11
+ date: 2023-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -59,8 +59,9 @@ files:
59
59
  homepage: http://www.rubyonrails.org
60
60
  licenses:
61
61
  - MIT
62
- metadata: {}
63
- post_install_message:
62
+ metadata:
63
+ rubygems_mfa_required: 'true'
64
+ post_install_message:
64
65
  rdoc_options: []
65
66
  require_paths:
66
67
  - lib
@@ -75,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubygems_version: 3.5.0.dev
79
- signing_key:
79
+ rubygems_version: 3.4.1
80
+ signing_key:
80
81
  specification_version: 4
81
82
  summary: 'Refer to any model with a URI: gid://app/class/id'
82
83
  test_files: []