Class: GlobalID

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Defined in:
lib/global_id.rb,
lib/global_id/locator.rb,
lib/global_id/verifier.rb,
lib/global_id/global_id.rb,
lib/global_id/identification.rb

Direct Known Subclasses

SignedGlobalID

Defined Under Namespace

Modules: Identification, Locator Classes: Verifier

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gid, options = {}) ⇒ GlobalID

Returns a new instance of GlobalID.


51
52
53
# File 'lib/global_id/global_id.rb', line 51

def initialize(gid, options = {})
  @uri = gid.is_a?(URI::GID) ? gid : URI::GID.parse(gid)
end

Class Attribute Details

.appObject

Returns the value of attribute app


10
11
12
# File 'lib/global_id/global_id.rb', line 10

def app
  @app
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri


48
49
50
# File 'lib/global_id/global_id.rb', line 48

def uri
  @uri
end

Class Method Details

.create(model, options = {}) ⇒ Object


12
13
14
15
16
17
18
19
20
# File 'lib/global_id/global_id.rb', line 12

def create(model, options = {})
  if app = options.fetch(:app) { GlobalID.app }
    params = options.except(:app, :verifier, :for)
    new URI::GID.create(app, model, params), options
  else
    raise ArgumentError, 'An app is required to create a GlobalID. ' \
      'Pass the :app option or set the default GlobalID.app.'
  end
end

.eager_load!Object


15
16
17
18
# File 'lib/global_id.rb', line 15

def self.eager_load!
  super
  require 'global_id/signed_global_id'
end

.find(gid, options = {}) ⇒ Object


22
23
24
# File 'lib/global_id/global_id.rb', line 22

def find(gid, options = {})
  parse(gid, options).try(:find, options)
end

.parse(gid, options = {}) ⇒ Object


26
27
28
29
30
# File 'lib/global_id/global_id.rb', line 26

def parse(gid, options = {})
  gid.is_a?(self) ? gid : new(gid, options)
rescue URI::Error
  parse_encoded_gid(gid, options)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


63
64
65
# File 'lib/global_id/global_id.rb', line 63

def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end

#find(options = {}) ⇒ Object


55
56
57
# File 'lib/global_id/global_id.rb', line 55

def find(options = {})
  Locator.locate self, options
end

#hashObject


68
69
70
# File 'lib/global_id/global_id.rb', line 68

def hash
  self.class.hash | @uri.hash
end

#model_classObject


59
60
61
# File 'lib/global_id/global_id.rb', line 59

def model_class
  model_name.constantize
end

#to_paramObject


72
73
74
75
# File 'lib/global_id/global_id.rb', line 72

def to_param
  # remove the = padding character for a prettier param -- it'll be added back in parse_encoded_gid
  Base64.urlsafe_encode64(to_s).sub(/=+$/, '')
end