globalid 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of globalid might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 452599d0406bd5fac9219973222368f7de7760a1
4
- data.tar.gz: 0204d5224c0a299a96db75bf9d16beb39bbc9e26
3
+ metadata.gz: 82142d7e95abab35aacc3b0a9aed4e6d44308834
4
+ data.tar.gz: e21a62ca151b606aa6639d8cd58110d40e3de2ef
5
5
  SHA512:
6
- metadata.gz: 12eff4acebdd4026d34cf7bd13a04670d0e6e8d639cb0102bb06e48e1171d75a161566b0fed51f0636daa67722f226374a76cd6b1a812d01fc23fdbdce5b695a
7
- data.tar.gz: 1b8039217e3f7b7735a51fbe09159334192dedc424535c4738e16a5870db1480eb82941f50284b01c4dffca12ce85db2a06a77d29f17feadeba7fe250e9f4915
6
+ metadata.gz: 488500c69b84eafbfea3e445b8c96c01716438cdd1fa894d038869e823cbbca6ce84cbda829bc7a5f4722f42779d3c82aa6b12f10b5b4d7152e7120fc7611c2a
7
+ data.tar.gz: 5280cee5f515c54f3625bb407a297f48b685104895181d9b98ce282762aa18f0160e570aa6987e4cac756f741dac62fa738df4262f64477ae9c89a7a6ca551ce
@@ -8,19 +8,32 @@ class GlobalID
8
8
  class << self
9
9
  attr_accessor :app
10
10
 
11
- def create(model)
12
- new URI("gid://#{GlobalID.app}/#{model.class.name}/#{model.id}")
11
+ def create(model, options = {})
12
+ app = options.fetch :app, GlobalID.app
13
+ raise ArgumentError, "An app is required to create a GlobalID. Pass the :app option or set the default GlobalID.app." unless app
14
+ new URI("gid://#{app}/#{model.class.name}/#{model.id}")
13
15
  end
14
16
 
15
- def find(gid)
16
- parse(gid).try :find
17
+ def find(gid, options = {})
18
+ parse(gid).try(:find, options)
17
19
  end
18
20
 
19
21
  def parse(gid)
20
22
  gid.is_a?(self) ? gid : new(gid)
21
23
  rescue URI::Error
22
- nil
24
+ parse_encoded_gid(gid)
23
25
  end
26
+
27
+ def parse_encoded_gid(gid)
28
+ new Base64.urlsafe_decode64(repad_gid(gid)) rescue nil
29
+ end
30
+
31
+ private
32
+ # We removed the base64 padding character = during #to_param, now we're adding it back so decoding will work
33
+ def repad_gid(gid)
34
+ padding_chars = 4 - gid.length % 4
35
+ gid + '=' * padding_chars
36
+ end
24
37
  end
25
38
 
26
39
  attr_reader :uri, :app, :model_name, :model_id
@@ -29,8 +42,8 @@ class GlobalID
29
42
  extract_uri_components gid
30
43
  end
31
44
 
32
- def find
33
- model_class.find model_id
45
+ def find(options = {})
46
+ model_class.find model_id if find_allowed?(options[:only])
34
47
  end
35
48
 
36
49
  def model_class
@@ -45,6 +58,11 @@ class GlobalID
45
58
  @uri.to_s
46
59
  end
47
60
 
61
+ def to_param
62
+ # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
63
+ Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
64
+ end
65
+
48
66
  private
49
67
  PATH_REGEXP = %r(\A/([^/]+)/([^/]+)\z)
50
68
 
@@ -61,4 +79,8 @@ class GlobalID
61
79
  raise URI::InvalidURIError, "Expected a URI like gid://app/Person/1234: #{@uri.inspect}"
62
80
  end
63
81
  end
82
+
83
+ def find_allowed?(only = nil)
84
+ only ? Array(only).any? { |c| model_class <= c } : true
85
+ end
64
86
  end
@@ -2,13 +2,27 @@ class GlobalID
2
2
  module Locator
3
3
  class << self
4
4
  # Takes either a GlobalID or a string that can be turned into a GlobalID
5
- def locate(gid)
6
- GlobalID.find gid
5
+ #
6
+ # Options:
7
+ # * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
8
+ # allowed to be located. Passing one or more classes limits instances of returned
9
+ # classes to those classes or their subclasses. Passing one or more modules in limits
10
+ # instances of returned classes to those including that module. If no classes or
11
+ # modules match, +nil+ is returned.
12
+ def locate(gid, options = {})
13
+ GlobalID.find gid, options
7
14
  end
8
15
 
9
16
  # Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
10
- def locate_signed(sgid)
11
- SignedGlobalID.find sgid
17
+ #
18
+ # Options:
19
+ # * <tt>:only</tt> - A class, module or Array of classes and/or modules that are
20
+ # allowed to be located. Passing one or more classes limits instances of returned
21
+ # classes to those classes or their subclasses. Passing one or more modules in limits
22
+ # instances of returned classes to those including that module. If no classes or
23
+ # modules match, +nil+ is returned.
24
+ def locate_signed(sgid, options = {})
25
+ SignedGlobalID.find sgid, options
12
26
  end
13
27
  end
14
28
  end
@@ -2,19 +2,22 @@ begin
2
2
  require 'rails/railtie'
3
3
  rescue LoadError
4
4
  else
5
+ require 'global_id'
5
6
 
6
7
  class GlobalID
8
+ # = GlobalID Railtie
7
9
  # Set up the signed GlobalID verifier and include Active Record support.
8
10
  class Railtie < Rails::Railtie # :nodoc:
9
- initializer 'global_id' do
10
- require 'global_id'
11
+ config.global_id = ActiveSupport::OrderedOptions.new
11
12
 
12
- # TODO: expose as app config.global_id.app = 'name'
13
- GlobalID.app = Rails.application.railtie_name.remove('_application')
13
+ initializer 'global_id' do |app|
14
14
 
15
- # TODO: expose as app config.global_id.verifier = custom_verifier
16
- config.after_initialize do |app|
17
- SignedGlobalID.verifier = app.message_verifier(:signed_global_ids)
15
+ app.config.global_id.app ||= app.railtie_name.remove('_application')
16
+ GlobalID.app = app.config.global_id.app
17
+
18
+ config.after_initialize do
19
+ app.config.global_id.verifier ||= app.message_verifier(:signed_global_ids)
20
+ SignedGlobalID.verifier = app.config.global_id.verifier
18
21
  end
19
22
 
20
23
  ActiveSupport.on_load(:active_record) do
@@ -15,4 +15,5 @@ class SignedGlobalID < GlobalID
15
15
  def to_s
16
16
  @sgid ||= self.class.verifier.generate(super)
17
17
  end
18
+ alias to_param to_s
18
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: globalid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -14,28 +14,28 @@ dependencies:
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: URIs for your models makes it easy to pass references around.
@@ -45,12 +45,12 @@ extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - MIT-LICENSE
48
+ - lib/global_id.rb
48
49
  - lib/global_id/global_id.rb
49
50
  - lib/global_id/identification.rb
50
51
  - lib/global_id/locator.rb
51
52
  - lib/global_id/railtie.rb
52
53
  - lib/global_id/signed_global_id.rb
53
- - lib/global_id.rb
54
54
  - lib/globalid.rb
55
55
  homepage: http://www.rubyonrails.org
56
56
  licenses:
@@ -62,17 +62,17 @@ require_paths:
62
62
  - lib
63
63
  required_ruby_version: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - '>='
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: 1.9.3
68
68
  required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  requirements:
70
- - - '>='
70
+ - - ">="
71
71
  - !ruby/object:Gem::Version
72
72
  version: '0'
73
73
  requirements: []
74
74
  rubyforge_project:
75
- rubygems_version: 2.0.14
75
+ rubygems_version: 2.2.2
76
76
  signing_key:
77
77
  specification_version: 4
78
78
  summary: 'Refer to any model with a URI: gid://app/class/id'