activemodel-globalid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f7e637641e9bef7b9102466221a912f79e190f84
4
+ data.tar.gz: b14880a4afca3cd86b76b9c5e52612d034558dd1
5
+ SHA512:
6
+ metadata.gz: 0431d4062d488553aa64f953b7be68bea97e8c954ddba8082eebececd36f80e66091020473772d82880895e8466b3337266fdfcd53c987fe0f9143af52132493
7
+ data.tar.gz: e25345afa813172b5a68251675d41e46077934062e4e7309bb4381ae2399fe2ddd0de5fbb21e56415a06e1b4013bd5a37f7e915d71c605d2436f4c0541aaa32b
data/MIT-LICENSE ADDED
@@ -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,30 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'active_support/core_ext/array/access'
3
+
4
+ module ActiveModel
5
+ class GlobalID
6
+ def self.create(model)
7
+ new "GlobalID-#{model.class.name}-#{model.id}"
8
+ end
9
+
10
+ def initialize(gid)
11
+ @gid = gid
12
+ end
13
+
14
+ def model_class
15
+ @model_klass ||= @gid.split("-").second.constantize
16
+ end
17
+
18
+ def model_id
19
+ @model_id ||= @gid.split("-").third
20
+ end
21
+
22
+ def ==(other_global_id)
23
+ other_global_id.is_a?(GlobalID) && to_s == other_global_id.to_s
24
+ end
25
+
26
+ def to_s
27
+ @gid
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_model/global_id'
2
+ require 'active_model/signed_global_id'
3
+
4
+ module ActiveModel
5
+ module GlobalIdentification
6
+ extend ActiveSupport::Concern
7
+
8
+ def global_id
9
+ GlobalID.create(self)
10
+ end
11
+ alias gid global_id
12
+
13
+ def signed_global_id
14
+ SignedGlobalID.create(self)
15
+ end
16
+ alias sgid signed_global_id
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_model/global_id'
2
+ require 'active_model/signed_global_id'
3
+
4
+ module ActiveModel
5
+ class GlobalLocator
6
+ class << self
7
+ # Takes either a GlobalID or a string that can be turned into a GlobalID
8
+ def locate(gid)
9
+ if gid.is_a? GlobalID
10
+ gid.model_class.find(gid.model_id)
11
+ elsif properly_formatted_gid?(gid)
12
+ locate GlobalID.new(gid)
13
+ end
14
+ end
15
+
16
+ # Takes either a SignedGlobalID or a string that can be turned into a SignedGlobalID
17
+ def locate_signed(sgid)
18
+ if sgid.is_a? SignedGlobalID
19
+ sgid.model_class.find(sgid.model_id)
20
+ else
21
+ locate SignedGlobalID.new(sgid)
22
+ end
23
+ end
24
+
25
+ private
26
+ def properly_formatted_gid?(gid)
27
+ gid =~ /GlobalID-([^-]+)-(.+)/
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_model/global_identification'
2
+
3
+ ActiveModel::SignedGlobalID.verifier = Rails.application.message_verifier(:signed_global_ids)
4
+
5
+ ActiveRecord::Base.send :include, ActiveModel::GlobalIdentification
@@ -0,0 +1,25 @@
1
+ require 'active_model/global_id'
2
+ require 'active_support/core_ext/module/attribute_accessors'
3
+
4
+
5
+ module ActiveModel
6
+ class SignedGlobalID < GlobalID
7
+ cattr_accessor :verifier
8
+
9
+ def self.create(model)
10
+ new verifier.generate("GlobalID-#{model.class.name}-#{model.id}")
11
+ end
12
+
13
+ def initialize(sgid)
14
+ @gid = self.class.verifier.verify(sgid)
15
+ end
16
+
17
+ def ==(other_global_id)
18
+ other_global_id.is_a?(SignedGlobalID) && to_s == other_global_id.to_s
19
+ end
20
+
21
+ def to_s
22
+ @sgid ||= self.class.verifier.generate(@gid)
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-globalid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-19 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: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 4.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 4.1.0
41
+ description: Serializing models to a single string makes it easy to pass references
42
+ around.
43
+ email: david@loudthinking.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - lib/active_model/global_id.rb
50
+ - lib/active_model/global_identification.rb
51
+ - lib/active_model/global_locator.rb
52
+ - lib/active_model/railtie.rb
53
+ - lib/active_model/signed_global_id.rb
54
+ homepage: http://www.rubyonrails.org
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 1.9.3
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.14
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Serialize models that can be found by id again (will be part of Active Model).
78
+ test_files: []