globalid 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 452599d0406bd5fac9219973222368f7de7760a1
4
+ data.tar.gz: 0204d5224c0a299a96db75bf9d16beb39bbc9e26
5
+ SHA512:
6
+ metadata.gz: 12eff4acebdd4026d34cf7bd13a04670d0e6e8d639cb0102bb06e48e1171d75a161566b0fed51f0636daa67722f226374a76cd6b1a812d01fc23fdbdce5b695a
7
+ data.tar.gz: 1b8039217e3f7b7735a51fbe09159334192dedc424535c4738e16a5870db1480eb82941f50284b01c4dffca12ce85db2a06a77d29f17feadeba7fe250e9f4915
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2014 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
@@ -0,0 +1,7 @@
1
+ require 'global_id/global_id'
2
+ autoload :SignedGlobalID, 'global_id/signed_global_id'
3
+
4
+ class GlobalID
5
+ autoload :Locator, 'global_id/locator'
6
+ autoload :Identification, 'global_id/identification'
7
+ end
@@ -0,0 +1,64 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/string/inflections' # For #model_class constantize
3
+ require 'active_support/core_ext/array/access'
4
+ require 'active_support/core_ext/object/try' # For #find
5
+ require 'uri'
6
+
7
+ class GlobalID
8
+ class << self
9
+ attr_accessor :app
10
+
11
+ def create(model)
12
+ new URI("gid://#{GlobalID.app}/#{model.class.name}/#{model.id}")
13
+ end
14
+
15
+ def find(gid)
16
+ parse(gid).try :find
17
+ end
18
+
19
+ def parse(gid)
20
+ gid.is_a?(self) ? gid : new(gid)
21
+ rescue URI::Error
22
+ nil
23
+ end
24
+ end
25
+
26
+ attr_reader :uri, :app, :model_name, :model_id
27
+
28
+ def initialize(gid)
29
+ extract_uri_components gid
30
+ end
31
+
32
+ def find
33
+ model_class.find model_id
34
+ end
35
+
36
+ def model_class
37
+ model_name.constantize
38
+ end
39
+
40
+ def ==(other)
41
+ other.is_a?(GlobalID) && @uri == other.uri
42
+ end
43
+
44
+ def to_s
45
+ @uri.to_s
46
+ end
47
+
48
+ private
49
+ PATH_REGEXP = %r(\A/([^/]+)/([^/]+)\z)
50
+
51
+ # Pending a URI::GID to handle validation
52
+ def extract_uri_components(gid)
53
+ @uri = gid.is_a?(URI) ? gid : URI.parse(gid)
54
+ raise URI::BadURIError, "Not a gid:// URI scheme: #{@uri.inspect}" unless @uri.scheme == 'gid'
55
+
56
+ if @uri.path =~ PATH_REGEXP
57
+ @app = @uri.host
58
+ @model_name = $1
59
+ @model_id = $2
60
+ else
61
+ raise URI::InvalidURIError, "Expected a URI like gid://app/Person/1234: #{@uri.inspect}"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ require 'active_support/concern'
2
+
3
+ class GlobalID
4
+ module Identification
5
+ extend ActiveSupport::Concern
6
+
7
+ def global_id
8
+ @global_id ||= GlobalID.create(self)
9
+ end
10
+ alias gid global_id
11
+
12
+ def signed_global_id
13
+ @signed_global_id ||= SignedGlobalID.create(self)
14
+ end
15
+ alias sgid signed_global_id
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ class GlobalID
2
+ module Locator
3
+ class << self
4
+ # Takes either a GlobalID or a string that can be turned into a GlobalID
5
+ def locate(gid)
6
+ GlobalID.find gid
7
+ end
8
+
9
+ # Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
10
+ def locate_signed(sgid)
11
+ SignedGlobalID.find sgid
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'rails/railtie'
3
+ rescue LoadError
4
+ else
5
+
6
+ class GlobalID
7
+ # Set up the signed GlobalID verifier and include Active Record support.
8
+ class Railtie < Rails::Railtie # :nodoc:
9
+ initializer 'global_id' do
10
+ require 'global_id'
11
+
12
+ # TODO: expose as app config.global_id.app = 'name'
13
+ GlobalID.app = Rails.application.railtie_name.remove('_application')
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)
18
+ end
19
+
20
+ ActiveSupport.on_load(:active_record) do
21
+ require 'global_id/identification'
22
+ send :include, GlobalID::Identification
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,18 @@
1
+ require 'global_id'
2
+ require 'active_support/message_verifier'
3
+
4
+ class SignedGlobalID < GlobalID
5
+ class << self
6
+ attr_accessor :verifier
7
+ end
8
+
9
+ def self.parse(sgid)
10
+ sgid.is_a?(self) ? sgid : super(verifier.verify(sgid))
11
+ rescue ActiveSupport::MessageVerifier::InvalidSignature
12
+ nil
13
+ end
14
+
15
+ def to_s
16
+ @sgid ||= self.class.verifier.generate(super)
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ require 'global_id'
2
+ require 'global_id/railtie'
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: URIs for your models makes it easy to pass references around.
42
+ email: david@loudthinking.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - MIT-LICENSE
48
+ - lib/global_id/global_id.rb
49
+ - lib/global_id/identification.rb
50
+ - lib/global_id/locator.rb
51
+ - lib/global_id/railtie.rb
52
+ - lib/global_id/signed_global_id.rb
53
+ - lib/global_id.rb
54
+ - lib/globalid.rb
55
+ homepage: http://www.rubyonrails.org
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.9.3
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.0.14
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: 'Refer to any model with a URI: gid://app/class/id'
79
+ test_files: []